Beispiel #1
0
        unsafe private void SetupMFT_Enum_DataBuffer(ref IntPtr medBuffer)
        {
            uint bytesReturned = 0;

            MFTReader.USN_JOURNAL_DATA ujd = new MFTReader.USN_JOURNAL_DATA();

            bool bOk = MFTReader.DeviceIoControl(_changeJournalRootHandle,           // Handle to drive
                                                 MFTReader.FSCTL_QUERY_USN_JOURNAL,  // IO Control Code
                                                 IntPtr.Zero,                        // In Buffer
                                                 0,                                  // In Buffer Size
                                                 out ujd,                            // Out Buffer
                                                 sizeof(MFTReader.USN_JOURNAL_DATA), // Size Of Out Buffer
                                                 out bytesReturned,                  // Bytes Returned
                                                 IntPtr.Zero);                       // lpOverlapped

            if (bOk)
            {
                MFTReader.MFT_ENUM_DATA med;
                med.StartFileReferenceNumber = 0;
                med.LowUsn  = 0;
                med.HighUsn = ujd.NextUsn;
                int sizeMftEnumData = Marshal.SizeOf(med);
                medBuffer = Marshal.AllocHGlobal(sizeMftEnumData);
                MFTReader.ZeroMemory(medBuffer, sizeMftEnumData);
                Marshal.StructureToPtr(med, medBuffer, true);
            }
            else
            {
                throw new IOException("DeviceIoControl() returned false", new Win32Exception(Marshal.GetLastWin32Error()));
            }
        }
Beispiel #2
0
        unsafe private void CreateChangeJournal()
        {
            // This function creates a journal on the volume. If a journal already
            // exists this function will adjust the MaximumSize and AllocationDelta
            // parameters of the journal
            UInt64 MaximumSize     = 0x800000;
            UInt64 AllocationDelta = 0x100000;
            UInt32 cb;

            MFTReader.CREATE_USN_JOURNAL_DATA cujd;
            cujd.MaximumSize     = MaximumSize;
            cujd.AllocationDelta = AllocationDelta;

            int    sizeCujd   = Marshal.SizeOf(cujd);
            IntPtr cujdBuffer = Marshal.AllocHGlobal(sizeCujd);

            MFTReader.ZeroMemory(cujdBuffer, sizeCujd);
            Marshal.StructureToPtr(cujd, cujdBuffer, true);

            bool fOk = MFTReader.DeviceIoControl(_changeJournalRootHandle, MFTReader.FSCTL_CREATE_USN_JOURNAL,
                                                 cujdBuffer, sizeCujd, IntPtr.Zero, 0, out cb, IntPtr.Zero);

            if (!fOk)
            {
                throw new IOException("DeviceIoControl() returned false", new Win32Exception(Marshal.GetLastWin32Error()));
            }
        }
Beispiel #3
0
        public void ScanExes()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            Stopwatch stop = new Stopwatch();

            for (int i = 0; i < drives.Length; i++)
            {
                DriveInfo d = drives[i];

                if (d.DriveFormat != "NTFS")
                {
                    continue;
                }

                LogManager.Log("> Searching drive {0} for game executables", d.Name);

                stop.Reset();
                stop.Start();

                Dictionary<ulong, FileNameAndParentFrn> mDict = new Dictionary<ulong, FileNameAndParentFrn>();
                MFTReader mft = new MFTReader();
                mft.Drive = d.RootDirectory.FullName;

                mft.EnumerateVolume(out mDict, new string[] { ".exe" });
                foreach (KeyValuePair<UInt64, FileNameAndParentFrn> entry in mDict)
                {
                    FileNameAndParentFrn file = (FileNameAndParentFrn)entry.Value;

                    string name = file.Name;
                    string lower = name.ToLower();

                    GameInfo game;
                    if (gameManager.GameInfos.TryGetValue(lower, out game))
                    {
                        string path = mft.GetFullPath(file);
                        LogManager.Log("Found game: {0}, full path: {1}", game.GameName, path);

                        UserGameInfo info = new UserGameInfo();
                        info.InitializeDefault(game, path);
                        gameManager.User.Games.Add(info);
                    }
                }

                stop.Stop();
                LogManager.Log("> Took {0} seconds to search drive {1}", stop.Elapsed.TotalSeconds.ToString("0.00"), d.Name);
            }

            gameManager.SaveUserProfile();
            gameManager.WaitSave();
        }
Beispiel #4
0
        private void GetRootHandle()
        {
            string vol = string.Concat("\\\\.\\", _drive);

            _changeJournalRootHandle = MFTReader.CreateFile(vol,
                                                            MFTReader.GENERIC_READ | MFTReader.GENERIC_WRITE,
                                                            MFTReader.FILE_SHARE_READ | MFTReader.FILE_SHARE_WRITE,
                                                            IntPtr.Zero,
                                                            MFTReader.OPEN_EXISTING,
                                                            0,
                                                            IntPtr.Zero);
            if (_changeJournalRootHandle.ToInt32() == MFTReader.INVALID_HANDLE_VALUE)
            {
                throw new IOException("CreateFile() returned invalid handle",
                                      new Win32Exception(Marshal.GetLastWin32Error()));
            }
        }
