-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prune invalid SSZ objects (#5388)
* Prune old invalid SSZ files * Fix pruneOldFilesInDir * Capitalize error message * Rephrase CLI flag description * Check mtimeMs instead of ctimeMs * Add tests for pruneOldFilesInDir --------- Co-authored-by: dapplion <35266934+dapplion@users.noreply.github.com> Co-authored-by: Nico Flaig <nflaig@protonmail.com>
- Loading branch information
1 parent
7bf0b75
commit e2e5417
Showing
5 changed files
with
128 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): void { | ||
for (const entryName of fs.readdirSync(dirpath)) { | ||
const entryPath = path.join(dirpath, entryName); | ||
|
||
const stat = fs.statSync(entryPath); | ||
if (stat.isDirectory()) { | ||
pruneOldFilesInDir(entryPath, maxAgeMs); | ||
} else if (stat.isFile()) { | ||
if (Date.now() - stat.mtimeMs > maxAgeMs) { | ||
fs.unlinkSync(entryPath); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import {rimraf} from "rimraf"; | ||
import {expect} from "chai"; | ||
import {pruneOldFilesInDir} from "../../../src/util/index.js"; | ||
import {testFilesDir} from "../../utils.js"; | ||
|
||
describe("pruneOldFilesInDir", () => { | ||
const DAYS_TO_MS = 24 * 60 * 60 * 1000; | ||
const dataDir = path.join(testFilesDir, "prune-old-files-test"); | ||
const newFile = "newFile.txt"; | ||
const oldFile = "oldFile.txt"; | ||
|
||
beforeEach(() => { | ||
fs.mkdirSync(dataDir, {recursive: true}); | ||
createFileWithAge(path.join(dataDir, oldFile), 2); | ||
createFileWithAge(path.join(dataDir, newFile), 0); | ||
}); | ||
|
||
afterEach(() => { | ||
rimraf.sync(dataDir); | ||
}); | ||
|
||
it("should delete old files", () => { | ||
pruneOldFilesInDir(dataDir, DAYS_TO_MS); | ||
|
||
const files = fs.readdirSync(dataDir); | ||
expect(files).to.not.include(oldFile); | ||
}); | ||
|
||
it("should not delete new files", () => { | ||
pruneOldFilesInDir(dataDir, DAYS_TO_MS); | ||
|
||
const files = fs.readdirSync(dataDir); | ||
expect(files).to.include(newFile); | ||
}); | ||
|
||
it("should delete old files in nested directories", () => { | ||
const nestedDir = path.join(dataDir, "prune-old-files-nested"); | ||
|
||
fs.mkdirSync(nestedDir); | ||
createFileWithAge(path.join(nestedDir, "nestedFile.txt"), 2); | ||
|
||
pruneOldFilesInDir(dataDir, DAYS_TO_MS); | ||
|
||
expect(fs.readdirSync(nestedDir)).to.be.empty; | ||
}); | ||
|
||
it("should handle empty directories", () => { | ||
const emptyDir = path.join(dataDir, "prune-old-files-empty"); | ||
fs.mkdirSync(emptyDir, {recursive: true}); | ||
|
||
pruneOldFilesInDir(emptyDir, DAYS_TO_MS); | ||
|
||
expect(fs.readdirSync(emptyDir)).to.be.empty; | ||
}); | ||
|
||
function createFileWithAge(path: string, ageInDays: number): void { | ||
// Create a new empty file | ||
fs.closeSync(fs.openSync(path, "w")); | ||
|
||
if (ageInDays > 0) { | ||
// Alter file's access and modification dates | ||
fs.utimesSync(path, Date.now() / 1000, (Date.now() - ageInDays * DAYS_TO_MS) / 1000); | ||
} | ||
} | ||
}); |