-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f60b45
commit 6d83b84
Showing
18 changed files
with
237 additions
and
854 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export const isMetadataFile = filename => filename.endsWith('.json') && !filename.endsWith('.json.noncrypted.json') | ||
export const isMetadataFile = filename => filename.endsWith('.json') | ||
export const isVhdFile = filename => filename.endsWith('.vhd') | ||
export const isXvaFile = filename => filename.endsWith('.xva') | ||
export const isXvaSumFile = filename => filename.endsWith('.xva.checksum') |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
import { basename } from 'node:path' | ||
import { sha256 } from './directory.mjs' | ||
|
||
export function computeCacheFilePath(path, immutabilityCachePath, isFile) { | ||
return path.join(immutabilityCachePath, `${sha256(path)}.${isFile ? 'file' : 'dir'}.${basename(path)}`) | ||
} |
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,8 +1,22 @@ | ||
import execa from 'execa' | ||
export async function makeImmutable(path){ | ||
return execa('chattr', ['+i', '-R', path]) | ||
import { createHash } from 'node:crypto' | ||
import fs from 'node:fs/promises' | ||
import { computeCacheFilePath } from './computeCacheFilePath.mjs' | ||
|
||
export function sha256(content) { | ||
return createHash('sha256').update(content).digest('hex') | ||
} | ||
|
||
export async function makeImmutable(dirPath, immutabilityCachePath) { | ||
const cacheFileName = computeCacheFilePath(dirPath, immutabilityCachePath, false) | ||
await fs.writeFile(cacheFileName, dirPath) | ||
await execa('chattr', ['+i', '-R', dirPath]) | ||
await execa('chattr', ['+i', cacheFileName]) | ||
} | ||
|
||
export async function liftImmutability(path){ | ||
return execa('chattr', ['-i','-R', path]) | ||
} | ||
export async function liftImmutability(dirPath, immutabilityCachePath) { | ||
const cacheFileName = computeCacheFilePath(dirPath, immutabilityCachePath, false) | ||
await execa('chattr', ['-i', cacheFileName]) | ||
await execa('chattr', ['-i', '-R', dirPath]) | ||
await fs.unlink(cacheFileName) | ||
} |
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,20 +1,20 @@ | ||
import execa from 'execa' | ||
import fs from 'node:fs/promises' | ||
import { computeCacheFilePath } from './computeCacheFilePath.mjs' | ||
|
||
// this work only on linux like systems | ||
// this could wokr on windwos : https://4sysops.com/archives/set-and-remove-the-read-only-file-attribute-with-powershell/ | ||
|
||
export async function makeImmutable(path){ | ||
await execa('chattr', ['+i', path]) | ||
export async function makeImmutable(path, immutabilityCachePath) { | ||
const cacheFileName = computeCacheFilePath(path, immutabilityCachePath, true) | ||
await fs.writeFile(cacheFileName, path) | ||
await execa('chattr', ['+i', path]) | ||
await execa('chattr', ['+i', cacheFileName]) | ||
} | ||
|
||
export async function liftImmutability(path){ | ||
console.log('lift', path) | ||
return execa('chattr', ['-i', path]) | ||
} | ||
|
||
export async function isImmutable(path){ | ||
const {stdout} = await execa('lsattr', [path]) | ||
const [flags] = stdout.split(' ') | ||
return flags.includes('i') | ||
export async function liftImmutability(filePath, immutabilityCachePath) { | ||
const cacheFileName = computeCacheFilePath(filePath, immutabilityCachePath, false) | ||
await execa('chattr', ['-i', cacheFileName]) | ||
await execa('chattr', ['-i', filePath]) | ||
await fs.unlink(cacheFileName) | ||
} |
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,22 @@ | ||
import { asyncEach } from '@vates/async-each' | ||
import fs from 'node:fs/promises' | ||
|
||
export async function watchForExistingAndNew(path, callback) { | ||
asyncEach(fs.readdir(path, entry => callback(entry, false, watcher))) | ||
|
||
const watcher = fs.watch(path) | ||
|
||
for await (const { eventType, filename } of watcher) { | ||
if (eventType === 'change') { | ||
continue | ||
} | ||
if (filename.startsWith('.')) { | ||
// temp file during upload | ||
continue | ||
} | ||
const stat = await fs.stat(path) | ||
if (stat.mtimeMs === stat.birthtimeMs) { | ||
await callback(filename, true, watcher) | ||
} | ||
} | ||
} |
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,26 @@ | ||
import { join } from 'node:path' | ||
import * as Directory from './file.mjs' | ||
import { watchForExistingAndNew } from './newFileWatcher.mjs' | ||
|
||
async function waitForCompletion(path) { | ||
await watchForExistingAndNew(path, (pathInDirectory, isNew, watcher) => { | ||
if (!isNew) { | ||
return | ||
} | ||
if (pathInDirectory === 'metadata.json') { | ||
watcher.close() | ||
// will end the watcher, stop this loop and return | ||
} | ||
}) | ||
} | ||
|
||
export async function watch(basePath, immutabilityCachePath) { | ||
await watchForExistingAndNew(basePath, async (pathInDirectory, isNew) => { | ||
if (!isNew) { | ||
return | ||
} | ||
const path = join(basePath, pathInDirectory) | ||
await waitForCompletion(path) | ||
await Directory.makeImmutable(path, immutabilityCachePath) | ||
}) | ||
} |
Oops, something went wrong.