示例#1
0
        private static int VolumeMaintenance(VolumeMaintenanceOptions args)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(args.LogPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + args.DatabasePath)) {
                    connection.Open();

                    if ((args.EnableVolume || args.DisableVolume) && !(args.EnableVolume && args.DisableVolume))
                    {
                        VolumeOperations.SetVolumeEnabledState(connection, args.Volume.Value, args.EnableVolume);
                    }

                    if (args.PurgeData)
                    {
                        // to do this we start a fake scan that isn't actually allowed to see/touch any file
                        var volumes = VolumeOperations.GetKnownVolumes(connection);
                        foreach (Volume v in volumes)
                        {
                            if (args.Volume.Value == v.ID)
                            {
                                ProcessVolume(textWriterWrapper.Writer, connection, v, fakeScan: true);
                            }
                        }
                    }

                    connection.Close();
                }

            return(0);
        }
示例#2
0
        public static int AddPathToArchive(TextWriter writer, SQLiteConnection connection, long archiveId, string path)
        {
            List <Volume> volumes = VolumeOperations.FindAndInsertAttachedVolumes(connection);
            string        devId   = Win32Util.FindVolumeIdFromPath(path);
            Volume        vol     = volumes.First(x => x.DeviceID == devId);

            // messy nonsense
            var    fi      = new FileInfo(path);
            string subpath = fi.FullName.Substring(Path.GetPathRoot(fi.FullName).Length).Replace("\\", "/");

            if (!subpath.StartsWith("/"))
            {
                subpath = "/" + subpath;
            }
            if (subpath.EndsWith("/"))
            {
                subpath = subpath.Substring(0, subpath.Length - 1);
            }
            if (subpath == "" || subpath == "/")
            {
                return(-1);
            }

            using (IDbTransaction t = connection.BeginTransaction()) {
                long pathId = DatabaseHelper.InsertOrUpdatePath(t, vol.ID, subpath);
                HyoutaTools.SqliteUtil.Update(t, "INSERT INTO ArchivePaths (archiveId, pathId) VALUES (?, ?)", new object[] { archiveId, pathId });
                t.Commit();
            }

            return(0);
        }
示例#3
0
        private static int ListVolumes(string logPath, string databasePath, bool showDisabledVolumes)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(logPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath)) {
                    connection.Open();
                    PrintVolumeInformation(textWriterWrapper.Writer, VolumeOperations.GetKnownVolumes(connection).Where(x => showDisabledVolumes || x.ShouldScan));
                    connection.Close();
                }

            return(0);
        }
示例#4
0
        private static int Search(SearchOptions args)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(args.LogPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + args.DatabasePath)) {
                    connection.Open();
                    List <Volume> volumes = VolumeOperations.GetKnownVolumes(connection);
                    foreach (var sameFiles in CollectFiles(connection, GetFilesWithFilename(connection, "%" + args.File + "%"), (x) => true))
                    {
                        PrintSameFileInformation(textWriterWrapper.Writer, sameFiles, volumes);
                    }
                    connection.Close();
                }

            return(0);
        }
示例#5
0
        private static int ListFiles(string logPath, string databasePath, int?volume, bool selectedVolumeOnly, ShouldPrint shouldPrint)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(logPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath)) {
                    connection.Open();

                    List <StorageFile> files   = GetKnownFilesOnVolume(connection, volume);
                    List <Volume>      volumes = VolumeOperations.GetKnownVolumes(connection);
                    foreach (var sameFiles in CollectFiles(connection, files, shouldPrint, selectedVolumeOnly ? volume : null))
                    {
                        PrintSameFileInformation(textWriterWrapper.Writer, sameFiles, volumes);
                    }

                    connection.Close();
                }

            return(0);
        }
