コード例 #1
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);
        }
コード例 #2
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);
            }
        }
コード例 #3
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);
        }
コード例 #4
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);
        }