-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prune invalid ssz objects (#6875)
* fix: prune invalid ssz objects * fix: handle non-existing invalidSszObjects folder * Review PR * fix: expect empty dirs to be removed in unit tests --------- Co-authored-by: Nico Flaig <nflaig@protonmail.com>
- Loading branch information
Showing
3 changed files
with
28 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): void { | ||
export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): number { | ||
let deletedFileCount = 0; | ||
for (const entryName of fs.readdirSync(dirpath)) { | ||
const entryPath = path.join(dirpath, entryName); | ||
|
||
const stat = fs.statSync(entryPath); | ||
if (stat.isDirectory()) { | ||
pruneOldFilesInDir(entryPath, maxAgeMs); | ||
deletedFileCount += pruneOldFilesInDir(entryPath, maxAgeMs); | ||
} else if (stat.isFile()) { | ||
if (Date.now() - stat.mtimeMs > maxAgeMs) { | ||
fs.unlinkSync(entryPath); | ||
deletedFileCount += 1; | ||
} | ||
} | ||
} | ||
|
||
// if all files are deleted, delete the directory | ||
if (fs.readdirSync(dirpath).length === 0) { | ||
fs.rmdirSync(dirpath); | ||
} | ||
return deletedFileCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters