diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-10-01 22:16:53 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-10-01 22:16:53 -0500 |
| commit | 14cdb89836575741104cfcf73f446233f18c3809 (patch) | |
| tree | bbb5ae3b9da16cd2cf8897fd033dcf83dab587fd /src | |
| parent | 6598b2ad3d7dcb2b814cee6a3f7b38b5365a7522 (diff) | |
| download | slskd-14cdb89836575741104cfcf73f446233f18c3809.tar.gz | |
implement file pruning
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 0ebffe94..b084e684 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -744,18 +744,52 @@ namespace slskd private void Clock_EveryHour(object sender, EventArgs e) { - _ = Task.Run(() => PruneLogs()); } private void PruneFiles() { - // todo: incomplete - // todo: complete - } + void PruneDirectory(int? age, string directory) + { + if (!age.HasValue) + { + return; + } - private void PruneLogs() - { - // todo: delete files matching pattern slskd-20230216.log + Log.Debug("Pruning files older than {Age} minutes from {Directory}", age, directory); + + var options = new EnumerationOptions + { + IgnoreInaccessible = true, + AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, + RecurseSubdirectories = true, + }; + + var files = System.IO.Directory.GetFiles(directory, "*", options) + .Select(filename => new FileInfo(filename)) + .Where(file => file.LastAccessTimeUtc <= DateTime.UtcNow.AddMinutes(-age.Value)); + + Log.Debug("Found {Count} files of need of pruning", files.Count()); + + int errors = 0; + + foreach (var file in files) + { + try + { + file.Delete(); + } + catch (Exception ex) + { + errors++; + Log.Warning(ex, "Failed to prune file {File}: {Message}", file, ex.Message); + } + } + + Log.Debug("Pruning complete. Deleted: {Deleted}, Errors: {Errors}", files.Count() - errors, errors); + } + + PruneDirectory(age: Options.Retention.File.Incomplete, directory: Options.Directories.Incomplete); + PruneDirectory(age: Options.Retention.File.Complete, directory: Options.Directories.Downloads); } private void PruneTransfers() |