示例#6
0
        public static void DoArchiveScan(TextWriter writer, SQLiteConnection connection, string scanPath)
        {
            bool fileExists = new FileInfo(scanPath).Exists;
            bool dirExists  = new DirectoryInfo(scanPath).Exists;

            if (fileExists == dirExists)
            {
                return;
            }

            List <Volume>  volumes  = VolumeOperations.FindAndInsertAttachedVolumes(connection);
            List <Archive> archives = GetKnownArchives(connection);

            if (fileExists)
            {
                ScanFile(writer, connection, volumes, archives, new FileInfo(scanPath));
            }
            else
            {
                ScanDirectory(writer, connection, volumes, archives, scanPath);
            }
        }
示例#7
0
        private static int Scan(ScanOptions args)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(args.LogPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + args.DatabasePath)) {
                    connection.Open();
                    using (IDbTransaction t = connection.BeginTransaction()) {
                        DatabaseHelper.CreateTables(t);
                        t.Commit();
                    }

                    var volumes = VolumeOperations.FindAndInsertAttachedVolumes(connection, args.Volume);
                    foreach (Volume v in volumes)
                    {
                        if (!args.Volume.HasValue || (args.Volume.Value == v.ID))
                        {
                            ProcessVolume(textWriterWrapper.Writer, connection, v);
                        }
                    }

                    connection.Close();
                }

            return(0);
        }
示例#8
0
        public static int PrintExistingArchives(string logPath, string databasePath)
        {
            using (TextWriterWrapper textWriterWrapper = new TextWriterWrapper(logPath))
                using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath)) {
                    connection.Open();
                    List <Volume>  volumes  = VolumeOperations.GetKnownVolumes(connection);
                    List <Archive> archives = GetKnownArchives(connection);
                    connection.Close();

                    foreach (Archive a in archives)
                    {
                        textWriterWrapper.Writer.WriteLine("Archive #{0}:", a.ArchiveId);
                        foreach (ArchivePath p in a.Paths)
                        {
                            textWriterWrapper.Writer.WriteLine("  Volume #{0} {1}{2}", p.VolumeId, volumes.First(x => x.ID == p.VolumeId).Label, p.Path);
                        }
                        foreach (ArchivePattern p in a.Patterns)
                        {
                            textWriterWrapper.Writer.WriteLine("  Pattern #{0}: {1} from {2} to {3}", p.ArchivePatternId, p.Pattern, p.TimestampBegin, p.TimestampEnd);
                        }
                    }
                }
            return(0);
        }
