Example #1
0
 public ActionDownload(ShowItem si, ProcessedEpisode pe, FileInfo dest, string path, bool mede8erShrink)
 {
     this.Episode            = pe;
     this.SI                 = si;
     this.Destination        = dest;
     this.Path               = path;
     ShrinkLargeMede8erImage = mede8erShrink;
 }
Example #2
0
 public RecoverXML(string hint)
 {
     this.InitializeComponent();
     this.SettingsFile = null;
     this.DBFile       = null;
     if (!string.IsNullOrEmpty(hint))
     {
         this.lbHint.Text = hint + "\r\n ";
     }
 }
Example #3
0
        //http://stackoverflow.com/questions/1600962/displaying-the-build-date
        //note: [assembly: AssemblyVersion("1.0.*")] // important: use wildcard for build and revision numbers!
        public static string GetBuildDateVersion()
        {
            Assembly assm          = Assembly.GetExecutingAssembly();
            Version  version       = assm.GetName().Version;
            DateTime buildDateTime = new fs.FileInfo(assm.Location).LastWriteTime;

            return(string.Format("Version: {0}, Build DateTime: {1}", version, buildDateTime));

            //ToString( "yyMMdd.HHmm" )
//			return string.Format( "{0}.{1}.{2}.{3}", version.Major, version.Minor, buildDateTime.ToString( "yy" ), buildDateTime.DayOfYear.ToString( "D3" ) );
        }
Example #4
0
        /// <summary>
        /// The user may already have some files in the OutputFolder. If so we can go through these and
        /// figure out which need to be updated, deleted, or left alone
        /// </summary>
        public async Task OptimizeModlist()
        {
            Utils.Log("Optimizing Modlist directives");
            var indexed = ModList.Directives.ToDictionary(d => d.To);

            UpdateTracker.NextStep("Looking for files to delete");
            await Directory.EnumerateFiles(OutputFolder, "*", DirectoryEnumerationOptions.Recursive)
            .PMap(Queue, UpdateTracker, f =>
            {
                var relative_to = f.RelativeTo(OutputFolder);
                Utils.Status($"Checking if modlist file {relative_to}");
                if (indexed.ContainsKey(relative_to) || f.IsInPath(DownloadFolder))
                {
                    return;
                }

                Utils.Log($"Deleting {relative_to} it's not part of this modlist");
                File.Delete(f);
            });

            UpdateTracker.NextStep("Looking for unmodified files");
            (await indexed.Values.PMap(Queue, UpdateTracker, d =>
            {
                // Bit backwards, but we want to return null for
                // all files we *want* installed. We return the files
                // to remove from the install list.
                Status($"Optimizing {d.To}");
                var path = Path.Combine(OutputFolder, d.To);
                if (!File.Exists(path))
                {
                    return(null);
                }

                var fi = new FileInfo(path);
                if (fi.Length != d.Size)
                {
                    return(null);
                }

                return(path.FileHash() == d.Hash ? d : null);
            }))
            .Where(d => d != null)
            .Do(d => indexed.Remove(d.To));

            UpdateTracker.NextStep("Updating Modlist");
            Utils.Log($"Optimized {ModList.Directives.Count} directives to {indexed.Count} required");
            var requiredArchives = indexed.Values.OfType <FromArchive>()
                                   .GroupBy(d => d.ArchiveHashPath[0])
                                   .Select(d => d.Key)
                                   .ToHashSet();

            ModList.Archives   = ModList.Archives.Where(a => requiredArchives.Contains(a.Hash)).ToList();
            ModList.Directives = indexed.Values.ToList();
        }
Example #5
0
        /// <summary>
        /// Calulate the file's SHA, size and last modified
        /// </summary>
        internal void Analyze()
        {
            if (!IsStaged)
            {
                throw new InvalidDataException("Cannot analyze an unstaged file");
            }

            var fio = new FileInfo(StagedPath);

            Size         = fio.Length;
            Hash         = Utils.FileSHA256(StagedPath);
            LastModified = fio.LastWriteTime.ToMilliseconds();
        }
        public static FilesystemFileInfo CreateFromEntry(string filesystemPath, string name)
        {
            var info = new FilesystemFileInfo
            {
                Name           = name,
                FilesystemPath = filesystemPath
            };

            var fsInfo = new FileInfo(filesystemPath);

            info.CachedSize = fsInfo.Length;
            return(info);
        }
Example #7
0
        public DirCacheEntry(FileInfo f)
        {
            this.TheFile            = f;
            this.SimplifiedFullName = Helpers.SimplifyName(f.FullName);
            this.LowerName          = f.Name.ToLower();
            this.Length             = f.Length;

            if (TVSettings.Instance == null)
            {
                return;
            }

            this.HasUsefulExtension_NotOthersToo = TVSettings.Instance.UsefulExtension(f.Extension, false);
            this.HasUsefulExtension_OthersToo    = this.HasUsefulExtension_NotOthersToo | TVSettings.Instance.UsefulExtension(f.Extension, true);
        }
Example #8
0
 protected void DeleteOrRecycleFile(FileInfo file)
 {
     if (file == null)
     {
         return;
     }
     if (_tidyup.DeleteEmptyIsRecycle)
     {
         Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(file.FullName, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
     }
     else
     {
         file.Delete(true);
     }
 }
Example #9
0
        public void VerifyAllFiles()
        {
            var skip_files = new HashSet <string> {
                "portable.txt"
            };

            foreach (var dest_file in Directory.EnumerateFiles(InstallFolder, "*", DirectoryEnumerationOptions.Recursive))
            {
                var rel_file = dest_file.RelativeTo(InstallFolder);
                if (rel_file.StartsWith(Consts.LOOTFolderFilesDir) || rel_file.StartsWith(Consts.GameFolderFilesDir))
                {
                    continue;
                }

                if (!skip_files.Contains(rel_file))
                {
                    Assert.IsTrue(File.Exists(Path.Combine(MO2Folder, rel_file)), $"Only in Destination: {rel_file}");
                }
            }

            var skip_extensions = new HashSet <string> {
                ".txt", ".ini"
            };

            foreach (var src_file in Directory.EnumerateFiles(MO2Folder, "*", DirectoryEnumerationOptions.Recursive))
            {
                var rel_file = src_file.RelativeTo(MO2Folder);

                if (rel_file.StartsWith("downloads\\"))
                {
                    continue;
                }

                var dest_file = Path.Combine(InstallFolder, rel_file);
                Assert.IsTrue(File.Exists(dest_file), $"Only in Source: {rel_file}");

                var fi_src  = new FileInfo(src_file);
                var fi_dest = new FileInfo(dest_file);

                if (!skip_extensions.Contains(Path.GetExtension(src_file)))
                {
                    Assert.AreEqual(fi_src.Length, fi_dest.Length, $"Differing sizes {rel_file}");
                    Assert.AreEqual(src_file.FileHash(), dest_file.FileHash(), $"Differing content hash {rel_file}");
                }
            }
        }
Example #10
0
        // 导出数据
        private void ExportData(object sender, RoutedEventArgs e)
        {
            if (!File.Exists("./data.db"))
            {
                MessageWindow.ShowDialog("文件 data.db 不存在", this);
                return;
            }

            SaveFileDialog dlg = new SaveFileDialog
            {
                Title      = "另存为",
                FileName   = "data.db",
                DefaultExt = ".db",
                Filter     = "Data base file|*.db"
            };

            if (dlg.ShowDialog() == true)
            {
                FileInfo fi = new FileInfo("./data.db");
                fi.CopyTo(dlg.FileName);
            }
        }
Example #11
0
        private void bnOK_Click(object sender, System.EventArgs e)
        {
            // we added a 'none' item at the top of the list, so adjust for that

            int n = this.lbDB.SelectedIndex;

            if (n == -1)
            {
                n = 0;
            }
            this.DBFile = (n == 0) ? null : this.DBList[n - 1];

            n = this.lbSettings.SelectedIndex;
            if (n == -1)
            {
                n = 0;
            }
            this.SettingsFile = (n == 0) ? null : this.SettingsList[n];

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
Example #12
0
        private static bool CopyFile(USNJournalSyncLog syncLog, SyncMountpoint syncFrom)
        {
            logger.Info($"[{syncLog.Id}] [C] {syncLog.Action.RelativePath}");

            var copyAction = syncLog.Action as UpdateAction;

            var publicPath   = syncFrom.Mountpoint.Reference.PublicPath;
            var relativePath = copyAction.RelativePath;

            if (publicPath == null)
            {
                throw new NullReferenceException("publicPath");
            }
            if (relativePath == null)
            {
                throw new NullReferenceException("relativePath");
            }

            var    destination = Path.Get(syncFrom.Path, relativePath);
            string tempPath    = Path.Get(syncFrom.Path, ".proximaTemp.", "toCopy", syncLog.Id).FullPath;
            var    source      = Path.Get(publicPath, relativePath);

            if (source.IsDirectory)
            {
                DirectoryInfo sourceInfo = new DirectoryInfo(source.FullPath);
                DirectoryInfo tempInfo   = new DirectoryInfo(tempPath);
                DirectoryInfo destInfo   = new DirectoryInfo(destination.FullPath);
                sourceInfo.CopyTo(tempInfo.FullName);
                tempInfo.MoveTo(destInfo.FullName);
            }
            else
            {
                source.Copy(tempPath);
                FileInfo tempInfo = new FileInfo(tempPath);
                tempInfo.MoveTo(destination.FullPath, MoveOptions.ReplaceExisting);
            }

            return(true);
        }
Example #13
0
        private static void DeleteDirectory(string path)
        {
            Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
            .DoProgress("Cleaning VFS Files", file =>
            {
                try
                {
                    var fi         = new FileInfo(file);
                    fi.Attributes &= ~FileAttributes.ReadOnly;
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    Utils.Log(ex.ToString());
                }
            });

            Directory.EnumerateDirectories(path, DirectoryEnumerationOptions.Recursive)
            .OrderByDescending(d => d.Length)
            .DoProgress("Cleaning VFS Folders", folder =>
            {
                try
                {
                    if (!Directory.Exists(folder))
                    {
                        return;
                    }
                    var di         = new DirectoryInfo(folder);
                    di.Attributes &= ~FileAttributes.ReadOnly;
                    Directory.Delete(path, true);
                }
                catch (Exception ex)
                {
                    Utils.Log(ex.ToString());
                }
            });
        }
Example #14
0
        static void TransformWebConfig(Path websitePath, string transformConfiguration, Path tempWebsitePath)
        {
            if (string.IsNullOrWhiteSpace(transformConfiguration))
            {
                return;
            }

            Path transformRootFile = Path.Combine(websitePath, "web.config");

            Path transformationFile = Path.Combine(websitePath, string.Format("web.{0}.config", transformConfiguration));

            if (File.Exists(transformRootFile.FullName) && File.Exists(transformationFile.FullName))
            {
                Path targetFile = Path.Combine(tempWebsitePath, "web.config");

                string fullName = new FileInfo(targetFile.FullName).Directory.FullName;
                if (Directory.Exists(fullName))
                {
                    Console.WriteLine("Transforming root file '{0}' with transformation '{1}' into '{2}'", transformRootFile.FullName, transformationFile.FullName, targetFile.FullName);

                    var transformable = new XmlTransformableDocument();
                    transformable.Load(transformRootFile.FullName);

                    var transformation = new XmlTransformation(transformationFile.FullName);

                    if (transformation.Apply(transformable))
                    {
                        transformable.Save(targetFile.FullName);
                    }
                }
                else
                {
                    Console.WriteLine("Directory {0} does not exist", fullName);
                }
            }
        }
Example #15
0
 public FileLocalArticle(string path)
     : base()
 {
     Info = new FileInfo(path);
 }
Example #16
0
        public override void Check(SetProgressDelegate prog, int startpct, int totPct)
        {
            // get list of files being downloaded by uTorrent
            string resDatFile = TVSettings.Instance.ResumeDatPath;

            if (string.IsNullOrEmpty(resDatFile) || !File.Exists(resDatFile))
            {
                return;
            }

            BTResume btr = new BTResume(prog, resDatFile);

            if (!btr.LoadResumeDat())
            {
                return;
            }

            List <TorrentEntry> downloading = btr.AllFilesBeingDownloaded();

            ItemList newList  = new ItemList();
            ItemList toRemove = new ItemList();
            int      c        = this.ActionList.Count + 2;
            int      n        = 1;

            prog.Invoke(startpct);
            foreach (Item action1 in this.ActionList)
            {
                if (this.ActionCancel)
                {
                    return;
                }

                prog.Invoke(startpct + (totPct - startpct) * (++n) / (c));


                if (!(action1 is ItemMissing action))
                {
                    continue;
                }

                foreach (TorrentEntry te in downloading)
                {
                    FileInfo file = new FileInfo(te.DownloadingTo);
                    if (!TVSettings.Instance.UsefulExtension(file.Extension, false)) // not a usefile file extension
                    {
                        continue;
                    }

                    //do any of the possible names for the series match the filename?
                    bool matched = (action.Episode.SI.getSimplifiedPossibleShowNames().Any(name => FileHelper.SimplifyAndCheckFilename(file.FullName, name)));

                    if (!matched)
                    {
                        continue;
                    }

                    if (TVDoc.FindSeasEp(file, out int seasF, out int epF, out int maxEp, action.Episode.SI) && (seasF == action.Episode.AppropriateSeasonNumber) && (epF == action.Episode.AppropriateEpNum))
                    {
                        toRemove.Add(action1);
                        newList.Add(new ItemuTorrenting(te, action.Episode, action.TheFileNoExt));
                        break;
                    }
                }
            }

            foreach (Item i in toRemove)
            {
                this.ActionList.Remove(i);
            }

            foreach (Item action in newList)
            {
                this.ActionList.Add(action);
            }

            prog.Invoke(totPct);
        }
        /// <summary>Shows the Object's available Properties and Values.</summary>
        private static void Dump(object obj, int width = -35, bool indent = false)
        {
            int          cnt      = 0;
            const string nulll    = "\t\tnull";
            string       template = "\t{0}#{1:000}\t{2, " + width + "} == \t[{3}]";

            if (obj == null)
            {
                Console.WriteLine(nulll);
                return;
            }

            Console.WriteLine("\n\t{0}Instance: [{1}]\n", indent ? "\t" : "", obj.GetType().FullName);

            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj).Sort().Cast <PropertyDescriptor>().Where(descriptor => descriptor != null))
            {
                string propValue;
                try
                {
                    object value = descriptor.GetValue(obj);
                    propValue = (value == null) ? "null" : value.ToString();
                }
                catch (Exception ex)
                {
                    // Please do tell, oneliner preferably.
                    propValue = ex.Message.Replace(Environment.NewLine, "  ");
                }


                switch (descriptor.Name)
                {
                case "Directory":
                    if (obj.GetType().Namespace.Equals("Alphaleonis.Win32.Filesystem", StringComparison.OrdinalIgnoreCase))
                    {
                        if (obj != null)
                        {
                            FileInfo fi = (FileInfo)obj;
                            if (fi != null && fi.Directory != null)
                            {
                                propValue = fi.Directory.ToString();
                            }
                        }
                    }
                    break;

                case "EntryInfo":
                    if (obj.GetType().Namespace.Equals("Alphaleonis.Win32.Filesystem", StringComparison.OrdinalIgnoreCase))
                    {
                        if (obj != null)
                        {
                            FileInfo fi = (FileInfo)obj;
                            if (fi != null && fi.EntryInfo != null)
                            {
                                propValue = fi.EntryInfo.FullPath;
                            }
                        }
                    }
                    break;
                }

                Console.WriteLine(template, indent ? "\t" : "", ++cnt, descriptor.Name, propValue);
            }
        }
Example #18
0
 public ActionMede8erXML(FileInfo nfo, ShowItem si)
 {
     this.SI      = si;
     this.Episode = null;
     this.Where   = nfo;
 }
Example #19
0
        private static void UpdateFromRepo()
        {
            Console.WriteLine();

            _logger.Info(
                "Checking for updated maps at https://github.com/EricZimmerman/evtx/tree/master/evtx/Maps...");
            Console.WriteLine();
            var archivePath = Path.Combine(BaseDirectory, "____master.zip");

            if (File.Exists(archivePath))
            {
                File.Delete(archivePath);
            }

            using (var client = new WebClient())
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;
                client.DownloadFile("https://github.com/EricZimmerman/evtx/archive/master.zip", archivePath);
            }

            var fff = new FastZip();

            if (Directory.Exists(Path.Combine(BaseDirectory, "Maps")) == false)
            {
                Directory.CreateDirectory(Path.Combine(BaseDirectory, "Maps"));
            }

            var directoryFilter = "Maps";

            // Will prompt to overwrite if target file names already exist
            fff.ExtractZip(archivePath, BaseDirectory, FastZip.Overwrite.Always, null,
                           null, directoryFilter, true);

            if (File.Exists(archivePath))
            {
                File.Delete(archivePath);
            }

            var newMapPath = Path.Combine(BaseDirectory, "evtx-master", "evtx", "Maps");

            var orgMapPath = Path.Combine(BaseDirectory, "Maps");

            var newMaps = Directory.GetFiles(newMapPath);

            var newlocalMaps = new List <string>();

            var updatedlocalMaps = new List <string>();

            if (File.Exists(Path.Combine(orgMapPath, "Security_4624.map")))
            {
                _logger.Warn($"Old map format found. Zipping to '!!oldMaps.zip' and cleaning up old maps");
                //old maps found, so zip em first
                var oldZip = Path.Combine(orgMapPath, "!!oldMaps.zip");
                fff.CreateZip(oldZip, orgMapPath, false, @"\.map$");
                foreach (var m in Directory.GetFiles(orgMapPath, "*.map"))
                {
                    File.Delete(m);
                }
            }

            if (File.Exists(Path.Combine(orgMapPath, "!!!!README.txt")))
            {
                File.Delete(Path.Combine(orgMapPath, "!!!!README.txt"));
            }

            foreach (var newMap in newMaps)
            {
                var mName = Path.GetFileName(newMap);
                var dest  = Path.Combine(orgMapPath, mName);

                if (File.Exists(dest) == false)
                {
                    //new target
                    newlocalMaps.Add(mName);
                }
                else
                {
                    //current destination file exists, so compare to new
                    var fiNew = new FileInfo(newMap);
                    var fi    = new FileInfo(dest);

                    if (fiNew.GetHash(HashType.SHA1) != fi.GetHash(HashType.SHA1))
                    {
                        //updated file
                        updatedlocalMaps.Add(mName);
                    }
                }

                File.Copy(newMap, dest, CopyOptions.None);
            }



            if (newlocalMaps.Count > 0 || updatedlocalMaps.Count > 0)
            {
                _logger.Fatal("Updates found!");
                Console.WriteLine();

                if (newlocalMaps.Count > 0)
                {
                    _logger.Error("New maps");
                    foreach (var newLocalMap in newlocalMaps)
                    {
                        _logger.Info(Path.GetFileNameWithoutExtension(newLocalMap));
                    }

                    Console.WriteLine();
                }

                if (updatedlocalMaps.Count > 0)
                {
                    _logger.Error("Updated maps");
                    foreach (var um in updatedlocalMaps)
                    {
                        _logger.Info(Path.GetFileNameWithoutExtension(um));
                    }

                    Console.WriteLine();
                }

                Console.WriteLine();
            }
            else
            {
                Console.WriteLine();
                _logger.Info("No new maps available");
                Console.WriteLine();
            }

            Directory.Delete(Path.Combine(BaseDirectory, "evtx-master"), true);
        }
Example #20
0
        private async Task InstallArchive(WorkQueue queue, Archive archive, string absolutePath, IGrouping <string, FromArchive> grouping)
        {
            Status($"Extracting {archive.Name}");

            List <FromArchive> vFiles = grouping.Select(g =>
            {
                var file   = VFS.Index.FileForArchiveHashPath(g.ArchiveHashPath);
                g.FromFile = file;
                return(g);
            }).ToList();

            var onFinish = await VFS.Stage(vFiles.Select(f => f.FromFile).Distinct());


            Status($"Copying files for {archive.Name}");

            void CopyFile(string from, string to, bool useMove)
            {
                if (File.Exists(to))
                {
                    var fi = new FileInfo(to);
                    if (fi.IsReadOnly)
                    {
                        fi.IsReadOnly = false;
                    }
                    File.Delete(to);
                }

                if (File.Exists(from))
                {
                    var fi = new FileInfo(from);
                    if (fi.IsReadOnly)
                    {
                        fi.IsReadOnly = false;
                    }
                }


                if (useMove)
                {
                    File.Move(from, to);
                }
                else
                {
                    File.Copy(from, to);
                }
                // If we don't do this, the file will use the last-modified date of the file when it was compressed
                // into an archive, which isn't really what we want in the case of files installed archives
                File.SetLastWriteTime(to, DateTime.Now);
            }

            await vFiles.GroupBy(f => f.FromFile)
            .PDoIndexed(queue, (idx, group) =>
            {
                Utils.Status("Installing files", idx * 100 / vFiles.Count);
                var firstDest = Path.Combine(OutputFolder, group.First().To);
                CopyFile(group.Key.StagedPath, firstDest, true);

                foreach (var copy in group.Skip(1))
                {
                    var nextDest = Path.Combine(OutputFolder, copy.To);
                    CopyFile(firstDest, nextDest, false);
                }
            });

            Status("Unstaging files");
            onFinish();

            // Now patch all the files from this archive
            foreach (var toPatch in grouping.OfType <PatchedFromArchive>())
            {
                using (var patchStream = new MemoryStream())
                {
                    Status($"Patching {Path.GetFileName(toPatch.To)}");
                    // Read in the patch data

                    byte[] patchData = LoadBytesFromPath(toPatch.PatchID);

                    var toFile  = Path.Combine(OutputFolder, toPatch.To);
                    var oldData = new MemoryStream(File.ReadAllBytes(toFile));

                    // Remove the file we're about to patch
                    File.Delete(toFile);

                    // Patch it
                    using (var outStream = File.Open(toFile, FileMode.Create))
                    {
                        BSDiff.Apply(oldData, () => new MemoryStream(patchData), outStream);
                    }

                    Status($"Verifying Patch {Path.GetFileName(toPatch.To)}");
                    var resultSha = toFile.FileHash();
                    if (resultSha != toPatch.Hash)
                    {
                        throw new InvalidDataException($"Invalid Hash for {toPatch.To} after patching");
                    }
                }
            }
        }
Example #21
0
      internal static FileInfo GetFileInfoInternal(string path, System.IO.FileAttributes attributes, FileAttributes fileAttributes, bool checkInvalidPathChars, bool continueOnException)
      {
         // Prevent possible crash.
         FileInfo fileInfo = new FileInfo
         {
            DisplayName = string.Empty,
            TypeName = string.Empty,
            IconIndex = 0
         };

         if (!Utils.IsNullOrWhiteSpace(path))
         {
            // ShGetFileInfo()
            // In the ANSI version of this function, the name is limited to 248 characters.
            // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
            // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
            // However, the function fails when using Unicode format.

            IntPtr shGetFileInfo = NativeMethods.ShGetFileInfo(Path.GetRegularPathInternal(path, checkInvalidPathChars ? GetFullPathOptions.CheckInvalidPathChars : 0), attributes, out fileInfo, (uint) Marshal.SizeOf(fileInfo), fileAttributes);

            if (shGetFileInfo == IntPtr.Zero && !continueOnException)
               NativeError.ThrowException(Marshal.GetLastWin32Error(), path);
         }

         return fileInfo;
      }
