コード例 #1
0
ファイル: SampleDataTests.cs プロジェクト: vnisor/DiscUtils
        private void ValidateContent(DiscFileSystem xfs)
        {
            Assert.True(xfs.DirectoryExists(""));
            Assert.True(xfs.FileExists("folder\\nested\\file"));
            Assert.Equal(0, xfs.GetFileSystemEntries("empty").Length);
            for (int i = 1; i <= 1000; i++)
            {
                Assert.True(xfs.FileExists($"folder\\file.{i}"), $"File file.{i} not found");
            }

            using (var file = xfs.OpenFile("folder\\file.100", FileMode.Open))
            {
                var md5 = MD5.Create().ComputeHash(file);
                Assert.Equal("620f0b67a91f7f74151bc5be745b7110", BitConverter.ToString(md5).ToLowerInvariant().Replace("-", string.Empty));
            }

            using (var file = xfs.OpenFile("folder\\file.random", FileMode.Open))
            {
                var md5 = MD5.Create().ComputeHash(file);
                Assert.Equal("9a202a11d6e87688591eb97714ed56f1", BitConverter.ToString(md5).ToLowerInvariant().Replace("-", string.Empty));
            }

            for (int i = 1; i <= 999; i++)
            {
                Assert.True(xfs.FileExists($"huge\\{i}"), $"File huge/{i} not found");
            }
        }
コード例 #2
0
 public void AddFile(string fsFilePath, string filePath)
 {
     using (FileStream fsStream = File.OpenRead(fsFilePath))
         using (var vhdStream = discFs.OpenFile(filePath, FileMode.OpenOrCreate))
         {
             fsStream.CopyTo(vhdStream);
         }
 }
コード例 #3
0
        public void Length(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.GetFileInfo("foo.txt").Open(FileMode.Create, FileAccess.ReadWrite))
            {
                s.SetLength(3128);
            }

            Assert.Equal(3128, fs.GetFileInfo("foo.txt").Length);

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Open, FileAccess.ReadWrite))
            {
                s.SetLength(3);
                Assert.Equal(3, s.Length);
            }

            Assert.Equal(3, fs.GetFileInfo("foo.txt").Length);

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Open, FileAccess.ReadWrite))
            {
                s.SetLength(3333);

                byte[] buffer = new byte[512];
                for (int i = 0; i < buffer.Length; ++i)
                {
                    buffer[i] = (byte)i;
                }

                s.Write(buffer, 0, buffer.Length);
                s.Write(buffer, 0, buffer.Length);

                Assert.Equal(1024, s.Position);
                Assert.Equal(3333, s.Length);

                s.SetLength(512);

                Assert.Equal(512, s.Length);
            }

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Open, FileAccess.ReadWrite))
            {
                byte[] buffer    = new byte[512];
                int    numRead   = s.Read(buffer, 0, buffer.Length);
                int    totalRead = 0;
                while (numRead != 0)
                {
                    totalRead += numRead;
                    numRead    = s.Read(buffer, totalRead, buffer.Length - totalRead);
                }

                for (int i = 0; i < buffer.Length; ++i)
                {
                    Assert.Equal((byte)i, buffer[i]);
                }
            }
        }
コード例 #4
0
        public void LastWriteTime(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Create)) { }
            DiscFileInfo fi = fs.GetFileInfo("foo.txt");

            DateTime baseTime = DateTime.Now - TimeSpan.FromMinutes(10);

            fi.LastWriteTime = baseTime;

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Open)) { s.WriteByte(1); }

            Assert.True(baseTime < fi.LastWriteTime);
        }
コード例 #5
0
        public void LastAccessTime(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Create)) { }
            DiscFileInfo fi = fs.GetFileInfo("foo.txt");

            DateTime baseTime = DateTime.Now - TimeSpan.FromDays(2);

            fi.LastAccessTime = baseTime;

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Open, FileAccess.Read)) { }

            Assert.True(baseTime < fi.LastAccessTime);
        }
