Пример #1
0
        private async Task PrintFolderRecursively(FolderModel f, StreamWriter sw)
        {
            await sw.WriteLineAsync($"{f.name}: {f.total_size}");

            foreach (var child in f.folders)
            {
                await PrintFolderRecursively(child, sw);
            }
        }
Пример #2
0
        /// <summary>
        /// Write just this folder and its files to the DB
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        public async Task Write(FolderModel folder)
        {
            using (var conn = new SQLiteConnection(_connection_string))
            {
                conn.Open();
                var tran = conn.BeginTransaction();
                try
                {
                    // Insert the folder
                    var result = await conn.QueryAsync <long>("INSERT OR IGNORE INTO folders "
                                                              + "(name, total_size, parent_folder_id) "
                                                              + "VALUES (@name, @total_size, @parent_folder_id); "
                                                              + "SELECT last_insert_rowid();", folder, tran, null, System.Data.CommandType.Text);

                    folder.id = result.FirstOrDefault();

                    // Assign the ID to all children and insert them
                    foreach (var file in folder.files)
                    {
                        file.parent_folder_id = folder.id;
                        var result2 = await conn.QueryAsync <long>("INSERT OR IGNORE INTO files "
                                                                   + "(name, parent_folder_id, size, hash, last_modified) "
                                                                   + "VALUES (@name, @parent_folder_id, @size, @hash, @last_modified);"
                                                                   + "SELECT last_insert_rowid();", file, tran, null, System.Data.CommandType.Text);

                        file.id = result2.FirstOrDefault();
                    }

                    // Update all the child folders
                    foreach (var subfolder in folder.folders)
                    {
                        await conn.ExecuteAsync("UPDATE folders SET parent_folder_id = @parent_folder_id WHERE id = @id;",
                                                new { parent_folder_id = folder.id, id = subfolder.id }, tran, null, System.Data.CommandType.Text);
                    }

                    // We're done
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    tran.Rollback();
                }
            }
        }
Пример #3
0
        public async Task <FolderModel> ScanFolder(string path)
        {
            FoldersFound++;

            // Insert this folder
            var folder = new FolderModel()
            {
                name    = path,
                files   = new List <FileModel>(),
                folders = new List <FolderModel>()
            };

            // Scan and capture problems
            try
            {
                var this_dir = new DirectoryInfo(path);

                // Insert all files
                foreach (var f in this_dir.GetFiles())
                {
                    // Ignore the filescan DB
                    if (!should_ignore(f.FullName))
                    {
                        var file = new FileModel()
                        {
                            name          = f.Name,
                            size          = f.Length,
                            last_modified = f.LastWriteTime.ToString("o"),
                            hash          = null
                        };
                        folder.files.Add(file);
                        TrackFile(file);
                        FilesScanned++;
                    }
                }

                // Check all subfolders
                var subfolder_tasks = (from d in this_dir.GetDirectories() where !should_ignore(d.FullName) select ScanFolder(d.FullName));
                await Task.WhenAll(subfolder_tasks.ToArray());

                foreach (var t in subfolder_tasks)
                {
                    folder.folders.Add(t.Result);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Exception scanning {path}: {ex.Message}.");
            }

            // Update information about this folder, then save it to the database
            folder.total_size =
                (from f in folder.files select f.size).Sum()
                + (from sub in folder.folders select folder.total_size).Sum();

            // Here's the folder we scanned
            FoldersScanned++;
            var ts = DateTime.UtcNow - LastPrintTime;

            if (ts.TotalMilliseconds > 100)
            {
                Console.Write($"\rScanned {FoldersScanned}/{FoldersFound} folders, found {FilesScanned} files...");
                LastPrintTime = DateTime.UtcNow;
            }
            return(folder);
        }
Пример #4
0
 public async Task Begin()
 {
     RootFolder = await ScanFolder(_root_path);
 }