示例#9
0
        private static int RunInteractiveFileDeleteMode(string databasePath, long volumeId, ShouldPrint shouldPrint)
        {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + databasePath)) {
                connection.Open();

                Volume volume;
                {
                    var volumes = VolumeOperations.FindAndInsertAttachedVolumes(connection);
                    volume = volumes.FirstOrDefault((x) => x.ID == volumeId);
                }
                if (volume == null)
                {
                    Console.WriteLine("Volume {0} is not attached.", volumeId);
                    return(-1);
                }


                List <StorageFile>         files    = GetKnownFilesOnVolume(connection, volumeId);
                List <List <StorageFile> > allfiles = CollectFiles(connection, files, shouldPrint, volumeId).ToList();
                for (int allfilesindex = 0; allfilesindex < allfiles.Count; allfilesindex++)
                {
                    allfiles[allfilesindex] = allfiles[allfilesindex].OrderBy(x => (x.Path + "/" + x.Filename)).ToList();
                }

                int more = 5;
                for (int allfilesindex = 0; allfilesindex < allfiles.Count; allfilesindex++)
                {
                    List <StorageFile> sameFiles         = allfiles[allfilesindex];
                    List <string>      existingFilenames = new List <string>();
                    foreach (StorageFile ssf in sameFiles)
                    {
                        if (!existingFilenames.Contains(ssf.Filename))
                        {
                            existingFilenames.Add(ssf.Filename);
                        }
                    }
                    existingFilenames.Sort();

                    int?selectedFilename = null;
                    while (true)
                    {
                        int prevfilesindex = allfilesindex - 1;
                        int nextfilesindex = allfilesindex + more;
                        if (prevfilesindex >= 0)
                        {
                            PrintFileInfoForInteractiveDelete(allfiles[prevfilesindex], "     ");
                            Console.WriteLine();
                        }
                        PrintFileInfoForInteractiveDelete(sameFiles, " >>> ");

                        if (existingFilenames.Count > 1)
                        {
                            Console.WriteLine(" >>> Available filenames:");
                            for (int i = 0; i < existingFilenames.Count; ++i)
                            {
                                Console.WriteLine(" >>> {2}Filename {0}: {1}", AsLetter(i), existingFilenames[i], selectedFilename == i ? "!" : " ");
                            }
                        }

                        for (int iiii = allfilesindex + 1; iiii <= nextfilesindex; ++iiii)
                        {
                            if (iiii < allfiles.Count)
                            {
                                Console.WriteLine();
                                PrintFileInfoForInteractiveDelete(allfiles[iiii], "     ");
                            }
                        }

                        Console.WriteLine();
                        Console.WriteLine(" [file {0}/{1}, {2} to go]", allfilesindex + 1, allfiles.Count, allfiles.Count - allfilesindex);

                        Console.WriteLine();
                        Console.WriteLine("Enter number of file to keep, lowercase letter for target filename, nothing to skip, Q to quit, +/- to show more/less files.");
                        Console.Write(" > ");

                        string input = Console.ReadLine();
                        if (input == "")
                        {
                            Console.Clear();
                            break;
                        }
                        if (input == "+")
                        {
                            ++more;
                            Console.Clear();
                            continue;
                        }
                        if (input == "-")
                        {
                            if (more > 0)
                            {
                                --more;
                            }
                            Console.Clear();
                            continue;
                        }
                        if (input == "Q")
                        {
                            return(-2);
                        }

                        int number;
                        if (int.TryParse(input, out number))
                        {
                            if (number >= 0 && number < sameFiles.Count)
                            {
                                List <(FileInfo fi, bool shouldDelete)> data = new List <(FileInfo fi, bool shouldDelete)>();
                                for (int i = 0; i < sameFiles.Count; ++i)
                                {
                                    var    sf = sameFiles[i];
                                    string path;
                                    if (sf.Path == "" || sf.Path == "/" || sf.Path == "\\")
                                    {
                                        path = Path.Combine(volume.DeviceID, sf.Filename);
                                    }
                                    else if (sf.Path[0] == '/' || sf.Path[0] == '\\')
                                    {
                                        path = Path.Combine(volume.DeviceID, sf.Path.Substring(1).Replace('/', '\\'), sf.Filename);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Unexpected path in database: " + sf.Path);
                                        break;
                                    }
                                    data.Add((new FileInfo(path), i != number));
                                }

                                Console.Clear();

                                bool inconsistent = false;
                                foreach (var d in data)
                                {
                                    if (!d.fi.Exists)
                                    {
                                        Console.WriteLine("Inconsistent file state: File {0} does not actually exist on disk.", d.fi.FullName);
                                        inconsistent = true;
                                    }
                                }
                                if (!inconsistent)
                                {
                                    foreach (var d in data)
                                    {
                                        if (d.shouldDelete)
                                        {
                                            Console.WriteLine("Deleting {0}", d.fi.FullName);
                                            d.fi.Delete();
                                        }
                                        else
                                        {
                                            if (selectedFilename.HasValue)
                                            {
                                                string newpath = Path.Combine(Path.GetDirectoryName(d.fi.FullName), existingFilenames[selectedFilename.Value]);
                                                if (File.Exists(newpath))
                                                {
                                                    Console.WriteLine("Keeping {0}; renaming to {1} not possible, skipping it", d.fi.FullName, newpath);
                                                }
                                                else
                                                {
                                                    Console.WriteLine("Renaming {0} to {1}", d.fi.FullName, newpath);
                                                    File.Move(d.fi.FullName, newpath);
                                                }
                                            }
                                            else
                                            {
                                                Console.WriteLine("Keeping {0}", d.fi.FullName);
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }

                        for (int i = 0; i < existingFilenames.Count; ++i)
                        {
                            if (AsLetter(i) == input)
                            {
                                selectedFilename = i;
                                break;
                            }
                        }
                    }
                }

                connection.Close();
            }

            return(0);
        }