コード例 #6
0
ファイル: Provider.cs プロジェクト: tsharp/DiscUtils
        private void DoCopyFile(DiscFileSystem srcFs, string srcPath, DiscFileSystem destFs, string destPath)
        {
            IWindowsFileSystem destWindowsFs = destFs as IWindowsFileSystem;
            IWindowsFileSystem srcWindowsFs  = srcFs as IWindowsFileSystem;

            using (Stream src = srcFs.OpenFile(srcPath, FileMode.Open, FileAccess.Read))
                using (Stream dest = destFs.OpenFile(destPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    dest.SetLength(src.Length);
                    byte[] buffer  = new byte[1024 * 1024];
                    int    numRead = src.Read(buffer, 0, buffer.Length);
                    while (numRead > 0)
                    {
                        dest.Write(buffer, 0, numRead);
                        numRead = src.Read(buffer, 0, buffer.Length);
                    }
                }

            if (srcWindowsFs != null && destWindowsFs != null)
            {
                if ((srcWindowsFs.GetAttributes(srcPath) & FileAttributes.ReparsePoint) != 0)
                {
                    destWindowsFs.SetReparsePoint(destPath, srcWindowsFs.GetReparsePoint(srcPath));
                }

                var sd = srcWindowsFs.GetSecurity(srcPath);
                if (sd != null)
                {
                    destWindowsFs.SetSecurity(destPath, sd);
                }
            }

            destFs.SetAttributes(destPath, srcFs.GetAttributes(srcPath));
            destFs.SetCreationTimeUtc(destPath, srcFs.GetCreationTimeUtc(srcPath));
        }
コード例 #7
0
        public void GetFileSystemInfos(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            fs.CreateDirectory(@"SOMEDIR\CHILD\GCHILD");
            fs.CreateDirectory(@"AAA.EXT");
            using (Stream s = fs.OpenFile(@"FOO.TXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\CHILD.EXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\FOO.TXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", FileMode.Create)) { }

            Assert.Equal(3, fs.Root.GetFileSystemInfos().Length);

            Assert.Equal(1, fs.Root.GetFileSystemInfos("*.EXT").Length);
            Assert.Equal(2, fs.Root.GetFileSystemInfos("*.?XT").Length);
        }
コード例 #8
0
        public override void PerformStep()
        {
            logger.LogInformation("Fixing cloned OS registry.");

            string vhdFullName = $"{migrationData.VhdFileTemporaryFolder}\\{migrationData.VhdFileName}";

            using (VirtualDiskDecorator disk = this.fileSystemHelper.OpenVhdx(vhdFullName))
            {
                PartitionInfoDecorator clonedPartition = disk.Partitions[0];
                using (Stream partitionStream = clonedPartition.Open())
                {
                    using (DiscFileSystem ntfs = this.fileSystemHelper.OpenNtfsFileSystem(partitionStream))
                    {
                        if (ntfs is NtfsFileSystem)
                        {
                            (ntfs as NtfsFileSystem).NtfsOptions.HideSystemFiles = false;
                            (ntfs as NtfsFileSystem).NtfsOptions.HideHiddenFiles = false;
                        }

                        // removes not necessary files from the image
                        // Remove VSS snapshot files (can be very large)
                        foreach (string filePath in ntfs.GetFiles(@"\System Volume Information", "*{3808876B-C176-4e48-B7AE-04046E6CC752}"))
                        {
                            ntfs.DeleteFile(filePath);
                        }

                        // Remove the page file
                        if (ntfs.FileExists(@"\Pagefile.sys"))
                        {
                            ntfs.DeleteFile(@"\Pagefile.sys");
                        }

                        // Remove the hibernation file
                        if (ntfs.FileExists(@"\hiberfil.sys"))
                        {
                            ntfs.DeleteFile(@"\hiberfil.sys");
                        }

                        using (Stream systemRegistryStream = ntfs.OpenFile(@"windows\system32\config\system", FileMode.Open))
                        {
                            this.fileSystemHelper.ChangeSystemDriveMappingFromRegistry(systemRegistryStream);
                        }
                    }
                }

                if (migrationData.TemporaryVhdFileIsTheFinalOne)
                {
                    char vhdLocationDriveLetter = migrationData.VhdFileTemporaryFolder[0];
                    var  driveInfo = this.fileSystem.DriveInfo.FromDriveName(vhdLocationDriveLetter.ToString() + ":\\");
                    if (driveInfo.AvailableFreeSpace <= disk.Geometry.Capacity)
                    {
                        logger.LogWarning($"The image is located in a drive which has not enough free space for it to expand. If you will not free some space on drive '{driveInfo.Name}' then you will see BSOD when trying to boot from the created image.");
                    }
                }
            }
            return;
        }
コード例 #9
0
        public void CreationTime(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Create)) { }

            Assert.True(DateTime.Now >= fs.GetFileInfo("foo.txt").CreationTime);
            Assert.True(DateTime.Now.Subtract(TimeSpan.FromSeconds(10)) <= fs.GetFileInfo("foo.txt").CreationTime);
        }