Beispiel #5
0
        public void EnumerateVolume(
            out Dictionary <UInt64, FileNameAndParentFrn> files, string[] fileExtensions)
        {
            files = new Dictionary <ulong, FileNameAndParentFrn>();
            IntPtr medBuffer = IntPtr.Zero;

            try
            {
                GetRootFrnEntry();
                GetRootHandle();

                CreateChangeJournal();

                SetupMFT_Enum_DataBuffer(ref medBuffer);
                EnumerateFiles(medBuffer, ref files, fileExtensions);
            }
            catch (Exception e)
            {
                //	Log.Info(e.Message, e);
                Exception innerException = e.InnerException;
                while (innerException != null)
                {
                    //		Log.Info(innerException.Message, innerException);
                    innerException = innerException.InnerException;
                }
                throw new ApplicationException("Error in EnumerateVolume()", e);
            }
            finally
            {
                if (_changeJournalRootHandle.ToInt32() != MFTReader.INVALID_HANDLE_VALUE)
                {
                    MFTReader.CloseHandle(_changeJournalRootHandle);
                }
                if (medBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(medBuffer);
                }
            }
        }
Beispiel #6
0
        private void GetRootFrnEntry()
        {
            string driveRoot = string.Concat("\\\\.\\", _drive);

            driveRoot = string.Concat(driveRoot, Path.DirectorySeparatorChar);
            IntPtr hRoot = MFTReader.CreateFile(driveRoot,
                                                0,
                                                MFTReader.FILE_SHARE_READ | MFTReader.FILE_SHARE_WRITE,
                                                IntPtr.Zero,
                                                MFTReader.OPEN_EXISTING,
                                                MFTReader.FILE_FLAG_BACKUP_SEMANTICS,
                                                IntPtr.Zero);

            if (hRoot.ToInt32() != MFTReader.INVALID_HANDLE_VALUE)
            {
                MFTReader.BY_HANDLE_FILE_INFORMATION fi = new MFTReader.BY_HANDLE_FILE_INFORMATION();
                bool bRtn = MFTReader.GetFileInformationByHandle(hRoot, out fi);
                if (bRtn)
                {
                    UInt64 fileIndexHigh = (UInt64)fi.FileIndexHigh;
                    UInt64 indexRoot     = (fileIndexHigh << 32) | fi.FileIndexLow;

                    FileNameAndParentFrn f = new FileNameAndParentFrn(driveRoot, 0);
                    _directories.Add(indexRoot, f);
                }
                else
                {
                    throw new IOException("GetFileInformationbyHandle() returned invalid handle",
                                          new Win32Exception(Marshal.GetLastWin32Error()));
                }
                MFTReader.CloseHandle(hRoot);
            }
            else
            {
                throw new IOException("Unable to get root frn entry", new Win32Exception(Marshal.GetLastWin32Error()));
            }
        }