Example #22
0
      private void DumpGetSize(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);
         string tempPath = Path.GetTempPath("File-GetSize()-file-" + Path.GetRandomFileName());
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         Console.WriteLine("\nInput File Path: [{0}]\n", tempPath);

         int randomLines = new Random().Next(1000, 100000);

         // Create file with contents.
         FileInfo fi = new FileInfo(tempPath);
         using (StreamWriter sw = fi.CreateText())
            for (int i = 0; i < randomLines; i++)
               sw.WriteLine(TextHelloWorld);


         long fileGetSize = File.GetSize(tempPath);
         long fileGetCompressedSize = File.GetCompressedSize(tempPath);
         long fiLength = fi.Length;

         Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize);
         Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize);

         Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength);
         Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes);

         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "File should be uncompressed.");
         Assert.IsTrue(fiLength == fileGetSize, "Uncompressed size should match.");
         Assert.IsTrue(fiLength == fileGetCompressedSize, "Uncompressed size should match.");

         #endregion // Setup

         #region Compress

         bool compressOk = false;
         string report;
         StopWatcher(true);

         try
         {
            fi.Compress();
            report = Reporter(true);
            compressOk = true;

            fileGetSize = File.GetSize(tempPath);
            fileGetCompressedSize = File.GetCompressedSize(tempPath);
         }
         catch (Exception ex)
         {
            report = Reporter(true);
            Console.WriteLine("\n\tFile.Compress(): Caught unexpected Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, "  "));
         }


         // FileInfo() must Refresh().
         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "FileInfo() should not know it is compressed.");
         fi.Refresh();
         fiLength = fi.Length;
         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "FileInfo() should know it is compressed.");


         Console.WriteLine("\n\n\tFile.Compress() (Should be True): [{0}]{1}\n", compressOk, report);

         Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize);
         Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize);

         Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength);
         Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes);

         Assert.IsTrue(compressOk);

         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "File should be compressed.");
         Assert.IsTrue(fiLength != fileGetCompressedSize, "FileInfo() size should not match compressed size.");
         Assert.IsTrue(fiLength == fileGetSize, "File size should match FileInfo() size.");

         #endregion // Compress

         #region Decompress

         bool decompressOk = false;
         StopWatcher(true);

         try
         {
            File.Decompress(tempPath);
            report = Reporter(true);
            decompressOk = true;

            fileGetSize = File.GetSize(tempPath);
            fileGetCompressedSize = File.GetCompressedSize(tempPath);

         }
         catch (Exception ex)
         {
            report = Reporter(true);
            Console.WriteLine("\n\tFile.Decompress(): Caught unexpected Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, "  "));
         }


         // FileInfo() must Refresh().
         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) != 0, "FileInfo() should not know it is compressed.");
         fi.Refresh();
         fiLength = fi.Length;
         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "FileInfo() should know it is compressed.");


         Console.WriteLine("\n\n\tFile.Decompress() (Should be True): [{0}]{1}\n", decompressOk, report);

         Console.WriteLine("\tFile.GetSize()\t\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetSize), fileGetSize);
         Console.WriteLine("\tFile.GetCompressedSize()\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fileGetCompressedSize), fileGetCompressedSize);

         Console.WriteLine("\tFileInfo().Length\t\t== [{0}] [{1} bytes]", Utils.UnitSizeToText(fiLength), fiLength);
         Console.WriteLine("\tFileInfo().Attributes\t\t== [{0}]", fi.Attributes);

         Assert.IsTrue(decompressOk);

         Assert.IsTrue((fi.Attributes & FileAttributes.Compressed) == 0, "File should be uncompressed.");
         Assert.IsTrue(fiLength == fileGetSize, "Uncompressed size should match.");
         Assert.IsTrue(fiLength == fileGetCompressedSize, "Uncompressed size should match.");

         #endregion //Decompress

         fi.Delete();
         fi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(fi.Exists, "Cleanup failed: File should have been removed.");
         Console.WriteLine();
      }
Example #23
0
        private static void Main(string[] args)
        {
            ExceptionlessClient.Default.Startup("Kruacm8p1B6RFAw2WMnKcEqkQcnWRkF3RmPSOzlW");

            SetupNLog();
            SetupPatterns();

            _logger = LogManager.GetCurrentClassLogger();

            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("File to search. Either this or -d is required");

            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription("Directory to recursively process. Either this or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.SaveTo)
            .As('o')
            .WithDescription("File to save results to");

            _fluentCommandLineParser.Setup(arg => arg.GetAscii)
            .As('a')
            .SetDefault(true)
            .WithDescription("If set, look for ASCII strings. Default is true. Use -a false to disable");

            _fluentCommandLineParser.Setup(arg => arg.GetUnicode)
            .As('u')
            .SetDefault(true)
            .WithDescription("If set, look for Unicode strings. Default is true. Use -u false to disable");

            _fluentCommandLineParser.Setup(arg => arg.MinimumLength)
            .As('m').SetDefault(3).WithDescription("Minimum string length. Default is 3");

            _fluentCommandLineParser.Setup(arg => arg.BlockSizeMb)
            .As('b').SetDefault(512).WithDescription("Chunk size in MB. Valid range is 1 to 1024. Default is 512");

            _fluentCommandLineParser.Setup(arg => arg.Quiet)
            .As('q').SetDefault(false).WithDescription("Quiet mode (Do not show header or total number of hits)");

            _fluentCommandLineParser.Setup(arg => arg.QuietQuiet)
            .As('s')
            .SetDefault(false)
            .WithDescription(
                "Really Quiet mode (Do not display hits to console. Speeds up processing when using -o)");

            _fluentCommandLineParser.Setup(arg => arg.MaximumLength)
            .As('x').SetDefault(-1).WithDescription("Maximum string length. Default is unlimited\r\n");

            _fluentCommandLineParser.Setup(arg => arg.GetPatterns)
            .As('p').SetDefault(false).WithDescription("Display list of built in regular expressions");

            _fluentCommandLineParser.Setup(arg => arg.LookForString)
            .As("ls")
            .SetDefault(string.Empty)
            .WithDescription("String to look for. When set, only matching strings are returned");

            _fluentCommandLineParser.Setup(arg => arg.LookForRegex)
            .As("lr")
            .SetDefault(string.Empty)
            .WithDescription("Regex to look for. When set, only strings matching the regex are returned");

            _fluentCommandLineParser.Setup(arg => arg.StringFile)
            .As("fs")
            .SetDefault(string.Empty)
            .WithDescription("File containing strings to look for. When set, only matching strings are returned");

            _fluentCommandLineParser.Setup(arg => arg.RegexFile)
            .As("fr")
            .SetDefault(string.Empty)
            .WithDescription(
                "File containing regex patterns to look for. When set, only strings matching regex patterns are returned\r\n");


            _fluentCommandLineParser.Setup(arg => arg.AsciiRange)
            .As("ar")
            .SetDefault("[\x20-\x7E]")
            .WithDescription(
                @"Range of characters to search for in 'Code page' strings. Specify as a range of characters in hex format and enclose in quotes. Default is [\x20 -\x7E]");

            _fluentCommandLineParser.Setup(arg => arg.UnicodeRange)
            .As("ur")
            .SetDefault("[\u0020-\u007E]")
            .WithDescription(
                "Range of characters to search for in Unicode strings. Specify as a range of characters in hex format and enclose in quotes. Default is [\\u0020-\\u007E]\r\n");

            _fluentCommandLineParser.Setup(arg => arg.CodePage)
            .As("cp")
            .SetDefault(1252)
            .WithDescription(
                "Code page to use. Default is 1252. Use the Identifier value for code pages at https://goo.gl/ig6DxW");

            _fluentCommandLineParser.Setup(arg => arg.FileMask)
            .As("mask")
            .SetDefault(string.Empty)
            .WithDescription(
                "When using -d, file mask to search for. * and ? are supported. This option has no effect when using -f");

            _fluentCommandLineParser.Setup(arg => arg.RegexOnly)
            .As("ro")
            .SetDefault(false)
            .WithDescription(
                "When true, list the string matched by regex pattern vs string the pattern was found in (This may result in duplicate strings in output. ~ denotes approx. offset)");

            _fluentCommandLineParser.Setup(arg => arg.ShowOffset)
            .As("off")
            .SetDefault(false)
            .WithDescription(
                $"Show offset to hit after string, followed by the encoding (A={_fluentCommandLineParser.Object.CodePage}, U=Unicode)\r\n");

            _fluentCommandLineParser.Setup(arg => arg.SortAlpha)
            .As("sa").SetDefault(false).WithDescription("Sort results alphabetically");

            _fluentCommandLineParser.Setup(arg => arg.SortLength)
            .As("sl").SetDefault(false).WithDescription("Sort results by length");


            var header =
                $"bstrings version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/bstrings";

            var footer = @"Examples: bstrings.exe -f ""C:\Temp\UsrClass 1.dat"" --ls URL" + "\r\n\t " +
                         @" bstrings.exe -f ""C:\Temp\someFile.txt"" --lr guid" + "\r\n\t " +
                         @" bstrings.exe -f ""C:\Temp\aBigFile.bin"" --fs c:\temp\searchStrings.txt --fr c:\temp\searchRegex.txt -s" +
                         "\r\n\t " +
                         @" bstrings.exe -d ""C:\Temp"" --mask ""*.dll""" + "\r\n\t " +
                         @" bstrings.exe -d ""C:\Temp"" --ar ""[\x20-\x37]""" + "\r\n\t " +
                         @" bstrings.exe -d ""C:\Temp"" --cp 10007" + "\r\n\t " +
                         @" bstrings.exe -d ""C:\Temp"" --ls test" + "\r\n\t " +
                         @" bstrings.exe -f ""C:\Temp\someOtherFile.txt"" --lr cc -sa" + "\r\n\t " +
                         @" bstrings.exe -f ""C:\Temp\someOtherFile.txt"" --lr cc -sa -m 15 -x 22" + "\r\n\t " +
                         @" bstrings.exe -f ""C:\Temp\UsrClass 1.dat"" --ls mui -sl" + "\r\n\t ";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (_fluentCommandLineParser.Object.GetPatterns)
            {
                _logger.Warn("Name \t\tDescription");
                foreach (var regExPattern in RegExPatterns.OrderBy(t => t.Key))
                {
                    var desc = RegExDesc[regExPattern.Key];
                    _logger.Info($"{regExPattern.Key}\t{desc}");
                }

                _logger.Info("");
                _logger.Info("To use a built in pattern, supply the Name to the --lr switch\r\n");

                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() &&
                _fluentCommandLineParser.Object.Directory.IsNullOrEmpty())
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("Either -f or -d is required. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false &&
                !File.Exists(_fluentCommandLineParser.Object.File) &&
                _fluentCommandLineParser.Object.FileMask.Length == 0)
            {
                _logger.Warn($"File '{_fluentCommandLineParser.Object.File}' not found. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.Directory.IsNullOrEmpty() == false &&
                !Directory.Exists(_fluentCommandLineParser.Object.Directory) &&
                _fluentCommandLineParser.Object.FileMask.Length == 0)
            {
                _logger.Warn($"Directory '{_fluentCommandLineParser.Object.Directory}' not found. Exiting");
                return;
            }

            if (!_fluentCommandLineParser.Object.Quiet)
            {
                _logger.Info(header);
                _logger.Info("");
            }

            var files = new List <string>();

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false)
            {
                files.Add(Path.GetFullPath(_fluentCommandLineParser.Object.File));
            }
            else
            {
                try
                {
                    if (_fluentCommandLineParser.Object.FileMask.Length > 0)
                    {
                        files.AddRange(Directory.EnumerateFiles(Path.GetFullPath(_fluentCommandLineParser.Object.Directory),
                                                                _fluentCommandLineParser.Object.FileMask, SearchOption.AllDirectories));
                    }
                    else
                    {
                        files.AddRange(Directory.EnumerateFiles(Path.GetFullPath(_fluentCommandLineParser.Object.Directory), "*",
                                                                SearchOption.AllDirectories));
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error getting files in '{_fluentCommandLineParser.Object.Directory}'. Error message: {ex.Message}");
                    return;
                }
            }

            if (!_fluentCommandLineParser.Object.Quiet)
            {
                _logger.Info($"Command line: {string.Join(" ", args)}");
                _logger.Info("");
            }

            StreamWriter sw = null;

            var    globalCounter    = 0;
            var    globalHits       = 0;
            double globalTimespan   = 0;
            var    withBoundaryHits = false;

            if (_fluentCommandLineParser.Object.SaveTo.IsNullOrEmpty() == false && _fluentCommandLineParser.Object.SaveTo.Length > 0)
            {
                _fluentCommandLineParser.Object.SaveTo = _fluentCommandLineParser.Object.SaveTo.TrimEnd('\\');

                var dir = Path.GetDirectoryName(_fluentCommandLineParser.Object.SaveTo);

                if (dir != null && Directory.Exists(dir) == false)
                {
                    try
                    {
                        Directory.CreateDirectory(dir);
                    }
                    catch (Exception)
                    {
                        _logger.Warn(
                            $"Invalid path: '{_fluentCommandLineParser.Object.SaveTo}'. Results will not be saved to a file.");
                        _logger.Info("");
                        _fluentCommandLineParser.Object.SaveTo = string.Empty;
                    }
                }
                else
                {
                    if (dir == null)
                    {
                        _logger.Warn($"Invalid path: '{_fluentCommandLineParser.Object.SaveTo}");
                        _fluentCommandLineParser.Object.SaveTo = string.Empty;
                    }
                }

                if (_fluentCommandLineParser.Object.SaveTo.Length > 0 && !_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info($"Saving hits to '{_fluentCommandLineParser.Object.SaveTo}'");
                    _logger.Info("");
                }

                if (_fluentCommandLineParser.Object.SaveTo.Length > 0)
                {
//                    try
//                    {
//                        File.Create(_fluentCommandLineParser.Object.SaveTo);
//                    }
//                    catch (Exception e)
//                    {
//                        _logger.Fatal($"Unable to create output file '{_fluentCommandLineParser.Object.SaveTo}'! Check permissions and try again! Error: {e.Message}");
//                        return;
//                    }
                    sw = new StreamWriter(_fluentCommandLineParser.Object.SaveTo, true);
                }
            }


            foreach (var file in files)
            {
                if (File.Exists(file) == false)
                {
                    _logger.Warn($"'{file}' does not exist! Skipping");
                }

                _sw = new Stopwatch();
                _sw.Start();
                var counter = 0;
                var hits    = new HashSet <string>();


                var regPattern = _fluentCommandLineParser.Object.LookForRegex;

                if (RegExPatterns.ContainsKey(_fluentCommandLineParser.Object.LookForRegex))
                {
                    regPattern = RegExPatterns[_fluentCommandLineParser.Object.LookForRegex];
                }

                if (regPattern.Length > 0 && !_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info($"Searching via RegEx pattern: {regPattern}");
                    _logger.Info("");
                }

                var minLength = 3;
                if (_fluentCommandLineParser.Object.MinimumLength > 0)
                {
                    minLength = _fluentCommandLineParser.Object.MinimumLength;
                }

                var maxLength = -1;

                if (_fluentCommandLineParser.Object.MaximumLength > minLength)
                {
                    maxLength = _fluentCommandLineParser.Object.MaximumLength;
                }

                var chunkSizeMb = _fluentCommandLineParser.Object.BlockSizeMb < 1 ||
                                  _fluentCommandLineParser.Object.BlockSizeMb > 1024
                    ? 512
                    : _fluentCommandLineParser.Object.BlockSizeMb;
                var chunkSizeBytes = chunkSizeMb * 1024 * 1024;

                var  fileSizeBytes  = new FileInfo(file).Length;
                var  bytesRemaining = fileSizeBytes;
                long offset         = 0;

                var chunkIndex  = 1;
                var totalChunks = fileSizeBytes / chunkSizeBytes + 1;
                var hsuffix     = totalChunks == 1 ? "" : "s";

                if (!_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info(
                        $"Searching {totalChunks:N0} chunk{hsuffix} ({chunkSizeMb} MB each) across {GetSizeReadable(fileSizeBytes)} in '{file}'");
                    _logger.Info("");
                }

                try
                {
                    MappedStream ms = null;

                    try
                    {
                        var fs =
                            File.Open(File.GetFileSystemEntryInfo(file).LongFullPath, FileMode.Open, FileAccess.Read,
                                      FileShare.Read, PathFormat.LongFullPath);

                        ms = MappedStream.FromStream(fs, Ownership.None);
                    }
                    catch (Exception)
                    {
                        _logger.Warn($"Unable to open file directly. This usually means the file is in use. Switching to raw access\r\n");
                    }

                    if (ms == null)
                    {
                        //raw mode
                        var ss = OpenFile(file);

                        ms = MappedStream.FromStream(ss, Ownership.None);
                    }


                    using (ms)
                    {
                        while (bytesRemaining > 0)
                        {
                            if (bytesRemaining <= chunkSizeBytes)
                            {
                                chunkSizeBytes = (int)bytesRemaining;
                            }
                            var chunk = new byte[chunkSizeBytes];

                            ms.Read(chunk, 0, chunkSizeBytes);

                            if (_fluentCommandLineParser.Object.GetUnicode)
                            {
                                var uh = GetUnicodeHits(chunk, minLength, maxLength, offset,
                                                        _fluentCommandLineParser.Object.ShowOffset);
                                foreach (var h in uh)
                                {
                                    hits.Add(h);
                                }
                            }

                            if (_fluentCommandLineParser.Object.GetAscii)
                            {
                                var ah = GetAsciiHits(chunk, minLength, maxLength, offset,
                                                      _fluentCommandLineParser.Object.ShowOffset);
                                foreach (var h in ah)
                                {
                                    hits.Add(h);
                                }
                            }

                            offset         += chunkSizeBytes;
                            bytesRemaining -= chunkSizeBytes;

                            if (!_fluentCommandLineParser.Object.Quiet)
                            {
                                _logger.Info(
                                    $"Chunk {chunkIndex:N0} of {totalChunks:N0} finished. Total strings so far: {hits.Count:N0} Elapsed time: {_sw.Elapsed.TotalSeconds:N3} seconds. Average strings/sec: {hits.Count/_sw.Elapsed.TotalSeconds:N0}");
                            }

                            chunkIndex += 1;
                        }

                        //do chunk boundary checks to make sure we get everything and not split things

                        if (!_fluentCommandLineParser.Object.Quiet)
                        {
                            _logger.Info(
                                "Primary search complete. Looking for strings across chunk boundaries...");
                        }

                        bytesRemaining = fileSizeBytes;
                        chunkSizeBytes = chunkSizeMb * 1024 * 1024;
                        offset         = chunkSizeBytes - _fluentCommandLineParser.Object.MinimumLength * 10 * 2;
                        //move starting point backwards for our starting point
                        chunkIndex = 0;

                        var boundaryChunkSize = _fluentCommandLineParser.Object.MinimumLength * 10 * 2 * 2;
                        //grab the same # of bytes on both sides of the boundary

                        while (bytesRemaining > 0)
                        {
                            if (offset + boundaryChunkSize > fileSizeBytes)
                            {
                                break;
                            }


                            var chunk = new byte[boundaryChunkSize];

                            ms.Read(chunk, 0, boundaryChunkSize);

                            if (_fluentCommandLineParser.Object.GetUnicode)
                            {
                                var uh = GetUnicodeHits(chunk, minLength, maxLength, offset,
                                                        _fluentCommandLineParser.Object.ShowOffset);
                                foreach (var h in uh)
                                {
                                    hits.Add("  " + h);
                                }

                                if (withBoundaryHits == false && uh.Count > 0)
                                {
                                    withBoundaryHits = uh.Count > 0;
                                }
                            }

                            if (_fluentCommandLineParser.Object.GetAscii)
                            {
                                var ah = GetAsciiHits(chunk, minLength, maxLength, offset,
                                                      _fluentCommandLineParser.Object.ShowOffset);
                                foreach (var h in ah)
                                {
                                    hits.Add("  " + h);
                                }

                                if (withBoundaryHits == false && ah.Count > 0)
                                {
                                    withBoundaryHits = true;
                                }
                            }

                            offset         += chunkSizeBytes;
                            bytesRemaining -= chunkSizeBytes;

                            chunkIndex += 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Info("");
                    _logger.Error($"Error: {ex.Message}");
                }
                _sw.Stop();

                if (!_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info("Search complete.");
                    _logger.Info("");
                }

                if (_fluentCommandLineParser.Object.SortAlpha)
                {
                    _logger.Info("Sorting alphabetically...");
                    _logger.Info("");
                    var tempList = hits.ToList();
                    tempList.Sort();
                    hits = new HashSet <string>(tempList);
                }
                else if (_fluentCommandLineParser.Object.SortLength)
                {
                    _logger.Info("Sorting by length...");
                    _logger.Info("");
                    var tempList = SortByLength(hits.ToList()).ToList();
                    hits = new HashSet <string>(tempList);
                }

                var fileStrings  = new HashSet <string>();
                var regexStrings = new HashSet <string>();

                //set up highlighting
                if (_fluentCommandLineParser.Object.LookForString.Length > 0)
                {
                    fileStrings.Add(_fluentCommandLineParser.Object.LookForString);
                }

                if (_fluentCommandLineParser.Object.LookForRegex.Length > 0)
                {
                    regexStrings.Add(regPattern);
                }

                if (_fluentCommandLineParser.Object.StringFile.IsNullOrEmpty() == false || _fluentCommandLineParser.Object.RegexFile.IsNullOrEmpty() == false)
                {
                    if (_fluentCommandLineParser.Object.StringFile.Length > 0)
                    {
                        if (File.Exists(_fluentCommandLineParser.Object.StringFile))
                        {
                            fileStrings.UnionWith(new HashSet <string>(File.ReadAllLines(_fluentCommandLineParser.Object.StringFile)));
                        }
                        else
                        {
                            _logger.Error($"Strings file '{_fluentCommandLineParser.Object.StringFile}' not found.");
                        }
                    }

                    if (_fluentCommandLineParser.Object.RegexFile.Length > 0)
                    {
                        if (File.Exists(_fluentCommandLineParser.Object.RegexFile))
                        {
                            regexStrings.UnionWith(new HashSet <string>(File.ReadAllLines(_fluentCommandLineParser.Object.RegexFile)));
                        }
                        else
                        {
                            _logger.Error($"Regex file '{_fluentCommandLineParser.Object.RegexFile}' not found.");
                        }
                    }
                }

                AddHighlightingRules(fileStrings.ToList());

                if (_fluentCommandLineParser.Object.RegexOnly == false)
                {
                    AddHighlightingRules(regexStrings.ToList(), true);
                }


                if (!_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info("Processing strings...");
                    _logger.Info("");
                }

                foreach (var hit in hits)
                {
                    if (hit.Length == 0)
                    {
                        continue;
                    }

                    if (fileStrings.Count > 0 || regexStrings.Count > 0)
                    {
                        foreach (var fileString in fileStrings)
                        {
                            if (fileString.Trim().Length == 0)
                            {
                                continue;
                            }
                            if (hit.IndexOf(fileString, StringComparison.InvariantCultureIgnoreCase) < 0)
                            {
                                continue;
                            }

                            counter += 1;

                            if (_fluentCommandLineParser.Object.QuietQuiet == false)
                            {
                                _logger.Info(hit);
                            }

                            sw?.WriteLine(hit);
                        }

                        var hitoffset = "";
                        if (_fluentCommandLineParser.Object.ShowOffset)
                        {
                            hitoffset = $"~{hit.Split('\t').Last()}";
                        }

                        foreach (var regString in regexStrings)
                        {
                            if (regString.Trim().Length == 0)
                            {
                                continue;
                            }

                            try
                            {
                                var reg1 = new Regex(regString,
                                                     RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                                if (reg1.IsMatch(hit) == false)
                                {
                                    continue;
                                }

                                counter += 1;

                                if (_fluentCommandLineParser.Object.RegexOnly)
                                {
                                    foreach (var match in reg1.Matches(hit))
                                    {
                                        if (_fluentCommandLineParser.Object.QuietQuiet == false)
                                        {
                                            _logger.Info($"{match}\t{hitoffset}");
                                        }

                                        sw?.WriteLine($"{match}\t{hitoffset}");
                                    }
                                }
                                else
                                {
                                    if (_fluentCommandLineParser.Object.QuietQuiet == false)
                                    {
                                        _logger.Info(hit);
                                    }

                                    sw?.WriteLine(hit);
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.Error($"Error setting up regular expression '{regString}': {ex.Message}");
                            }
                        }
                    }
                    else
                    {
                        //dump all strings
                        counter += 1;

                        if (_fluentCommandLineParser.Object.QuietQuiet == false)
                        {
                            _logger.Info(hit);
                        }

                        sw?.WriteLine(hit);
                    }
                }

                if (_fluentCommandLineParser.Object.Quiet)
                {
                    continue;
                }

                var suffix = counter == 1 ? "" : "s";

                _logger.Info("");

                if (withBoundaryHits)
                {
                    _logger.Info("** Strings prefixed with 2 spaces are hits found across chunk boundaries **");
                    _logger.Info("");
                }

                _logger.Info(
                    $"Found {counter:N0} string{suffix} in {_sw.Elapsed.TotalSeconds:N3} seconds. Average strings/sec: {hits.Count/_sw.Elapsed.TotalSeconds:N0}");
                globalCounter  += counter;
                globalHits     += hits.Count;
                globalTimespan += _sw.Elapsed.TotalSeconds;
                if (files.Count > 1)
                {
                    _logger.Info(
                        "-------------------------------------------------------------------------------------\r\n");
                }
            }
            if (sw != null)
            {
                sw.Flush();
                sw.Close();
            }

            if (!_fluentCommandLineParser.Object.Quiet && files.Count > 1)
            {
                var suffix = globalCounter == 1 ? "" : "s";

                _logger.Info("");
                _logger.Info(
                    $"Found {globalCounter:N0} string{suffix} in {globalTimespan:N3} seconds. Average strings/sec: {globalHits/globalTimespan:N0}");
            }
        }
Example #24
0
 public static bool Same(FileInfo a, FileInfo b)
 {
     return(String.Compare(a.FullName, b.FullName, true) == 0); // true->ignore case
 }
Example #25
0
      private void DumpClassAlternateDataStreamInfo(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);

         const int defaultStreamsFile = 1; // The default number of data streams for a file.

         string tempPath;
         int currentNumberofStreams;
         int newNumberofStreams;
         string reporter;
         long fileSize;

         string random = Path.GetRandomFileName();
         string myStream = "ӍƔŞtrëƛɱ-" + random;
         string myStream2 = "myStreamTWO-" + random;

         var arrayContent = new[]
         {
            "(1) The quick brown fox jumps over the lazy dog.",
            "(2) Albert Einstein: \"Science is a wonderful thing if one does not have to earn one's living at it.",
            "(3) " + TextHelloWorld + " " + TextUnicode
         };

         string stringContent = "(1) Computer: [" + LocalHost + "]" + "\tHello there, " + Environment.UserName;

         #endregion // Setup

         #region Create Stream

         tempPath = Path.GetTempPath("Class.AlternateDataStreamInfo()-file-" + Path.GetRandomFileName());
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         Console.WriteLine("\nInput File Path: [{0}]", tempPath);
         Console.WriteLine("\nA file is created and three streams are added.");

         
         // Create file and add 10 characters to it, file is created in ANSI format.
         File.WriteAllText(tempPath, TenNumbers);


         var fi = new FileInfo(tempPath);  // Class FileInfo() instance.

         currentNumberofStreams = File.EnumerateAlternateDataStreams(tempPath).Count();            

         Assert.AreEqual(defaultStreamsFile, currentNumberofStreams, "Total amount of default streams do not match.");
         Assert.AreEqual(currentNumberofStreams, File.EnumerateAlternateDataStreams(tempPath).Count(), "Total amount of File.EnumerateAlternateDataStreams() streams do not match.");
         Assert.AreEqual(currentNumberofStreams, fi.EnumerateAlternateDataStreams().Count(), "Total amount of FileInfo() streams do not match.");


         fileSize = File.GetSize(tempPath);
         Assert.AreEqual(TenNumbers.Length, fileSize);
         
         
         // Create alternate data streams.
         // Because of the colon, you must supply a full path and use PathFormat.FullPath or a NotSupportedException is thrown: path is in an invalid format.

         File.WriteAllLines(tempPath + ":" + myStream, arrayContent, PathFormat.FullPath);
         File.WriteAllText(tempPath + ":" + myStream2, stringContent, PathFormat.FullPath);

         StopWatcher(true);
         newNumberofStreams = File.EnumerateAlternateDataStreams(tempPath).Count();
         reporter = Reporter(true);

         // Enumerate all streams from the file.
         foreach (AlternateDataStreamInfo stream in fi.EnumerateAlternateDataStreams())
         {
            Assert.IsTrue(Dump(stream, -10));

            // The default stream, a file as you know it.
            if (stream.StreamName == "")
               Assert.AreEqual(fileSize, stream.Size);
         }

         Console.WriteLine("\n\n\tCurrent stream Count(): [{0}]    {1}", newNumberofStreams, reporter);

         Assert.AreEqual(newNumberofStreams, File.EnumerateAlternateDataStreams(tempPath).Count(), "Total amount of streams do not match.");

         
         // Show the contents of our streams.
         foreach (string streamName in (new[] {myStream, myStream2 }))
         {
            Console.WriteLine("\n\tStream name: [{0}]", streamName);

            // Because of the colon, you must supply a full path and use PathFormat.FullPath or a NotSupportedException is thrown: path is in an invalid format.
            foreach (var line in File.ReadAllLines(tempPath + ":" + streamName, PathFormat.FullPath))
               Console.WriteLine("\t\t{0}", line);
         }

         
         StopWatcher(true);

         #endregion // Create Stream

         File.Delete(tempPath);
         Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed.");
         Console.WriteLine();
      }
Example #26
0
 public ActionDownload(ShowItem si, ProcessedEpisode pe, FileInfo dest, string path) : this(si, pe, dest, path, false)
 {
 }
Example #27
0
        private void TransferItem(USNJournalSyncLog syncLog, SyncMountpoint syncFrom)
        {
            try
            {
                syncLog.ActionStartDate  = DateTime.Now;
                syncLog.ActionFinishDate = null;
                Singleton.Instance.Repository.Update(syncLog);

                bool successfull = false;

                if (syncLog.Action.GetType() == typeof(DeleteAction))
                {
                    var path = Path.Get(syncFrom.Path, syncLog.Action.RelativePath);

                    logger.Info($"[{syncLog.Id}] [D] " + path);

                    if (path.Exists)
                    {
                        var destinationPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toDelete", syncLog.Id).FullPath;

                        if (syncLog.Action.IsDirectory)
                        {
                            DirectoryInfo info = new DirectoryInfo(path.FullPath);
                            info.MoveTo(destinationPath);
                            info.Delete(true, true);
                        }
                        else
                        {
                            FileInfo info = new FileInfo(path.FullPath);
                            info.MoveTo(destinationPath);
                            info.Delete();
                        }
                    }


                    successfull = true;
                }

                else if (syncLog.Action.GetType() == typeof(RenameAction))
                {
                    var renameAction = syncLog.Action as RenameAction;

                    logger.Info($"[{syncLog.Id}] [R] {renameAction.RenameFrom} to {renameAction.RelativePath}");

                    if (String.IsNullOrWhiteSpace(renameAction.RenameFrom))
                    {
                        CopyFile(syncLog, syncFrom);
                    }

                    string pathFrom = Path.Get(syncFrom.Path, renameAction.RenameFrom).FullPath;
                    string tempPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toRename", syncLog.Id).FullPath;
                    string pathTo   = Path.Get(syncFrom.Path, renameAction.RelativePath).FullPath;

                    if (syncLog.Action.IsDirectory)
                    {
                        new DirectoryInfo(pathFrom).MoveTo(tempPath, MoveOptions.None);
                        new DirectoryInfo(tempPath).MoveTo(pathTo, MoveOptions.None);
                    }
                    else
                    {
                        Alphaleonis.Win32.Filesystem.File.Move(pathFrom, tempPath, MoveOptions.None);
                        Alphaleonis.Win32.Filesystem.File.Move(tempPath, pathTo, MoveOptions.None);
                        //new FileInfo(pathFrom).MoveTo(tempPath, MoveOptions.WriteThrough);
                        //new FileInfo(tempPath).MoveTo(pathTo, MoveOptions.WriteThrough);
                    }

                    successfull = true;
                }
                else
                {
                    successfull = CopyFile(syncLog, syncFrom);
                }


                syncLog.ActionFinishDate = DateTime.Now;
                syncLog.Successfull      = successfull;
                Singleton.Instance.Repository.Update(syncLog);

                foreach (var error in Singleton.Instance.Repository.Many <Error>(f => f.SyncLog.Id == syncLog.Id))
                {
                    Singleton.Instance.Repository.Delete(error);
                }
            }
            catch (FileNotFoundException ex)
            {
                syncLog.RequiresManualIntervention = true;
                syncLog.ActionFinishDate           = DateTime.Now;
                Singleton.Instance.Repository.Update(syncLog);

                logger.Error(ex, "Error on item " + syncLog.Id);
                Error error = new Error();
                error.SyncLog   = syncLog;
                error.Exception = ex;
                error.ItemId    = syncLog.Id;
                error.Message   = ex.Message;
                Singleton.Instance.Repository.Add(error);
            }
            catch (Exception e)
            {
                syncLog.ActionFinishDate = DateTime.Now;
                Singleton.Instance.Repository.Update(syncLog);

                logger.Error(e, "Error on item " + syncLog.Id);
                Error error = new Error();
                error.SyncLog   = syncLog;
                error.Exception = e;
                error.ItemId    = syncLog.Id;
                error.Message   = e.Message;
                Singleton.Instance.Repository.Add(error);
            }
        }
Example #28
0
      private void DumpFileTrailingDotSpace(bool isLocal)
      {
         Console.WriteLine("\n=== TEST {0} ===\n", isLocal ? Local : Network);
         const string characterDot = ".";
         const string characterSpace = " ";
         string random = Path.GetRandomFileName();
         string tempPathDot = Path.GetTempPath("File.Create()-" + random + "-file-with-dot-" + characterDot);
         string tempPathSpace = Path.GetTempPath("File.Create()-" + random + "-file-with-space-" + characterSpace);
         if (!isLocal) tempPathDot = Path.LocalToUnc(tempPathDot);
         if (!isLocal) tempPathSpace = Path.LocalToUnc(tempPathSpace);

         Console.WriteLine("Input File Path (with dot)  : [{0}]", tempPathDot);
         Console.WriteLine("Input File Path (with space): [{0}]", tempPathSpace);

         Assert.IsTrue(tempPathDot.EndsWith(characterDot), "Path should have a trailing dot.");
         Assert.IsTrue(tempPathSpace.EndsWith(characterSpace), "Path should have a trailing space.");

         #region Path.GetFullPath(), Path Normalization

         string sysIo = System.IO.Path.GetFullPath(tempPathDot);
         Assert.IsFalse(sysIo.EndsWith(characterDot), "Path should not have a trailing dot.");

         sysIo = System.IO.Path.GetFullPath(tempPathSpace);
         Assert.IsFalse(sysIo.EndsWith(characterSpace), "Path should not have a trailing space.");


         string alphaFs = Path.GetFullPath(tempPathDot);
         Assert.IsFalse(alphaFs.EndsWith(characterDot), "Path should not have a trailing dot.");

         alphaFs = Path.GetFullPath(tempPathSpace);
         Assert.IsFalse(alphaFs.EndsWith(characterSpace), "Path should not have a trailing space.");


         Assert.AreEqual(sysIo, alphaFs, "Paths should be the same.");

         #endregion // Path.GetFullPath(), Path Normalization

         #region Path.GetLongPath(), No Path Normalization

         alphaFs = Path.GetLongPath(tempPathDot);
         Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot.");

         alphaFs = Path.GetLongPath(tempPathSpace);
         Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space.");

         Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same.");

         #endregion // Path.GetLongPath(), No Path Normalization

         #region Path.GetRegularPath(), No Path Normalization

         alphaFs = Path.GetRegularPath(tempPathDot);
         Assert.IsTrue(alphaFs.EndsWith(characterDot), "Path should have a trailing dot.");

         alphaFs = Path.GetRegularPath(tempPathSpace);
         Assert.IsTrue(alphaFs.EndsWith(characterSpace), "Path should have a trailing space.");

         Assert.AreNotEqual(alphaFs, sysIo, "Paths should not be the same.");

         #endregion // Path.GetRegularPath(), No Path Normalization

         Console.WriteLine();

         #region File() Class

         StopWatcher(true);

         #region TrailingDot

         #region System.IO

         // tempPathDot contains a trailing dot but gets stripped on path normalization.
         // System.IO handles the file without the trailing dot. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         using (FileStream fs = System.IO.File.Create(tempPathDot)) { fs.WriteByte(1); }
         Assert.IsTrue(System.IO.File.Exists(tempPathDot), "File should exist.");
         Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS.");
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS.");

         using (StreamWriter sw = System.IO.File.AppendText(tempPathDot))
            sw.WriteLine(TextHelloWorld);

         string lineSysIo;
         using (StreamReader sr = System.IO.File.OpenText(tempPathDot))
            lineSysIo = sr.ReadToEnd();

         System.IO.File.Delete(tempPathDot);
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         using (FileStream fs = File.Create(tempPathDot, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization.
         Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should exist and visible to AlphaFS.");
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should be invisible to System.IO.");

         using (StreamWriter sw = File.AppendText(tempPathDot, PathFormat.FullPath))
            sw.WriteLine(TextHelloWorld);

         string lineAlphaFs;
         using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         File.Delete(tempPathDot, true, PathFormat.FullPath); // Delete file without path normalization.
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingDot

         #region TrailingSpace

         #region System.IO

         // tempPathSpace contains a trailing space but gets stripped on path normalization.
         // System.IO handles the file without the trailing space. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         using (FileStream fs = System.IO.File.Create(tempPathSpace)) { fs.WriteByte(1); }
         Assert.IsTrue(System.IO.File.Exists(tempPathSpace), "File should exist.");
         Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS.");
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS.");

         using (StreamWriter sw = System.IO.File.AppendText(tempPathSpace))
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathSpace))
            lineSysIo = sr.ReadToEnd();

         System.IO.File.Delete(tempPathSpace);
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         using (FileStream fs = File.Create(tempPathSpace, PathFormat.FullPath)) { fs.WriteByte(1); } // Create file without path normalization.
         Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should exist and visible to AlphaFS.");
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should be invisible to System.IO.");

         using (StreamWriter sw = File.AppendText(tempPathSpace, PathFormat.FullPath))
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         File.Delete(tempPathSpace, true, PathFormat.FullPath); // Delete file without path normalization.
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingSpace

         Console.WriteLine("\tClass File(){0}", Reporter());

         #endregion // File() Class

         #region FileInfo() Class

         StopWatcher(true);

         #region TrailingDot

         #region System.IO

         // tempPathDot contains a trailing dot but gets stripped on path normalization.
         // System.IO handles the file without the trailing dot. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         System.IO.FileInfo sysIoFi = new System.IO.FileInfo(tempPathDot);
         Assert.IsTrue(sysIoFi.Name.EndsWith(characterDot), "Path should have a trailing dot.");

         using (FileStream fs = sysIoFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(sysIoFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathDot), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = sysIoFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathDot))
            lineSysIo = sr.ReadToEnd();

         sysIoFi.Delete();
         Assert.IsFalse(System.IO.File.Exists(tempPathDot), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         FileInfo alphaFsFi = new FileInfo(tempPathDot, PathFormat.FullPath);
         Assert.IsTrue(alphaFsFi.Name.EndsWith(characterDot), "Path should have a trailing dot.");

         using (FileStream fs = alphaFsFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(alphaFsFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathDot, PathFormat.FullPath), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathDot), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = alphaFsFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathDot, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         alphaFsFi.Delete();
         alphaFsFi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(File.Exists(tempPathDot, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingDot

         #region TrailingSpace

         #region System.IO

         // tempPathSpace contains a trailing space but gets stripped on path normalization.
         // System.IO handles the file without the trailing space. Therefore, the file exists.
         // AlphaFS has the same behaviour as .NET for default methods.

         sysIoFi = new System.IO.FileInfo(tempPathSpace);
         Assert.IsTrue(sysIoFi.Name.EndsWith(characterSpace), "Path should have a trailing space.");

         using (FileStream fs = sysIoFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(sysIoFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathSpace), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = sysIoFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = System.IO.File.OpenText(tempPathSpace))
            lineSysIo = sr.ReadToEnd();

         sysIoFi.Delete();
         Assert.IsFalse(System.IO.File.Exists(tempPathSpace), "File should not exist.");

         #endregion // System.IO

         #region AlphaFS

         alphaFsFi = new FileInfo(tempPathSpace, PathFormat.FullPath);
         Assert.IsTrue(alphaFsFi.Name.EndsWith(characterSpace), "Path should have a trailing space.");

         using (FileStream fs = alphaFsFi.Create())
         {
            fs.WriteByte(100);

            Assert.IsTrue(alphaFsFi.Exists, "File should exist.");
            Assert.IsTrue(File.Exists(tempPathSpace, PathFormat.FullPath), "File should be visible to AlphaFS.");
            Assert.IsFalse(File.Exists(tempPathSpace), "File should be invisible to AlphaFS.");
         }

         using (StreamWriter sw = alphaFsFi.AppendText())
            sw.WriteLine(TextHelloWorld);

         using (StreamReader sr = File.OpenText(tempPathSpace, PathFormat.FullPath))
            lineAlphaFs = sr.ReadToEnd();

         alphaFsFi.Delete();
         alphaFsFi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(File.Exists(tempPathSpace, PathFormat.FullPath), "File should not exist.");

         #endregion // AlphaFS

         Assert.AreEqual(lineSysIo, lineAlphaFs);

         #endregion // TrailingSpace

         Console.WriteLine("\tClass FileInfo(){0}", Reporter());

         #endregion // FileInfo() Class

         Console.WriteLine();
      }
Example #29
0
 internal FileLocalArticle(FileInfo info)
     : base()
 {
     Info = info;
 }
Example #30
0
        private void InitDataBase(string alarmImagePath, string videoPath, string xmlFile)
        {
            dbConnection?.Close();
            dbConnection = null;
            // 删除旧文件
            if (File.Exists("./data.db"))
            {
                string   ex = DateTime.Now.ToString("yyyyMMddHHmmss");
                FileInfo fi = new FileInfo("./data.db");
                try
                {
                    fi.MoveTo("./data.db." + ex, MoveOptions.ReplaceExisting);
                }
                catch (IOException e)
                {
                    MessageWindow.ShowDialog(e.ToString(), this);
                    return;
                }
            }

            // 创建数据表
            CreateBlankTabs();

            /********************* DataPathTab 数据更新 ********************/
            SQLiteCommand cmd = new SQLiteCommand();

            cmd.Connection = dbConnection;

            cmd.CommandText = "BEGIN";
            cmd.ExecuteNonQuery();
            cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('AlarmImagePath', '{alarmImagePath}')");
            cmd.ExecuteNonQuery();
            cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('VideoPath', '{videoPath}')");
            cmd.ExecuteNonQuery();
            cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('VideoInfoFilePath', '{xmlFile}')");
            cmd.ExecuteNonQuery();
            cmd.CommandText = "COMMIT";
            cmd.ExecuteNonQuery();

            /********************* AlarmImageTab 数据更新 ********************/
            cmd.CommandText = "BEGIN";
            cmd.ExecuteNonQuery();
            foreach (var item in alarmImageListAll)
            {
                cmd.CommandText = string.Format($"INSERT INTO AlarmImageTab(id, image, scene, incident, video, frame, state, count)" +
                                                $" VALUES ('{item.ID}', '{item.ImagePath}','{item.Scene}','{item.Incident}', '{item.Video}', '{item.Frame}', '{(int)item.State}', '{item.IncidentCount}')");
                cmd.ExecuteNonQuery();
            }
            cmd.CommandText = "COMMIT";
            cmd.ExecuteNonQuery();

            /************************** TestVideoTab 数据更新 ************************************/
            cmd.CommandText = "BEGIN";
            cmd.ExecuteNonQuery();
            foreach (var item in testVideoList)
            {
                cmd.CommandText = string.Format($"INSERT INTO TestVideoTab(VideoName, Directory) VALUES ('{item.VideoName}', '{item.VideoPath}')");
                cmd.ExecuteNonQuery();
            }
            cmd.CommandText = "COMMIT";
            cmd.ExecuteNonQuery();

            /************************** MarkVideoTab 数据更新 ************************************/
            cmd.CommandText = "BEGIN";
            cmd.ExecuteNonQuery();
            foreach (var item in videoInfoList)
            {
                cmd.CommandText = string.Format($"INSERT INTO VideoInfoTab(Scene, Video, Incident, Count) " +
                                                $"VALUES ('{item.Scene}', '{item.VideoName}', '{item.Incident}', '{item.Count}')");
                cmd.ExecuteNonQuery();
            }
            cmd.CommandText = "COMMIT";
            cmd.ExecuteNonQuery();
        }
Example #31
0
      private void DumpGetXxxTimeXxx(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);
         string path = NotepadExe;
         if (!isLocal) path = Path.LocalToUnc(path);

         Console.WriteLine("\nInput File Path: [{0}]\n", path);

         #endregion // Setup

         StopWatcher(true);

         #region GetCreationTimeXxx

         DateTime actual = File.GetCreationTime(path);
         DateTime expected = System.IO.File.GetCreationTime(path);
         Console.WriteLine("\tGetCreationTime()     : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetCreationTimeUtc(path);
         expected = System.IO.File.GetCreationTimeUtc(path);
         Console.WriteLine("\tGetCreationTimeUtc()  : [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetCreationTimeXxx

         #region GetLastAccessTimeXxx

         actual = File.GetLastAccessTime(path);
         expected = System.IO.File.GetLastAccessTime(path);
         Console.WriteLine("\tGetLastAccessTime()   : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetLastAccessTimeUtc(path);
         expected = System.IO.File.GetLastAccessTimeUtc(path);
         Console.WriteLine("\tGetLastAccessTimeUtc(): [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetLastAccessTimeXxx

         #region GetLastWriteTimeXxx

         actual = File.GetLastWriteTime(path);
         expected = System.IO.File.GetLastWriteTime(path);
         Console.WriteLine("\tGetLastWriteTime()    : [{0}]    System.IO: [{1}]", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         actual = File.GetLastWriteTimeUtc(path);
         expected = System.IO.File.GetLastWriteTimeUtc(path);
         Console.WriteLine("\tGetLastWriteTimeUtc() : [{0}]    System.IO: [{1}]\n", actual, expected);
         Assert.AreEqual(expected, actual, "AlphaFS != System.IO");

         #endregion // GetLastWriteTimeXxx


         #region GetChangeTimeXxx

         Console.WriteLine("\tGetChangeTime()       : [{0}]    System.IO: [N/A]", File.GetChangeTime(path));
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]    System.IO: [N/A]", File.GetChangeTimeUtc(path));

         #endregion // GetChangeTimeXxx

         Console.WriteLine();
         Console.WriteLine(Reporter());
         Console.WriteLine();

         #region Trigger GetChangeTimeXxx

         // We can not compare ChangeTime against .NET because it does not exist.
         // Creating a file and renaming it triggers ChangeTime, so test for that.

         path = Path.GetTempPath("File-GetChangeTimeXxx()-file-" + Path.GetRandomFileName());
         if (!isLocal) path = Path.LocalToUnc(path);

         FileInfo fi = new FileInfo(path);
         using (fi.Create()) { }
         string fileName = fi.Name;

         DateTime lastAccessTimeActual = File.GetLastAccessTime(path);
         DateTime lastAccessTimeUtcActual = File.GetLastAccessTimeUtc(path);

         DateTime changeTimeActual = File.GetChangeTime(path);
         DateTime changeTimeUtcActual = File.GetChangeTimeUtc(path);

         Console.WriteLine("\nTesting ChangeTime on a temp file.");
         Console.WriteLine("\nInput File Path: [{0}]\n", path);
         Console.WriteLine("\tGetChangeTime()       : [{0}]\t", changeTimeActual);
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]\t", changeTimeUtcActual);

         fi.MoveTo(fi.FullName.Replace(fileName, fileName + "-Renamed"));

         // Pause for at least a second so that the difference in time can be seen.
         int sleep = new Random().Next(2000, 4000);
         Thread.Sleep(sleep);

         fi.MoveTo(fi.FullName.Replace(fileName + "-Renamed", fileName));

         DateTime lastAccessTimeExpected = File.GetLastAccessTime(path);
         DateTime lastAccessTimeUtcExpected = File.GetLastAccessTimeUtc(path);
         DateTime changeTimeExpected = File.GetChangeTime(path);
         DateTime changeTimeUtcExpected = File.GetChangeTimeUtc(path);

         Console.WriteLine("\nTrigger ChangeTime by renaming the file.");
         Console.WriteLine("For Unit Test, ChangeTime should differ approximately: [{0}] seconds.\n", sleep / 1000);
         Console.WriteLine("\tGetChangeTime()       : [{0}]\t", changeTimeExpected);
         Console.WriteLine("\tGetChangeTimeUtc()    : [{0}]\t\n", changeTimeUtcExpected);


         Assert.AreNotEqual(changeTimeActual, changeTimeExpected);
         Assert.AreNotEqual(changeTimeUtcActual, changeTimeUtcExpected);

         Assert.AreEqual(lastAccessTimeExpected, lastAccessTimeActual);
         Assert.AreEqual(lastAccessTimeUtcExpected, lastAccessTimeUtcActual);

         #endregion // Trigger GetChangeTimeXxx


         fi.Delete();
         fi.Refresh(); // Must Refresh() to get actual state.
         Assert.IsFalse(fi.Exists, "Cleanup failed: File should have been removed.");
         Console.WriteLine();
      }
Example #32
0
      private void DumpCopy(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);
         string path = isLocal ? SysRoot : Path.LocalToUnc(SysRoot);
         string tempPath = Path.GetTempPath("File-Copy-" + Path.GetRandomFileName());
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         bool exception;
         int expectedLastError;
         string expectedException;

         string fileSource = @"file-source-" + Path.GetRandomFileName() + ".exe";
         string fileDestination = @"file-destination-" + Path.GetRandomFileName() + ".exe";

         string folderSource = tempPath + @"\folder-source-" + Path.GetRandomFileName();
         string folderDestination = tempPath + @"\folder-destination-" + Path.GetRandomFileName();

         string fullPathSource = folderSource + @"\" + fileSource;
         string fullPathDestination = folderDestination + @"\" + fileDestination;
         if (!isLocal) fullPathSource = Path.LocalToUnc(fullPathSource);
         if (!isLocal) fullPathDestination = Path.LocalToUnc(fullPathDestination);

         DirectoryInfo dirInfo = new DirectoryInfo(folderDestination);

         #endregion // Setup

         try
         {
            #region UnauthorizedAccessException

            Directory.CreateDirectory(folderSource);
            Directory.CreateDirectory(folderDestination);

            DirectorySecurity dirSecurity;

            string user = (Environment.UserDomainName + @"\" + Environment.UserName).TrimStart('\\');

            // ╔═════════════╦═════════════╦═══════════════════════════════╦════════════════════════╦══════════════════╦═══════════════════════╦═════════════╦═════════════╗
            // ║             ║ folder only ║ folder, sub-folders and files ║ folder and sub-folders ║ folder and files ║ sub-folders and files ║ sub-folders ║    files    ║
            // ╠═════════════╬═════════════╬═══════════════════════════════╬════════════════════════╬══════════════════╬═══════════════════════╬═════════════╬═════════════╣
            // ║ Propagation ║ none        ║ none                          ║ none                   ║ none             ║ InheritOnly           ║ InheritOnly ║ InheritOnly ║
            // ║ Inheritance ║ none        ║ Container|Object              ║ Container              ║ Object           ║ Container|Object      ║ Container   ║ Object      ║
            // ╚═════════════╩═════════════╩═══════════════════════════════╩════════════════════════╩══════════════════╩═══════════════════════╩═════════════╩═════════════╝

            var rule = new FileSystemAccessRule(user, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Deny);

            expectedLastError = (int)Win32Errors.ERROR_ACCESS_DENIED;
            expectedException = "System.UnauthorizedAccessException";
            exception = false;
            try
            {
               Console.WriteLine("\nCatch: [{0}]: The caller does not have the required permission.", expectedException);

               FileInfo fileInfo = new FileInfo(fullPathSource);
               using (StreamWriter sw = fileInfo.CreateText())
                  sw.WriteLine("MoveTo-TestFile");

               // Set DENY for current user.
               dirSecurity = dirInfo.GetAccessControl();
               dirSecurity.AddAccessRule(rule);
               dirInfo.SetAccessControl(dirSecurity);

               fileInfo.CopyTo(fullPathDestination);
            }
            catch (UnauthorizedAccessException ex)
            {
               Assert.IsTrue(ex.Message.StartsWith("(" + expectedLastError + ")"), string.Format("Expected Win32Exception error is: [{0}]", expectedLastError));

               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            catch (Exception ex)
            {
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            finally
            {
               // Remove DENY for current user.
               dirSecurity = dirInfo.GetAccessControl();
               dirSecurity.RemoveAccessRule(rule);
               dirInfo.SetAccessControl(dirSecurity, AccessControlSections.Access);

               Directory.Delete(tempPath, true, true);
            }
            Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
            Console.WriteLine();

            #endregion // UnauthorizedAccessException

            #region FileNotFoundException

            expectedLastError = (int)Win32Errors.ERROR_FILE_NOT_FOUND;
            expectedException = "System.IO.FileNotFoundException";
            exception = false;
            try
            {
               Console.WriteLine("\nCatch: [{0}]: sourceFileName was not found.", expectedException);
               
               File.Copy(isLocal ? fileSource : Path.LocalToUnc(fileSource), isLocal ? fileDestination : Path.LocalToUnc(fileDestination));
            }
            catch (FileNotFoundException ex)
            {
               var win32Error = new Win32Exception("", ex);
               Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));

               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            catch (Exception ex)
            {
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
            Console.WriteLine();

            #endregion // FileNotFoundException

            #region DirectoryNotFoundException

            expectedLastError = (int)Win32Errors.ERROR_PATH_NOT_FOUND;
            expectedException = "System.IO.DirectoryNotFoundException";
            exception = false;
            try
            {
               Console.WriteLine("\nCatch: [{0}]: The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).", expectedException);
               
               File.Copy(fullPathSource, fullPathDestination);
            }
            catch (DirectoryNotFoundException ex)
            {
               var win32Error = new Win32Exception("", ex);
               Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));

               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            catch (Exception ex)
            {
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));               
            }
            Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
            Console.WriteLine();

            #endregion // DirectoryNotFoundException

            #region IOException #1 (AlreadyExistsException)

            Directory.CreateDirectory(folderSource);
            Directory.CreateDirectory(folderDestination);
            using (File.Create(fullPathSource)) { }
            using (File.Create(fullPathDestination)) { }

            expectedLastError = (int)Win32Errors.ERROR_FILE_EXISTS;
            expectedException = "System.IO.IOException";
            exception = false;
            try
            {
               Console.WriteLine("\nCatch: [{0}]: destFileName exists and overwrite is false.", expectedException);

               File.Copy(fullPathSource, fullPathDestination);
            }
            catch (AlreadyExistsException ex)
            {
               var win32Error = new Win32Exception("", ex);
               Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));

               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            catch (Exception ex)
            {
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
            Console.WriteLine();

            Directory.Delete(folderSource, true);
            Directory.Delete(folderDestination, true);

            #endregion // IOException #1 (AlreadyExistsException)

            #region IOException #2

            string folderfileName = null;

            expectedLastError = (int)Win32Errors.ERROR_ACCESS_DENIED;
            expectedException = "System.IO.IOException";
            exception = false;
            try
            {
               Console.WriteLine("\nCatch: [{0}]: A folder with the same name as the file exists.", expectedException);
               foreach (string file in Directory.EnumerateFiles(path))
               {
                  string newFile = Path.Combine(tempPath, Path.GetFileName(file, true));
                  folderfileName = newFile;

                  // Trigger the Exception.
                  Directory.CreateDirectory(folderfileName);

                  // true: overwrite existing.
                  File.Copy(file, folderfileName, true);
               }
            }
            catch (AlreadyExistsException ex)
            {
               var win32Error = new Win32Exception("", ex);
               Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));

               exception = true;
               Directory.Delete(folderfileName);
               Console.WriteLine("\n\t[{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            catch (Exception ex)
            {
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
            Console.WriteLine();

            #endregion // IOException #2

            #region Copy

            Console.WriteLine("\nInput Directory Path: [{0}]\n", path);
            int cnt = 0;
            string readOnlySource = null;
            string readOnlyDestination = null;

            StopWatcher(true);
            foreach (string file in Directory.EnumerateFiles(path))
            {
               string newFile = Path.Combine(tempPath, Path.GetFileName(file, true));
               File.Copy(file, newFile);

               // Set the first file as read-only to trigger an Exception when copying again.
               if (cnt == 0)
               {
                  File.SetAttributes(newFile, FileAttributes.ReadOnly);
                  readOnlySource = file;
                  readOnlyDestination = newFile;
               }

               Console.WriteLine("\t#{0:000}\tCopied to: [{1}]", ++cnt, newFile);
               Assert.IsTrue(File.Exists(newFile));
            }
            Console.WriteLine("\n\tTotal Size: [{0}]{1}", Utils.UnitSizeToText(Directory.GetProperties(tempPath)["Size"]), Reporter());
            Console.WriteLine();

            #endregion // Copy

            #region Preserve Timestamps

            // Test preservation of timestamps.
            int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
            DateTime creationTime = new DateTime(new Random(seed).Next(1971, 2071), new Random(seed).Next(1, 12), new Random(seed).Next(1, 28), new Random(seed).Next(0, 23), new Random(seed).Next(0, 59), new Random(seed).Next(0, 59));
            seed += (int)DateTime.Now.Ticks & 0x0000FFFF;
            DateTime lastAccessTime = new DateTime(new Random(seed).Next(1971, 2071), new Random(seed).Next(1, 12), new Random(seed).Next(1, 28), new Random(seed).Next(0, 23), new Random(seed).Next(0, 59), new Random(seed).Next(0, 59));
            seed += (int)DateTime.Now.Ticks & 0x0000FFFF;
            DateTime lastWriteTime = new DateTime(new Random(seed).Next(1971, 2071), new Random(seed).Next(1, 12), new Random(seed).Next(1, 28), new Random(seed).Next(0, 23), new Random(seed).Next(0, 59), new Random(seed).Next(0, 59));

            string preservePath = Path.Combine(tempPath, "PreserveTimestamps");
            string preserveFile = Path.GetFileName(readOnlySource);
            string preserveReadOnlySource = Path.Combine(preservePath, preserveFile);

            Directory.CreateDirectory(preservePath);
            File.Copy(readOnlySource, preserveReadOnlySource);

            File.SetCreationTime(preserveReadOnlySource, creationTime);
            File.SetLastAccessTime(preserveReadOnlySource, lastAccessTime);
            File.SetLastWriteTime(preserveReadOnlySource, lastWriteTime);
            
            StopWatcher(true);

            // 3rd parameter CopyOptions.None: overwrite existing.
            // 4rd parameter true: preserve timestamps of source.
            File.Copy(preserveReadOnlySource, readOnlyDestination, CopyOptions.None, true);

            Console.WriteLine("\tFile copied.{0}", Reporter());

            Assert.IsTrue(File.Exists(preserveReadOnlySource));
            Assert.IsTrue(File.Exists(readOnlyDestination));

            Assert.AreEqual(File.GetCreationTime(readOnlyDestination), creationTime, "File CreationTime should match.");
            Assert.AreEqual(File.GetLastAccessTime(readOnlyDestination), lastAccessTime, "File LastAccessTime should match.");
            Assert.AreEqual(File.GetLastWriteTime(readOnlyDestination), lastWriteTime, "File LastWriteTime should match.");
            Console.WriteLine("\nTimestamps are transferred.");

            #endregion Preserve Timestamps
         }
         finally
         {
            if (Directory.Exists(tempPath))
            {
               Directory.Delete(tempPath, true, true);
               Assert.IsFalse(Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed.");
            }
            Console.WriteLine();
         }
      }
Example #33
0
        /// <summary>
        /// The user may already have some files in the OutputFolder. If so we can go through these and
        /// figure out which need to be updated, deleted, or left alone
        /// </summary>
        public async Task OptimizeModlist()
        {
            Utils.Log("Optimizing ModList directives");

            // Clone the ModList so our changes don't modify the original data
            ModList = ModList.Clone();

            var indexed = ModList.Directives.ToDictionary(d => d.To);

            UpdateTracker.NextStep("Looking for files to delete");
            await Directory.EnumerateFiles(OutputFolder, "*", DirectoryEnumerationOptions.Recursive)
            .PMap(Queue, UpdateTracker, f =>
            {
                var relative_to = f.RelativeTo(OutputFolder);
                Utils.Status($"Checking if ModList file {relative_to}");
                if (indexed.ContainsKey(relative_to) || f.IsInPath(DownloadFolder))
                {
                    return;
                }

                Utils.Log($"Deleting {relative_to} it's not part of this ModList");
                File.Delete(f);
            });

            UpdateTracker.NextStep("Looking for unmodified files");
            (await indexed.Values.PMap(Queue, UpdateTracker, d =>
            {
                // Bit backwards, but we want to return null for
                // all files we *want* installed. We return the files
                // to remove from the install list.
                Status($"Optimizing {d.To}");
                var path = Path.Combine(OutputFolder, d.To);
                if (!File.Exists(path))
                {
                    return(null);
                }

                var fi = new FileInfo(path);
                if (fi.Length != d.Size)
                {
                    return(null);
                }

                return(path.FileHash() == d.Hash ? d : null);
            }))
            .Where(d => d != null)
            .Do(d => indexed.Remove(d.To));

            Utils.Log("Cleaning empty folders");
            var expectedFolders = indexed.Keys
                                  // We ignore the last part of the path, so we need a dummy file name
                                  .Append(Path.Combine(DownloadFolder, "_"))
                                  .SelectMany(path =>
            {
                // Get all the folders and all the folder parents
                // so for foo\bar\baz\qux.txt this emits ["foo", "foo\\bar", "foo\\bar\\baz"]
                var split = path.Split('\\');
                return(Enumerable.Range(1, split.Length - 1).Select(t => string.Join("\\", split.Take(t))));
            }).Distinct()
                                  .Select(p => Path.Combine(OutputFolder, p))
                                  .ToHashSet();

            try
            {
                Directory.EnumerateDirectories(OutputFolder, DirectoryEnumerationOptions.Recursive)
                .Where(p => !expectedFolders.Contains(p))
                .OrderByDescending(p => p.Length)
                .Do(Utils.DeleteDirectory);
            }
            catch (Exception)
            {
                // ignored because it's not worth throwing a fit over
                Utils.Log("Error when trying to clean empty folders. This doesn't really matter.");
            }

            UpdateTracker.NextStep("Updating ModList");
            Utils.Log($"Optimized {ModList.Directives.Count} directives to {indexed.Count} required");
            var requiredArchives = indexed.Values.OfType <FromArchive>()
                                   .GroupBy(d => d.ArchiveHashPath[0])
                                   .Select(d => d.Key)
                                   .ToHashSet();

            ModList.Archives   = ModList.Archives.Where(a => requiredArchives.Contains(a.Hash)).ToList();
            ModList.Directives = indexed.Values.ToList();
        }
Example #34
0
 public ActionPyTivoMeta(FileInfo nfo, ProcessedEpisode pe)
 {
     this.Episode = pe;
     this.Where   = nfo;
 }
        private void File_EnumerateAlternateDataStreams(bool isNetwork)
        {
            UnitTestConstants.PrintUnitTestHeader(isNetwork);

            var tempPath = Path.GetTempPath("File-EnumerateAlternateDataStreams-" + Path.GetRandomFileName());

            if (isNetwork)
            {
                tempPath = Path.LocalToUnc(tempPath);
            }

            const int defaultStreamsFile = 1; // The default number of data streams for a file.

            Console.WriteLine("\nInput File Path: [{0}]", tempPath);
            Console.WriteLine("\nA file is created and {0} streams are added.", UnitTestConstants.AllStreams.Count());


            try
            {
                // Create file and add 10 characters to it, file is created in ANSI format.
                File.WriteAllText(tempPath, UnitTestConstants.TenNumbers);


                var fi = new FileInfo(tempPath);

                var currentNumberofStreams = fi.EnumerateAlternateDataStreams().Count();

                Assert.AreEqual(defaultStreamsFile, currentNumberofStreams, "Total amount of default streams do not match.");
                Assert.AreEqual(currentNumberofStreams, File.EnumerateAlternateDataStreams(tempPath).Count(), "Total amount of File.EnumerateAlternateDataStreams() streams do not match.");
                Assert.AreEqual(currentNumberofStreams, fi.EnumerateAlternateDataStreams().Count(), "Total amount of FileInfo() streams do not match.");


                var fileSize = File.GetSize(tempPath);
                Assert.AreEqual(UnitTestConstants.TenNumbers.Length, fileSize);


                // Create alternate data streams.
                // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                File.WriteAllLines(tempPath + Path.StreamSeparator + UnitTestConstants.MyStream, UnitTestConstants.StreamArrayContent, PathFormat.FullPath);
                File.WriteAllText(tempPath + Path.StreamSeparator + UnitTestConstants.MyStream2, UnitTestConstants.StreamStringContent, PathFormat.FullPath);


                var newNumberofStreams = File.EnumerateAlternateDataStreams(tempPath).Count();
                Console.WriteLine("\n\nCurrent stream Count(): [{0}]", newNumberofStreams);


                // Enumerate all streams from the file.
                foreach (var stream in fi.EnumerateAlternateDataStreams())
                {
                    Assert.IsTrue(UnitTestConstants.Dump(stream, -10));

                    // The default stream, a file as we know it.
                    if (Alphaleonis.Utils.IsNullOrWhiteSpace(stream.StreamName))
                    {
                        Assert.AreEqual(fileSize, stream.Size);
                    }
                }


                // Show the contents of our streams.
                Console.WriteLine();
                foreach (var streamName in UnitTestConstants.AllStreams)
                {
                    Console.WriteLine("\n\tStream name: [{0}]", streamName);

                    // Because of the colon, you must supply a full path and use PathFormat.FullPath or PathFormat.LongFullPath,
                    // to prevent a: "NotSupportedException: path is in an invalid format." exception.

                    foreach (var line in File.ReadAllLines(tempPath + Path.StreamSeparator + streamName, PathFormat.FullPath))
                    {
                        Console.WriteLine("\t\t{0}", line);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
                Assert.IsTrue(false);
            }
            finally
            {
                File.Delete(tempPath);
                Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed.");
            }

            Console.WriteLine();
        }
Example #36
0
 public ActionMede8erXML(FileInfo nfo, ProcessedEpisode pe)
 {
     this.SI      = null;
     this.Episode = pe;
     this.Where   = nfo;
 }
Example #37
0
      private static void CompareFileInfos(System.IO.FileInfo expected, FileInfo actual)
      {
         if (expected == null || actual == null)
            Assert.AreEqual(expected, actual, "Mismatch");

         Dump(expected, -17);
         Dump(actual, -17);


         int errorCnt = 0;
         int cnt = -1;
         while (cnt != 15)
         {
            cnt++;
            try
            {
               // Compare values of both instances.
               switch (cnt)
               {
                  case 0:
                     Assert.AreEqual(expected.Attributes, actual.Attributes, "Attributes AlphaFS != System.IO");
                     break;
                  case 1:
                     Assert.AreEqual(expected.CreationTime, actual.CreationTime, "CreationTime AlphaFS != System.IO");
                     break;
                  case 2:
                     Assert.AreEqual(expected.CreationTimeUtc, actual.CreationTimeUtc, "CreationTimeUtc AlphaFS != System.IO");
                     break;

                  // Need .ToString() here since the object types are obviously not the same.
                  case 3:
                     Assert.AreEqual(expected.Directory.ToString(), actual.Directory.ToString(), "Directory AlphaFS != System.IO");
                     break;

                  case 4:
                     Assert.AreEqual(expected.DirectoryName, actual.DirectoryName, "DirectoryName AlphaFS != System.IO");
                     break;
                  case 5:
                     Assert.AreEqual(expected.Exists, actual.Exists, "Exists AlphaFS != System.IO");
                     break;
                  case 6:
                     Assert.AreEqual(expected.Extension, actual.Extension, "Extension AlphaFS != System.IO");
                     break;
                  case 7:
                     Assert.AreEqual(expected.FullName, actual.FullName, "FullName AlphaFS != System.IO");
                     break;
                  case 8:
                     Assert.AreEqual(expected.IsReadOnly, actual.IsReadOnly, "IsReadOnly AlphaFS != System.IO");
                     break;
                  case 9:
                     Assert.AreEqual(expected.LastAccessTime, actual.LastAccessTime, "LastAccessTime AlphaFS != System.IO");
                     break;
                  case 10:
                     Assert.AreEqual(expected.LastAccessTimeUtc, actual.LastAccessTimeUtc, "LastAccessTimeUtc AlphaFS != System.IO");
                     break;
                  case 11:
                     Assert.AreEqual(expected.LastWriteTime, actual.LastWriteTime, "LastWriteTime AlphaFS != System.IO");
                     break;
                  case 12:
                     Assert.AreEqual(expected.LastWriteTimeUtc, actual.LastWriteTimeUtc, "LastWriteTimeUtc AlphaFS != System.IO");
                     break;
                  case 13:
                     Assert.AreEqual(expected.Length, actual.Length, "Length AlphaFS != System.IO");
                     break;
                  case 14:
                     Assert.AreEqual(expected.Name, actual.Name, "Name AlphaFS != System.IO");
                     break;
               }
            }
            catch (Exception ex)
            {
               errorCnt++;
               Console.WriteLine("\n\t\t\tProperty cnt #{0}\tCaught Exception: [{1}]", (cnt + 1), ex.Message.Replace(Environment.NewLine, "  "));
            }
         }

         //Assert.IsTrue(errorCnt == 0, "\tEncountered: [{0}] FileInfo Properties where AlphaFS != System.IO", errorCnt);
         Console.WriteLine();
      }
Example #38
0
      public void SetAccessControl()
      {
         Console.WriteLine("File.SetAccessControl()");

         if (!IsAdmin())
            Assert.Fail();

         string path = SysDrive + @"\AlphaFile-" + Path.GetRandomFileName();
         string pathAlpha = path;

         Console.WriteLine("\n\tFile: [{0}]", path);

         try
         {
            using (File.Create(pathAlpha))
            {
            }

            // Initial read.
            Console.WriteLine("\n\tInitial read.");
            FileSecurity dsSystem = System.IO.File.GetAccessControl(path, AccessControlSections.Access);
            FileSecurity dsAlpha = File.GetAccessControl(pathAlpha, AccessControlSections.Access);
            AuthorizationRuleCollection accessRulesSystem = dsSystem.GetAccessRules(true, true, typeof(NTAccount));
            AuthorizationRuleCollection accessRulesAlpha = dsAlpha.GetAccessRules(true, true, typeof(NTAccount));
            Console.WriteLine("\t    System.IO.File.GetAccessControl() rules found: [{0}]", accessRulesSystem.Count);
            Console.WriteLine("\t\t\t   File.GetAccessControl() rules found: [{0}]", accessRulesAlpha.Count);
            Assert.AreEqual(accessRulesSystem.Count, accessRulesAlpha.Count);

            // Sanity check.
            DumpAccessRules(1, dsSystem, dsAlpha);

            // Remove inherited properties.
            // Passing true for first parameter protects the new permission from inheritance, and second parameter removes the existing inherited permissions 
            Console.WriteLine("\n\tRemove inherited properties and persist it.");
            dsAlpha.SetAccessRuleProtection(true, false);
            File.SetAccessControl(pathAlpha, dsAlpha, AccessControlSections.Access);

            // Re-read, using instance methods.
            System.IO.FileInfo fiSystem = new System.IO.FileInfo(Path.LocalToUnc(path));
            FileInfo fiAlpha = new FileInfo(Path.LocalToUnc(path));

            dsSystem = fiSystem.GetAccessControl(AccessControlSections.Access);
            dsAlpha = fiAlpha.GetAccessControl(AccessControlSections.Access);

            // Sanity check.
            DumpAccessRules(2, dsSystem, dsAlpha);

            // Restore inherited properties.
            Console.WriteLine("\n\tRestore inherited properties and persist it.");
            dsAlpha.SetAccessRuleProtection(false, true);
            File.SetAccessControl(pathAlpha, dsAlpha, AccessControlSections.Access);

            // Re-read.
            dsSystem = System.IO.File.GetAccessControl(path, AccessControlSections.Access);
            dsAlpha = File.GetAccessControl(pathAlpha, AccessControlSections.Access);

            // Sanity check.
            DumpAccessRules(3, dsSystem, dsAlpha);

            fiSystem.Delete();
            fiSystem.Refresh(); // Must Refresh() to get actual state.

            fiAlpha.Delete();
            fiAlpha.Refresh(); // Must Refresh() to get actual state.
            Assert.IsFalse(fiAlpha.Exists);
         }
         catch (Exception ex)
         {
            Console.WriteLine("\nCaught Exception: [{0}]\n", ex.Message.Replace(Environment.NewLine, "  "));
         }
      }
Example #39
0
      private static void DumpClassFileInfo(bool isLocal)
      {
         #region Setup

         Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network);
         string tempPath = Path.Combine(Path.GetTempPath(), "FileInfo()-" + Path.GetRandomFileName());
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         int expectedLastError;
         string expectedException;

         string nonExistingFile = SysRoot32 + @"\NonExistingFile-" + Path.GetRandomFileName();
         if (!isLocal) nonExistingFile = Path.LocalToUnc(nonExistingFile);

         string sysDrive = SysDrive;
         if (!isLocal) sysDrive = Path.LocalToUnc(sysDrive);

         string sysRoot = SysRoot;
         if (!isLocal) sysRoot = Path.LocalToUnc(sysRoot);

         string letter = DriveInfo.GetFreeDriveLetter() + @":\";
         if (!isLocal) letter = Path.LocalToUnc(letter);

         #endregion // Setup

         #region NotSupportedException

         expectedLastError = (int) (isLocal ? Win32Errors.ERROR_ENVVAR_NOT_FOUND : Win32Errors.NERR_UseNotFound);
         expectedException = "System.NotSupportedException";
         bool exception = false;
         try
         {
            Console.WriteLine("\nCatch: [{0}]: The given path's format is not supported.", expectedException);

            string invalidPath = SysDrive + @"\:a";
            if (!isLocal) invalidPath = Path.LocalToUnc(invalidPath) + @":a";

            FileInfo fi = new FileInfo(invalidPath);
         }
         catch (Exception ex)
         {
            // Not reliable.
            //var win32Error = new Win32Exception("", ex);
            //Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));
            //Assert.IsTrue(ex.Message.StartsWith("(" + expectedLastError + ")"), string.Format("Expected Win32Exception error is: [{0}]", expectedLastError));

            string exceptionTypeName = ex.GetType().FullName;
            if (exceptionTypeName.Equals(expectedException))
            {
               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            else
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
         }
         Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
         Console.WriteLine();

         #endregion // NotSupportedException

         #region Length Property

         #region FileNotFoundException #1

         expectedLastError = (int) Win32Errors.ERROR_FILE_NOT_FOUND;
         expectedException = "System.IO.FileNotFoundException";
         exception = false;
         try
         {
            Console.WriteLine("\nCatch: [{0}]: Length property is called, the file does not exist.", expectedException);
            Console.WriteLine(new FileInfo(nonExistingFile).Length);
         }
         catch (Exception ex)
         {
            var win32Error = new Win32Exception("", ex);
            Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));
            Assert.IsTrue(ex.Message.StartsWith("(" + expectedLastError + ")"), string.Format("Expected Win32Exception error is: [{0}]", expectedLastError));

            string exceptionTypeName = ex.GetType().FullName;
            if (exceptionTypeName.Equals(expectedException))
            {
               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            else
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
         }
         Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
         Console.WriteLine();

         #endregion // FileNotFoundException #1

         #region FileNotFoundException #2

         expectedLastError = (int) (isLocal ? Win32Errors.ERROR_PATH_NOT_FOUND : Win32Errors.ERROR_BAD_NET_NAME);
         expectedException = isLocal ? "System.IO.FileNotFoundException" : "System.IO.IOException";
         exception = false;
         try
         {
            Console.WriteLine("\nCatch: [{0}]: Length property is called, the file does not exist (Unmapped drive).", expectedException);

            Console.WriteLine(new FileInfo(nonExistingFile.Replace(sysDrive + @"\", letter)).Length);
         }
         catch (Exception ex)
         {
            var win32Error = new Win32Exception("", ex);
            Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));
            //Assert.IsTrue(ex.Message.StartsWith("(" + expectedLastError + ")"), string.Format("Expected Win32Exception error is: [{0}]", expectedLastError));

            string exceptionTypeName = ex.GetType().FullName;
            if (exceptionTypeName.Equals(expectedException))
            {
               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            else
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
         }
         Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
         Console.WriteLine();

         #endregion // FileNotFoundException #1
         
         #region FileNotFoundException #3

         expectedLastError = (int) Win32Errors.ERROR_FILE_NOT_FOUND;
         expectedException = "System.IO.FileNotFoundException";
         exception = false;
         try
         {
            Console.WriteLine("\nCatch: [{0}]: Length property is called, the file is a directory.", expectedException);
            Console.WriteLine(new FileInfo(sysRoot).Length);
         }
         catch (Exception ex)
         {
            // win32Error is always 0
            //var win32Error = new Win32Exception("", ex);
            //Assert.IsTrue(win32Error.NativeErrorCode == expectedLastError, string.Format("Expected Win32Exception error should be: [{0}], got: [{1}]", expectedLastError, win32Error.NativeErrorCode));
            Assert.IsTrue(ex.Message.StartsWith("(" + expectedLastError + ")"), string.Format("Expected Win32Exception error is: [{0}]", expectedLastError));

            string exceptionTypeName = ex.GetType().FullName;
            if (exceptionTypeName.Equals(expectedException))
            {
               exception = true;
               Console.WriteLine("\n\t[{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            else
               Console.WriteLine("\n\tCaught Unexpected Exception: [{0}]: [{1}]", exceptionTypeName, ex.Message.Replace(Environment.NewLine, "  "));
         }
         Assert.IsTrue(exception, "[{0}] should have been caught.", expectedException);
         Console.WriteLine();

         #endregion // FileNotFoundException #3

         #endregion // Length Property

         
         #region Current Directory

         tempPath = Path.CurrentDirectoryPrefix;
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         Console.WriteLine("\nInput File Path (Current directory): [{0}]\n", tempPath);

         StopWatcher(true);
         System.IO.FileInfo expected = new System.IO.FileInfo(tempPath);
         Console.WriteLine("\tSystem.IO FileInfo(){0}", Reporter());

         StopWatcher(true);
         FileInfo actual = new FileInfo(tempPath);
         Console.WriteLine("\tAlphaFS FileInfo(){0}", Reporter());

         // Compare values of both instances.
         CompareFileInfos(expected, actual);

         #endregion // Current Directory

         #region Non-Existing File

         Console.WriteLine("\nInput File Path: [{0}]\n", nonExistingFile);

         StopWatcher(true);
         expected = new System.IO.FileInfo(nonExistingFile);
         Console.WriteLine("\tSystem.IO FileInfo(){0}", Reporter());

         StopWatcher(true);
         actual = new FileInfo(nonExistingFile);
         Console.WriteLine("\tAlphaFS FileInfo(){0}", Reporter());

         // Compare values of both instances.
         CompareFileInfos(expected, actual);

         #endregion // Non-Existing File

         #region Existing File

         tempPath = Path.Combine(Path.GetTempPath(), "FileInfo()-File-" + Path.GetRandomFileName());
         if (!isLocal) tempPath = Path.LocalToUnc(tempPath);

         try
         {
            using (File.Create(tempPath)) {}

            Console.WriteLine("\nInput File Path: [{0}]\n", tempPath);

            StopWatcher(true);
            expected = new System.IO.FileInfo(tempPath);
            Console.WriteLine("\tSystem.IO FileInfo(){0}", Reporter());

            StopWatcher(true);
            actual = new FileInfo(tempPath);
            Console.WriteLine("\tAlphaFS FileInfo(){0}", Reporter());

            // Compare values of both instances.
            CompareFileInfos(expected, actual);
         }
         finally
         {
            File.Delete(tempPath);
            Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed.");
         }

         #endregion // Existing File

         #region Method .ToString()

         Console.WriteLine("\nMethod .ToString()");
         Console.WriteLine("Both strings should be the same.\n");

         expected = new System.IO.FileInfo("ToString()-TestFile");
         actual = new FileInfo("ToString()-TestFile");

         string expectedToString = expected.ToString();
         string actualToString = actual.ToString();

         Console.WriteLine("\tSystem.IO: [{0}]", expectedToString);
         Console.WriteLine("\tAlphaFS  : [{0}]", actualToString);

         Assert.AreEqual(expectedToString, actualToString, false);
         Console.WriteLine();

         #endregion Method .ToString()
      }
Example #40
0
        public override void Check(SetProgressDelegate prog, int startpct, int totPct)
        {
            if (String.IsNullOrEmpty(TVSettings.Instance.SABAPIKey) || String.IsNullOrEmpty(TVSettings.Instance.SABHostPort))
            {
                prog.Invoke(startpct + totPct);
                return;
            }

            // get list of files being downloaded by SABnzbd

            // Something like:
            // http://localhost:8080/sabnzbd/api?mode=queue&apikey=xxx&start=0&limit=8888&output=xml
            String theURL = "http://" + TVSettings.Instance.SABHostPort +
                            "/sabnzbd/api?mode=queue&start=0&limit=8888&output=xml&apikey=" + TVSettings.Instance.SABAPIKey;

            WebClient wc = new WebClient();

            byte[] r = null;
            try
            {
                r = wc.DownloadData(theURL);
            }
            catch (WebException)
            {
            }

            if (r == null)
            {
                prog.Invoke(startpct + totPct);
                return;
            }

            try
            {
                SAB.result res = SAB.result.Deserialize(r);
                if (res != null && res.status == "False")
                {
                    logger.Error("Error processing data from SABnzbd (Queue Check): {0}", res.error);
                    prog.Invoke(startpct + totPct);
                    return;
                }
            }
            catch
            {
                // wasn't a result/error combo.  this is good!
            }

            SAB.queue sq = null;
            try
            {
                sq = SAB.queue.Deserialize(r);
            }
            catch (Exception e)
            {
                logger.Error(e, "Error processing data from SABnzbd (Queue Check)");
                prog.Invoke(startpct + totPct);
                return;
            }

            System.Diagnostics.Debug.Assert(sq != null);                // shouldn't happen
            if (sq == null || sq.slots == null || sq.slots.Length == 0) // empty queue
            {
                return;
            }

            ItemList newList  = new ItemList();
            ItemList toRemove = new ItemList();
            int      c        = this.TheActionList.Count + 2;
            int      n        = 1;

            foreach (Item Action1 in this.TheActionList)
            {
                if (this.ActionCancel)
                {
                    return;
                }

                n++;
                prog.Invoke(startpct + totPct * n / c);

                if (!(Action1 is ItemMissing))
                {
                    continue;
                }

                ItemMissing Action = (ItemMissing)(Action1);

                string showname = Helpers.SimplifyName(Action.Episode.SI.ShowName);

                foreach (SAB.queueSlotsSlot te in sq.slots)
                {
                    //foreach (queueSlotsSlot te in qs)
                    {
                        FileInfo file = new FileInfo(te.filename);
                        //if (!TVSettings.Instance.UsefulExtension(file.Extension, false)) // not a usefile file extension
                        //    continue;

                        if (FileHelper.SimplifyAndCheckFilename(file.FullName, showname, true, false))
                        {
                            int seasF;
                            int epF;
                            if (TVDoc.FindSeasEp(file, out seasF, out epF, Action.Episode.SI) &&
                                (seasF == Action.Episode.SeasonNumber) && (epF == Action.Episode.EpNum))
                            {
                                toRemove.Add(Action1);
                                newList.Add(new ItemSABnzbd(te, Action.Episode, Action.TheFileNoExt));
                                break;
                            }
                        }
                    }
                }
            }

            foreach (Item i in toRemove)
            {
                this.TheActionList.Remove(i);
            }

            foreach (Item Action in newList)
            {
                this.TheActionList.Add(Action);
            }

            prog.Invoke(startpct + totPct);
        }
Example #41
0
        public NTBackupFile(string fullName)
        {
            if (fullName == null)
            {
                throw new Exception("NTBackupFileXP(byname): NULL name");
            }
            //Console.WriteLine ("NTBackupFile(byname): raw name="+fullName);

            Alphaleonis.Win32.Filesystem.FileInfo fsi = new Alphaleonis.Win32.Filesystem.FileInfo(fullName);
            fsi.Refresh();

            fseInfo          = fsi.SystemInfo;
            fseInfo.FullPath = fullName;
            this.Name        = fsi.Name;
            fileName         = fullName;
            GetHandleInfos();             // gets ID and sparse attribute.
            //this.fileSize = fileI.Length;
            this.FileStartPos  = 0;
            this.ChunkStartPos = 0;
            this.Kind          = GetKind(fseInfo);

            // GetSize is more precise (though not yet 100%) but slower. As we will generally work on snapshot,
            // don't be so obsessed with getting real size and reporting sizes changes during backup on NT.
            //GetSize ();
            this.FileSize = fseInfo.FileSize;

            if (this.Kind == FileType.Symlink)
            {
                this.TargetName = fsi.SystemInfo.VirtualFullPath;
            }

            if (this.Kind != FileType.Directory || this.Kind != FileType.Unsupported)
            {
                this.LastModifiedTime = Utilities.Utils.GetUtcUnixTime(fsi.LastWriteTime);                // fsi.LastWriteTime.ToFileTimeUtc();
            }
            else
            {
                this.LastModifiedTime = 0;                                      //fsi.LastWriteTime.ToFileTimeUtc(); //DateTime.MaxValue.ToFileTimeUtc();
            }
            this.LastMetadataModifiedTime = 0;                                  // dummy value for correctness of incrementals using filecompare
            this.CreateTime = Utilities.Utils.GetUtcUnixTime(fsi.CreationTime); //fsi.CreationTime.ToFileTimeUtc();
            //this.ID = Utilities.Utils.GetUnixTime(fsi.CreationTime);
            if (fsi.Attributes.HasFlag(Alphaleonis.Win32.Filesystem.FileAttributes.SparseFile))
            {
                this.ChangeStatus |= DataLayoutInfos.SparseFile;
            }
            if (fseInfo.IsMountPoint || fseInfo.IsReparsePoint || fseInfo.IsSymbolicLink)
            {
                this.TargetName = fseInfo.VirtualFullPath;
                Console.WriteLine("** Item " + fileName + " is a " + this.Kind);
                Console.WriteLine("reparsepoint tag(s)=" + fsi.SystemInfo.ReparsePointTag.ToString());
            }
            if (this.Kind == FileType.Unsupported)
            {
                Console.WriteLine("unsupported file " + fileName + " with attributes " + fseInfo.Attributes.ToString());
            }
            this.Attributes = (int)fsi.Attributes;
            //wSecurity =  GetSecurity(); // unneeded as we save using BackupRead(), which includes security info
            //ownerUser = wSecurity.GetOwner(typeof(NTAccount)).;
            BlockMetadata = new FileBlockMetadata();
        }
Example #42
0
        static void Main(string[] args)
        {
            Exceptionless.ExceptionlessClient.Default.Startup("fTcEOUkt1CxljTyOZfsr8AcSGQwWE4aYaYqk7cE1");
            SetupNLog();

            _logger = LogManager.GetCurrentClassLogger();

            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription(
                "Directory to look for hives (recursively). -f or -d is required.");
            _fluentCommandLineParser.Setup(arg => arg.HiveFile)
            .As('f')
            .WithDescription("Hive to process. -f or -d is required.\r\n");

            _fluentCommandLineParser.Setup(arg => arg.OutDirectory)
            .As("out")
            .WithDescription(
                "Directory to save updated hives to. Only dirty hives with logs applied will end up in --out directory\r\n");

            _fluentCommandLineParser.Setup(arg => arg.CopyAlways)
            .As("ca")
            .WithDescription(
                "When true, always copy hives to --out directory, even if they aren't dirty. Default is TRUE").SetDefault(true);

            _fluentCommandLineParser.Setup(arg => arg.CompressNames)
            .As("cn")
            .WithDescription(
                "When true, compress names for profile based hives. Default is TRUE\r\n").SetDefault(true);

            _fluentCommandLineParser.Setup(arg => arg.Debug)
            .As("debug")
            .WithDescription("Show debug information during processing").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Trace)
            .As("trace")
            .WithDescription("Show trace information during processing").SetDefault(false);

            var header =
                $"rla version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/RECmd\r\n\r\nNote: Enclose all strings containing spaces (and all RegEx) with double quotes";

            var footer = @"Example: rla.exe --f ""C:\Temp\UsrClass 1.dat"" --out C:\temp" +
                         "\r\n\t " +
                         @"rla.exe --d ""D:\temp\"" --out c:\temp" + "\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help").WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (_fluentCommandLineParser.Object.HiveFile.IsNullOrEmpty() == false ||
                _fluentCommandLineParser.Object.Directory.IsNullOrEmpty() == false)
            {
                if (_fluentCommandLineParser.Object.OutDirectory.IsNullOrEmpty())
                {
                    _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);
                    Console.WriteLine();
                    _logger.Warn($"--out is required. Exiting");
                    Console.WriteLine();
                    return;
                }
            }

            if (_fluentCommandLineParser.Object.Debug)
            {
                foreach (var r in LogManager.Configuration.LoggingRules)
                {
                    r.EnableLoggingForLevel(LogLevel.Debug);
                }

                LogManager.ReconfigExistingLoggers();
                _logger.Debug("Enabled debug messages...");
            }

            if (_fluentCommandLineParser.Object.Trace)
            {
                foreach (var r in LogManager.Configuration.LoggingRules)
                {
                    r.EnableLoggingForLevel(LogLevel.Trace);
                }

                LogManager.ReconfigExistingLoggers();
                _logger.Trace("Enabled trace messages...");
            }

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            var hivesToProcess = new List <string>();

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n");

            if (_fluentCommandLineParser.Object.HiveFile?.Length > 0)
            {
                if (File.Exists(_fluentCommandLineParser.Object.HiveFile) == false)
                {
                    _logger.Error($"File '{_fluentCommandLineParser.Object.HiveFile}' does not exist.");
                    return;
                }

                hivesToProcess.Add(_fluentCommandLineParser.Object.HiveFile);
            }
            else if (_fluentCommandLineParser.Object.Directory?.Length > 0)
            {
                if (Directory.Exists(_fluentCommandLineParser.Object.Directory) == false)
                {
                    _logger.Error($"Directory '{_fluentCommandLineParser.Object.Directory}' does not exist.");
                    return;
                }

                var okFileParts = new HashSet <string>();
                okFileParts.Add("USRCLASS");
                okFileParts.Add("NTUSER");
                okFileParts.Add("SYSTEM");
                okFileParts.Add("SAM");
                okFileParts.Add("SOFTWARE");
                okFileParts.Add("AMCACHE");
                okFileParts.Add("SYSCACHE");
                okFileParts.Add("SECURITY");
                okFileParts.Add("DRIVERS");
                okFileParts.Add("COMPONENTS");

                var f = new DirectoryEnumerationFilters();
                f.InclusionFilter = fsei =>
                {
                    if (fsei.Extension.ToUpperInvariant() == ".LOG1" || fsei.Extension.ToUpperInvariant() == ".LOG2" ||
                        fsei.Extension.ToUpperInvariant() == ".DLL" ||
                        fsei.Extension.ToUpperInvariant() == ".LOG" ||
                        fsei.Extension.ToUpperInvariant() == ".CSV" ||
                        fsei.Extension.ToUpperInvariant() == ".BLF" ||
                        fsei.Extension.ToUpperInvariant() == ".REGTRANS-MS" ||
                        fsei.Extension.ToUpperInvariant() == ".EXE" ||
                        fsei.Extension.ToUpperInvariant() == ".TXT" || fsei.Extension.ToUpperInvariant() == ".INI")
                    {
                        return(false);
                    }

                    var foundOkFilePart = false;

                    foreach (var okFilePart in okFileParts)
                    {
                        if (fsei.FileName.ToUpperInvariant().Contains(okFilePart))
                        {
                            foundOkFilePart = true;
                            //     return true;
                        }
                    }

                    if (foundOkFilePart == false)
                    {
                        return(false);
                    }

                    var fi = new FileInfo(fsei.FullPath);

                    if (fi.Length < 4)
                    {
                        return(false);
                    }

                    try
                    {
                        using (var fs = new FileStream(fsei.FullPath, FileMode.Open, FileAccess.Read))
                        {
                            using (var br = new BinaryReader(fs, new ASCIIEncoding()))
                            {
                                try
                                {
                                    var chunk = br.ReadBytes(4);

                                    var sig = BitConverter.ToInt32(chunk, 0);

                                    if (sig == 0x66676572)
                                    {
                                        return(true);
                                    }
                                }
                                catch (Exception)
                                {
                                }

                                return(false);
                            }
                        }
                    }
                    catch (IOException)
                    {
                        if (Helper.IsAdministrator() == false)
                        {
                            throw new UnauthorizedAccessException("Administrator privileges not found!");
                        }

                        var files = new List <string>();
                        files.Add(fsei.FullPath);

                        var rawf = Helper.GetFiles(files);

                        if (rawf.First().FileStream.Length == 0)
                        {
                            return(false);
                        }

                        try
                        {
                            var b = new byte[4];
                            rawf.First().FileStream.ReadExactly(b, 4);

                            var sig = BitConverter.ToInt32(b, 0);

                            if (sig == 0x66676572)
                            {
                                return(true);
                            }
                        }
                        catch (Exception)
                        {
                        }

                        return(false);
                    }
                };

                f.RecursionFilter = entryInfo => !entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink;

                f.ErrorFilter = (errorCode, errorMessage, pathProcessed) => true;

                var dirEnumOptions =
                    DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive |
                    DirectoryEnumerationOptions.SkipReparsePoints | DirectoryEnumerationOptions.ContinueOnException |
                    DirectoryEnumerationOptions.BasicSearch;

                if (Directory.Exists(_fluentCommandLineParser.Object.OutDirectory) == false)
                {
                    _logger.Info($"Creating --out directory '{_fluentCommandLineParser.Object.OutDirectory}'...");
                    Directory.CreateDirectory(_fluentCommandLineParser.Object.OutDirectory);
                }
                else
                {
                    if (Directory.GetFiles(_fluentCommandLineParser.Object.OutDirectory).Length > 0 && _fluentCommandLineParser.Object.CompressNames)
                    {
                        _logger.Warn($"'{_fluentCommandLineParser.Object.OutDirectory}' contains files! This may cause --cn to revert back to uncompressed names. Ideally, '{_fluentCommandLineParser.Object.OutDirectory}' should be empty.");
                        Console.WriteLine();
                    }
                }

                _logger.Fatal($"Searching '{_fluentCommandLineParser.Object.Directory}' for hives...");

                var files2 =
                    Directory.EnumerateFileSystemEntries(_fluentCommandLineParser.Object.Directory, dirEnumOptions, f);

                var count = 0;

                try
                {
                    hivesToProcess.AddRange(files2);
                    count = hivesToProcess.Count;

                    _logger.Info($"\tHives found: {count:N0}");
                }
                catch (Exception ex)
                {
                    _logger.Fatal($"Could not access all files in '{_fluentCommandLineParser.Object.Directory}'! Error: {ex.Message}");
                    _logger.Error("");
                    _logger.Fatal("Rerun the program with Administrator privileges to try again\r\n");
                    //Environment.Exit(-1);
                }
            }
            else
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);
                return;
            }


            if (hivesToProcess.Count == 0)
            {
                _logger.Warn("No hives were found. Exiting...");

                return;
            }

            _sw = new Stopwatch();
            _sw.Start();

            foreach (var hiveToProcess in hivesToProcess)
            {
                _logger.Info("");

                byte[] updatedBytes = null;

                _logger.Info($"Processing hive '{hiveToProcess}'");

                if (File.Exists(hiveToProcess) == false)
                {
                    _logger.Warn($"'{hiveToProcess}' does not exist. Skipping");
                    continue;
                }

                try
                {
                    RegistryHive reg;

                    var dirname  = Path.GetDirectoryName(hiveToProcess);
                    var hiveBase = Path.GetFileName(hiveToProcess);

                    List <RawCopyReturn> rawFiles = null;

                    try
                    {
                        using (var fs = new FileStream(hiveToProcess, FileMode.Open, FileAccess.Read))
                        {
                            reg = new RegistryHive(fs.ReadFully(), hiveToProcess)
                            {
                            };
                        }
                    }
                    catch (IOException)
                    {
                        //file is in use

                        if (Helper.IsAdministrator() == false)
                        {
                            throw new UnauthorizedAccessException("Administrator privileges not found!");
                        }

                        _logger.Warn($"\t'{hiveToProcess}' is in use. Rerouting...\r\n");

                        var files = new List <string>();
                        files.Add(hiveToProcess);

                        var logFiles = Directory.GetFiles(dirname, $"{hiveBase}.LOG?");

                        foreach (var logFile in logFiles)
                        {
                            files.Add(logFile);
                        }

                        rawFiles = Helper.GetFiles(files);

                        if (rawFiles.First().FileStream.Length == 0)
                        {
                            continue;
                        }

                        var bb = rawFiles.First().FileStream.ReadFully();

                        reg = new RegistryHive(bb, rawFiles.First().InputFilename);
                    }

                    if (reg.Header.PrimarySequenceNumber != reg.Header.SecondarySequenceNumber)
                    {
                        if (string.IsNullOrEmpty(dirname))
                        {
                            dirname = ".";
                        }

                        var logFiles = Directory.GetFiles(dirname, $"{hiveBase}.LOG?");

                        if (logFiles.Length == 0)
                        {
                            if (_fluentCommandLineParser.Object.CopyAlways)
                            {
                                _logger.Info($"\tHive '{hiveToProcess}' is dirty, but no logs were found in the same directory. --ca is true. Copying...");
                                updatedBytes = File.ReadAllBytes(hiveToProcess);
                            }
                            else
                            {
                                _logger.Info($"\tHive '{hiveToProcess}' is dirty and no transaction logs were found in the same directory. --ca is false. Skipping...");
                                continue;
                            }
                        }

                        if (updatedBytes == null)
                        {
                            if (rawFiles != null)
                            {
                                var lt = new List <TransactionLogFileInfo>();
                                foreach (var rawCopyReturn in rawFiles.Skip(1).ToList())
                                {
                                    var bb1 = rawCopyReturn.FileStream.ReadFully();

                                    var tt = new TransactionLogFileInfo(rawCopyReturn.InputFilename, bb1);
                                    lt.Add(tt);
                                }

                                updatedBytes = reg.ProcessTransactionLogs(lt);
                            }
                            else
                            {
                                updatedBytes = reg.ProcessTransactionLogs(logFiles.ToList());
                            }
                        }
                    }

                    if (updatedBytes == null)
                    {
                        if (_fluentCommandLineParser.Object.CopyAlways)
                        {
                            _logger.Info($"\tHive '{hiveToProcess}' is not dirty, but --ca is true. Copying...");
                            updatedBytes = File.ReadAllBytes(hiveToProcess);
                        }
                        else
                        {
                            _logger.Info($"\tHive '{hiveToProcess}' is not dirty and --ca is false. Skipping...");
                            continue;
                        }
                    }

                    var outFile    = hiveToProcess.Replace(":", "").Replace("\\", "_");
                    var outFileAll = Path.Combine(_fluentCommandLineParser.Object.OutDirectory, outFile);

                    if (_fluentCommandLineParser.Object.CompressNames &&
                        (outFileAll.ToUpperInvariant().Contains("NTUSER") || outFileAll.ToUpperInvariant().Contains("USRCLASS")))
                    {
                        var dl   = hiveToProcess[0].ToString();
                        var segs = hiveToProcess.SplitAndTrim('\\');

                        var profile  = segs[2];
                        var filename = Path.GetFileName(hiveToProcess);

                        var outFile2 = $"{dl}_{profile}_{filename}";

                        outFileAll = Path.Combine(_fluentCommandLineParser.Object.OutDirectory, outFile2);
                    }

                    if (File.Exists(outFileAll))
                    {
                        var oldOut = outFileAll;

                        outFileAll = Path.Combine(_fluentCommandLineParser.Object.OutDirectory, outFile);

                        _logger.Warn($"\tFile '{oldOut}' exists! Saving as non-compressed name: '{outFileAll}'");
                    }

                    _logger.Fatal($"\tSaving updated hive to '{outFileAll}'");

                    using (var fs = new FileStream(outFileAll, FileMode.Create))
                    {
                        fs.Write(updatedBytes, 0, updatedBytes.Length);

                        fs.Flush();

                        fs.Close();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Sequence numbers do not match and transaction") == false)
                    {
                        if (ex.Message.Contains("Administrator privileges not found"))
                        {
                            _logger.Fatal($"Could not access '{hiveToProcess}' because it is in use");
                            _logger.Error("");
                            _logger.Fatal("Rerun the program with Administrator privileges to try again\r\n");
                        }
                        else
                        {
                            _logger.Error($"There was an error: {ex.Message}");
                        }
                    }
                }
            }

            _sw.Stop();
            _logger.Info("");

            _logger.Info($"Total processing time: {_sw.Elapsed.TotalSeconds:N3} seconds");
            _logger.Info("");
        }