コード例 #10
0
        public void Delete(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.OpenFile("foo.txt", FileMode.Create)) { }
            fs.GetFileInfo("foo.txt").Delete();

            Assert.False(fs.FileExists("foo.txt"));
        }
コード例 #11
0
        public void GetFiles(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            fs.CreateDirectory(@"SOMEDIR\CHILD\GCHILD");
            fs.CreateDirectory(@"AAA.DIR");
            using (Stream s = fs.OpenFile(@"FOO.TXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\CHILD.TXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\FOO.TXT", FileMode.Create)) { }
            using (Stream s = fs.OpenFile(@"SOMEDIR\CHILD\GCHILD\BAR.TXT", FileMode.Create)) { }

            Assert.Equal(1, fs.Root.GetFiles().Length);
            Assert.Equal("FOO.TXT", fs.Root.GetFiles()[0].FullName);

            Assert.Equal(2, fs.Root.GetDirectories("SOMEDIR")[0].GetFiles("*.TXT").Length);
            Assert.Equal(4, fs.Root.GetFiles("*.TXT", SearchOption.AllDirectories).Length);

            Assert.Equal(0, fs.Root.GetFiles("*.DIR", SearchOption.AllDirectories).Length);
        }
コード例 #12
0
        public void Parent(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            fs.CreateDirectory(@"SOMEDIR\ADIR");
            using (Stream s = fs.OpenFile(@"SOMEDIR\ADIR\FILE.TXT", FileMode.Create)) { }

            DiscFileInfo fi = fs.GetFileInfo(@"SOMEDIR\ADIR\FILE.TXT");

            Assert.Equal(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Parent);
            Assert.Equal(fs.GetDirectoryInfo(@"SOMEDIR\ADIR"), fi.Directory);
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: radtek/USBInsanity
        protected override void DoRun()
        {
            VolumeManager volMgr = new VolumeManager();

            foreach (string disk in _diskFiles.Values)
            {
                volMgr.AddDisk(VirtualDisk.OpenDisk(disk, _diskType.IsPresent ? _diskType.Value : null, FileAccess.Read, UserName, Password));
            }


            VolumeInfo volInfo = null;

            if (!string.IsNullOrEmpty(VolumeId))
            {
                volInfo = volMgr.GetVolume(VolumeId);
            }
            else if (Partition >= 0)
            {
                volInfo = volMgr.GetPhysicalVolumes()[Partition];
            }
            else
            {
                volInfo = volMgr.GetLogicalVolumes()[0];
            }


            DiscUtils.FileSystemInfo fsInfo = FileSystemManager.DetectDefaultFileSystems(volInfo)[0];


            using (DiscFileSystem fs = fsInfo.Open(volInfo))
            {
                using (Stream source = fs.OpenFile(_inFilePath.Value, FileMode.Open, FileAccess.Read))
                {
                    using (FileStream outFile = new FileStream(_outFilePath.Value, FileMode.Create, FileAccess.ReadWrite))
                    {
                        PumpStreams(source, outFile);
                    }

                    if (_hexDump.IsPresent)
                    {
                        source.Position = 0;
                        HexDump.Generate(source, Console.Out);
                    }
                }
            }
        }
コード例 #14
0
ファイル: Form1.cs プロジェクト: froize/isoview
 public static void ReadIsoFolder(DiscFileSystem cdReader, string sIsoPath, string sDestinationRootPath)
 {
     try
     {
         string[] saFiles = cdReader.GetFiles(sIsoPath);
         foreach (string sFile in saFiles)
         {
             DiscFileInfo dfiIso = cdReader.GetFileInfo(sFile);
             string sDestinationPath = Path.Combine(sDestinationRootPath, dfiIso.DirectoryName.Substring(0, dfiIso.DirectoryName.Length - 1));
             if (!Directory.Exists(sDestinationPath))
             {
                 Directory.CreateDirectory(sDestinationPath);
             }
             string sDestinationFile = Path.Combine(sDestinationPath, dfiIso.Name);
             SparseStream streamIsoFile = cdReader.OpenFile(sFile, FileMode.Open);
             FileStream fsDest = new FileStream(sDestinationFile, FileMode.Create);
             byte[] baData = new byte[0x4000];
             while (true)
             {
                 int nReadCount = streamIsoFile.Read(baData, 0, baData.Length);
                 if (nReadCount < 1)
                 {
                     break;
                 }
                 else
                 {
                     fsDest.Write(baData, 0, nReadCount);
                 }
             }
             streamIsoFile.Close();
             fsDest.Close();
         }
         string[] saDirectories = cdReader.GetDirectories(sIsoPath);
         foreach (string sDirectory in saDirectories)
         {
             ReadIsoFolder(cdReader, sDirectory, sDestinationRootPath);
         }
         return;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
コード例 #15
0
        public void CreateFile(NewFileSystemDelegate fsFactory)
        {
            DiscFileSystem fs = fsFactory();

            using (Stream s = fs.GetFileInfo("foo.txt").Open(FileMode.Create, FileAccess.ReadWrite))
            {
                s.WriteByte(1);
            }

            DiscFileInfo fi = fs.GetFileInfo("foo.txt");

            Assert.True(fi.Exists);
            Assert.Equal(FileAttributes.Archive, fi.Attributes);
            Assert.Equal(1, fi.Length);

            using (Stream s = fs.OpenFile("Foo.txt", FileMode.Open, FileAccess.Read))
            {
                Assert.Equal(1, s.ReadByte());
            }
        }
コード例 #16
0
 /// <summary>
 /// Opens the specified file.
 /// </summary>
 /// <param name="path">The full path of the file to open.</param>
 /// <param name="mode">The file mode for the created stream.</param>
 /// <param name="access">The access permissions for the created stream.</param>
 /// <returns>The new stream.</returns>
 public override Stream OpenFile(string path, FileMode mode, FileAccess access)
 {
     return(_wrapped.OpenFile(path, mode, access));
 }
コード例 #17
0
ファイル: Provider.cs プロジェクト: JGTM2016/discutils
        private void DoCopyFile(DiscFileSystem srcFs, string srcPath, DiscFileSystem destFs, string destPath)
        {
            IWindowsFileSystem destWindowsFs = destFs as IWindowsFileSystem;
            IWindowsFileSystem srcWindowsFs = srcFs as IWindowsFileSystem;

            using (Stream src = srcFs.OpenFile(srcPath, FileMode.Open, FileAccess.Read))
            using (Stream dest = destFs.OpenFile(destPath, FileMode.Create, FileAccess.ReadWrite))
            {
                dest.SetLength(src.Length);
                byte[] buffer = new byte[1024 * 1024];
                int numRead = src.Read(buffer, 0, buffer.Length);
                while (numRead > 0)
                {
                    dest.Write(buffer, 0, numRead);
                    numRead = src.Read(buffer, 0, buffer.Length);
                }
            }

            if (srcWindowsFs != null && destWindowsFs != null)
            {
                if ((srcWindowsFs.GetAttributes(srcPath) & FileAttributes.ReparsePoint) != 0)
                {
                    destWindowsFs.SetReparsePoint(destPath, srcWindowsFs.GetReparsePoint(srcPath));
                }

                var sd = srcWindowsFs.GetSecurity(srcPath);
                if(sd != null)
                {
                    destWindowsFs.SetSecurity(destPath, sd);
                }
            }

            destFs.SetAttributes(destPath, srcFs.GetAttributes(srcPath));
            destFs.SetCreationTimeUtc(destPath, srcFs.GetCreationTimeUtc(srcPath));
        }
コード例 #18
0
 /// <summary>
 /// Opens the specified file.
 /// </summary>
 /// <param name="path">The full path of the file to open.</param>
 /// <param name="mode">The file mode for the created stream.</param>
 /// <returns>The new stream.</returns>
 public override SparseStream OpenFile(string path, FileMode mode)
 {
     return(_wrapped.OpenFile(path, mode));
 }
コード例 #19
0
ファイル: MediaInfoManager.cs プロジェクト: realM4C/thumbgen
        private static string RedirectISO(string filename)
        {
            string _result = null;

            try
            {
                if (!string.IsNullOrEmpty(filename) && File.Exists(filename))
                {
                    List <string> _candidates = new List <string>();

                    // open the ISO file
                    VolumeManager volMgr = new VolumeManager();
                    volMgr.AddDisk(VirtualDisk.OpenDisk(filename, FileAccess.Read));

                    VolumeInfo volInfo = null;
                    volInfo = volMgr.GetLogicalVolumes()[0];

                    DiscUtils.FileSystemInfo fsInfo = FileSystemManager.DetectDefaultFileSystems(volInfo)[0];

                    using (DiscFileSystem _dfs = fsInfo.Open(volInfo))
                    {
                        foreach (string _fname in _dfs.GetFiles("", "*.*", SearchOption.AllDirectories))
                        {
                            string _fileExt = System.IO.Path.GetExtension(_fname).ToLowerInvariant();
                            if (_fileExt == ".ifo" || _fileExt == ".mpls")
                            {
                                _candidates.Add(_fname);
                            }
                        }

                        double _biggestDuration = 0d;
                        string _tmpBRResult     = "";
                        // select from the candidates the one that has the longest duration (if mpls skip files bigger than 10K)
                        foreach (string _cpath in _candidates)
                        {
                            string _cext = Path.GetExtension(_cpath).ToLowerInvariant();
                            string _tmp  = Helpers.GetUniqueFilename(_cext);
                            using (FileStream _fs = new FileStream(_tmp, FileMode.Create, FileAccess.Write))
                            {
                                using (Stream source = _dfs.OpenFile(_cpath, FileMode.Open, FileAccess.Read))
                                {
                                    source.CopyTo(_fs);
                                }
                            }

                            // if it is a DVD iso
                            if (_cext == ".ifo")
                            {
                                if (string.Compare(Path.GetFileNameWithoutExtension(_cpath), "video_ts", true) == 0)
                                {
                                    File.Delete(_tmp);
                                    // skip the menu
                                    continue;
                                }

                                // use first ifo that is not the menu
                                FileManager.AddToGarbageFiles(_tmp);
                                _result = _tmp;
                                break;
                            }

                            // if it is a BLURAY iso (choose biggest mpls file)
                            if (_cext == ".mpls")
                            {
                                long _length = new FileInfo(_tmp).Length;
                                if (Path.GetExtension(_cpath).ToLowerInvariant() == ".mpls" && _length > 10 * 1024)
                                {
                                    File.Delete(_tmp);
                                    continue; // take next one, this is too big and mediainfo will hang
                                }

                                if (GetDurationMilliseconds(_tmp) > _biggestDuration)
                                {
                                    // important.. add it to the garbage files
                                    //FileManager.AddToGarbageFiles(_tmp);
                                    //_result = _tmp;
                                    if (!string.IsNullOrEmpty(_tmpBRResult))
                                    {
                                        File.Delete(_tmpBRResult); // remove previous winner and remember the current one
                                    }
                                    _tmpBRResult = _tmp;
                                }
                                else
                                {
                                    File.Delete(_tmp);
                                }
                            }
                        }
                        if (string.IsNullOrEmpty(_result) && !string.IsNullOrEmpty(_tmpBRResult))
                        {
                            FileManager.AddToGarbageFiles(_tmpBRResult);
                            _result = _tmpBRResult;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Loggy.Logger.DebugException("ISO Processing", ex);
            }

            return(_result);
        }