Beispiel #7
0
        unsafe public void EnumerateFiles(IntPtr medBuffer, ref Dictionary <ulong, FileNameAndParentFrn> files, string[] fileExtensions)
        {
            IntPtr pData = Marshal.AllocHGlobal(sizeof(UInt64) + 0x10000);

            MFTReader.ZeroMemory(pData, sizeof(UInt64) + 0x10000);
            uint outBytesReturned = 0;

            while (false != MFTReader.DeviceIoControl(_changeJournalRootHandle, MFTReader.FSCTL_ENUM_USN_DATA, medBuffer,
                                                      sizeof(MFTReader.MFT_ENUM_DATA), pData, sizeof(UInt64) + 0x10000, out outBytesReturned,
                                                      IntPtr.Zero))
            {
                IntPtr pUsnRecord = new IntPtr(pData.ToInt32() + sizeof(Int64));
                while (outBytesReturned > 60)
                {
                    MFTReader.USN_RECORD usn = new MFTReader.USN_RECORD(pUsnRecord);
                    if (0 != (usn.FileAttributes & MFTReader.FILE_ATTRIBUTE_DIRECTORY))
                    {
                        //
                        // handle directories
                        //
                        if (!_directories.ContainsKey(usn.FileReferenceNumber))
                        {
                            _directories.Add(usn.FileReferenceNumber,
                                             new FileNameAndParentFrn(usn.FileName, usn.ParentFileReferenceNumber));
                        }
                        else
                        {   // this is debug code and should be removed when we are certain that
                            // duplicate frn's don't exist on a given drive.  To date, this exception has
                            // never been thrown.  Removing this code improves performance....
                            throw new Exception(string.Format("Duplicate FRN: {0} for {1}",
                                                              usn.FileReferenceNumber, usn.FileName));
                        }
                    }
                    else
                    {
                        //
                        // handle files
                        //

                        // at this point we could get the * for the extension
                        bool add      = true;
                        bool fullpath = false;
                        if (fileExtensions != null && fileExtensions.Length != 0)
                        {
                            if (fileExtensions[0].ToString() == "*")
                            {
                                add      = true;
                                fullpath = true;
                            }
                            else
                            {
                                add = false;
                                string s = Path.GetExtension(usn.FileName);
                                foreach (string extension in fileExtensions)
                                {
                                    if (0 == string.Compare(s, extension, true))
                                    {
                                        add = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if (add)
                        {
                            if (fullpath)
                            {
                                if (!files.ContainsKey(usn.FileReferenceNumber))
                                {
                                    files.Add(usn.FileReferenceNumber,
                                              new FileNameAndParentFrn(usn.FileName, usn.ParentFileReferenceNumber));
                                }
                                else
                                {
                                    FileNameAndParentFrn frn = files[usn.FileReferenceNumber];
                                    if (0 != string.Compare(usn.FileName, frn.Name, true))
                                    {
                                        //	Log.InfoFormat(
                                        //	"Attempt to add duplicate file reference number: {0} for file {1}, file from index {2}",
                                        //	usn.FileReferenceNumber, usn.FileName, frn.Name);
                                        throw new Exception(string.Format("Duplicate FRN: {0} for {1}",
                                                                          usn.FileReferenceNumber, usn.FileName));
                                    }
                                }
                            }
                            else
                            {
                                if (!files.ContainsKey(usn.FileReferenceNumber))
                                {
                                    files.Add(usn.FileReferenceNumber,
                                              new FileNameAndParentFrn(usn.FileName, usn.ParentFileReferenceNumber));
                                }
                                else
                                {
                                    FileNameAndParentFrn frn = files[usn.FileReferenceNumber];
                                    if (0 != string.Compare(usn.FileName, frn.Name, true))
                                    {
                                        //	Log.InfoFormat(
                                        //	"Attempt to add duplicate file reference number: {0} for file {1}, file from index {2}",
                                        //	usn.FileReferenceNumber, usn.FileName, frn.Name);
                                        throw new Exception(string.Format("Duplicate FRN: {0} for {1}",
                                                                          usn.FileReferenceNumber, usn.FileName));
                                    }
                                }
                            }
                        }
                    }
                    pUsnRecord        = new IntPtr(pUsnRecord.ToInt32() + usn.RecordLength);
                    outBytesReturned -= usn.RecordLength;
                }
                Marshal.WriteInt64(medBuffer, Marshal.ReadInt64(pData, 0));
            }
            Marshal.FreeHGlobal(pData);
        }
        private void SearchDrive(object state)
        {
            int i = (int)state;
            SearchDriveInfo info = toSearch[i];
            if (!info.drive.IsReady)
            {
                done++;
                return;
            }

            LogManager.Log("> Searching drive {0} for game executables", info.drive.Name);

            Dictionary<ulong, FileNameAndParentFrn> mDict = new Dictionary<ulong, FileNameAndParentFrn>();
            MFTReader mft = new MFTReader();
            mft.Drive = info.drive.RootDirectory.FullName;

            mft.EnumerateVolume(out mDict, new string[] { ".exe" });

            progress += (1 / (float)toSearch.Count) / 2.0f;
            UpdateProgress();

            float increment = (1 / (float)toSearch.Count) / (float)mDict.Count;
            foreach (KeyValuePair<UInt64, FileNameAndParentFrn> entry in mDict)
            {
                if (closed)
                {
                    return;
                }

                progress += increment;

                FileNameAndParentFrn file = (FileNameAndParentFrn)entry.Value;

                string name = file.Name;
                string lower = name.ToLower();

                if (GameManager.Instance.AnyGame(lower))
                {
                    string path = mft.GetFullPath(file);
                    if (path.Contains("$Recycle.Bin"))
                    {
                        // noope
                        continue;
                    }

                    UserGameInfo uinfo = GameManager.Instance.TryAddGame(path);

                    if (uinfo != null)
                    {
                        LogManager.Log("> Found new game {0} on drive {1}", uinfo.Game.GameName, info.drive.Name);
                        Invoke(new Action(delegate
                        {
                            listGames.Items.Add(uinfo.Game.GameName + " - " + path);
                            listGames.Invalidate();
                            main.NewUserGame(uinfo);
                        }));
                    }
                }
            }

            if (closed)
            {
                return;
            }
            UpdateProgress();

            done++;
            if (done == toSearch.Count)
            {
                searching = false;
                Invoke(new Action(delegate
                {
                    progress = 1;
                    UpdateProgress();
                    btnSearch.Enabled = true;
                    main.RefreshGames();
                    MessageBox.Show("Finished searching!");
                }));
            }
        }