@zenfs/core
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -82,4 +82,4 @@ import type { File } from '../file.js'; | ||
*/ | ||
private operateOnWritable; | ||
private operateOnWritableAsync; | ||
private copyForWriteSync; | ||
private copyForWrite; | ||
/** | ||
@@ -86,0 +86,0 @@ * Copy from readable to writable storage. |
@@ -0,1 +1,46 @@ | ||
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { | ||
if (value !== null && value !== void 0) { | ||
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); | ||
var dispose; | ||
if (async) { | ||
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); | ||
dispose = value[Symbol.asyncDispose]; | ||
} | ||
if (dispose === void 0) { | ||
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); | ||
dispose = value[Symbol.dispose]; | ||
} | ||
if (typeof dispose !== "function") throw new TypeError("Object not disposable."); | ||
env.stack.push({ value: value, dispose: dispose, async: async }); | ||
} | ||
else if (async) { | ||
env.stack.push({ async: true }); | ||
} | ||
return value; | ||
}; | ||
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { | ||
return function (env) { | ||
function fail(e) { | ||
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; | ||
env.hasError = true; | ||
} | ||
function next() { | ||
while (env.stack.length) { | ||
var rec = env.stack.pop(); | ||
try { | ||
var result = rec.dispose && rec.dispose.call(rec.value); | ||
if (rec.async) return Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); | ||
} | ||
catch (e) { | ||
fail(e); | ||
} | ||
} | ||
if (env.hasError) throw env.error; | ||
} | ||
return next(); | ||
}; | ||
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { | ||
var e = new Error(message); | ||
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; | ||
}); | ||
import { dirname } from '../emulation/path.js'; | ||
@@ -50,3 +95,3 @@ import { Errno, ErrnoError } from '../error.js'; | ||
async sync(path, data, stats) { | ||
await this.createParentDirectories(path); | ||
await this.copyForWrite(path); | ||
if (!(await this.writable.exists(path))) { | ||
@@ -58,3 +103,3 @@ await this.writable.createFile(path, 'w', 0o644); | ||
syncSync(path, data, stats) { | ||
this.createParentDirectoriesSync(path); | ||
this.copyForWriteSync(path); | ||
this.writable.syncSync(path, data, stats); | ||
@@ -97,2 +142,3 @@ } | ||
this.checkPath(newPath); | ||
await this.copyForWrite(oldPath); | ||
try { | ||
@@ -111,2 +157,3 @@ await this.writable.rename(oldPath, newPath); | ||
this.checkPath(newPath); | ||
this.copyForWriteSync(oldPath); | ||
try { | ||
@@ -131,3 +178,3 @@ this.writable.renameSync(oldPath, newPath); | ||
const oldStat = new Stats(await this.readable.stat(path)); | ||
// Make the oldStat's mode writable. Preserve the topmost part of the mode, which specifies the type | ||
// Make the oldStat's mode writable. | ||
oldStat.mode |= 0o222; | ||
@@ -147,3 +194,3 @@ return oldStat; | ||
const oldStat = new Stats(this.readable.statSync(path)); | ||
// Make the oldStat's mode writable. Preserve the topmost part of the mode, which specifies the type. | ||
// Make the oldStat's mode writable. | ||
oldStat.mode |= 0o222; | ||
@@ -159,3 +206,3 @@ return oldStat; | ||
const file = await this.readable.openFile(path, parseFlag('r')); | ||
const stats = new Stats(await file.stat()); | ||
const stats = await file.stat(); | ||
const { buffer } = await file.read(new Uint8Array(stats.size)); | ||
@@ -170,3 +217,3 @@ return new PreloadFile(this, path, flag, stats, buffer); | ||
const file = this.readable.openFileSync(path, parseFlag('r')); | ||
const stats = new Stats(file.statSync()); | ||
const stats = file.statSync(); | ||
const data = new Uint8Array(stats.size); | ||
@@ -188,2 +235,3 @@ file.readSync(data); | ||
this.checkInitialized(); | ||
await this.copyForWrite(srcpath); | ||
await this.writable.link(srcpath, dstpath); | ||
@@ -193,2 +241,3 @@ } | ||
this.checkInitialized(); | ||
this.copyForWriteSync(srcpath); | ||
this.writable.linkSync(srcpath, dstpath); | ||
@@ -232,11 +281,10 @@ } | ||
} | ||
if (await this.exists(path)) { | ||
// Check if directory is empty. | ||
if ((await this.readdir(path)).length > 0) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
else { | ||
await this.deletePath(path); | ||
} | ||
if (!(await this.exists(path))) { | ||
return; | ||
} | ||
// Check if directory is empty. | ||
if ((await this.readdir(path)).length) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
await this.deletePath(path); | ||
} | ||
@@ -251,11 +299,10 @@ rmdirSync(path) { | ||
} | ||
if (this.existsSync(path)) { | ||
// Check if directory is empty. | ||
if (this.readdirSync(path).length > 0) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
else { | ||
void this.deletePath(path); | ||
} | ||
if (!this.existsSync(path)) { | ||
return; | ||
} | ||
// Check if directory is empty. | ||
if (this.readdirSync(path).length) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
void this.deletePath(path); | ||
} | ||
@@ -418,16 +465,20 @@ async mkdir(path, mode) { | ||
*/ | ||
operateOnWritable(path) { | ||
copyForWriteSync(path) { | ||
if (!this.existsSync(path)) { | ||
throw ErrnoError.With('ENOENT', path, 'operateOnWriteable'); | ||
throw ErrnoError.With('ENOENT', path, 'copyForWrite'); | ||
} | ||
if (!this.writable.existsSync(dirname(path))) { | ||
this.createParentDirectoriesSync(path); | ||
} | ||
if (!this.writable.existsSync(path)) { | ||
// File is on readable storage. Copy to writable storage before | ||
// changing its mode. | ||
this.copyToWritableSync(path); | ||
} | ||
} | ||
async operateOnWritableAsync(path) { | ||
async copyForWrite(path) { | ||
if (!(await this.exists(path))) { | ||
throw ErrnoError.With('ENOENT', path, 'operateOnWritable'); | ||
throw ErrnoError.With('ENOENT', path, 'copyForWrite'); | ||
} | ||
if (!(await this.writable.exists(dirname(path)))) { | ||
await this.createParentDirectories(path); | ||
} | ||
if (!(await this.writable.exists(path))) { | ||
@@ -442,28 +493,46 @@ return this.copyToWritable(path); | ||
copyToWritableSync(path) { | ||
const stats = this.statSync(path); | ||
if (stats.isDirectory()) { | ||
this.writable.mkdirSync(path, stats.mode); | ||
return; | ||
const env_1 = { stack: [], error: void 0, hasError: false }; | ||
try { | ||
const stats = this.statSync(path); | ||
if (stats.isDirectory()) { | ||
this.writable.mkdirSync(path, stats.mode); | ||
return; | ||
} | ||
const data = new Uint8Array(stats.size); | ||
const readable = __addDisposableResource(env_1, this.readable.openFileSync(path, 'r'), false); | ||
readable.readSync(data); | ||
const writable = __addDisposableResource(env_1, this.writable.createFileSync(path, 'w', stats.mode | 0o222), false); | ||
writable.writeSync(data); | ||
} | ||
const data = new Uint8Array(stats.size); | ||
const readable = this.readable.openFileSync(path, parseFlag('r')); | ||
readable.readSync(data); | ||
readable.closeSync(); | ||
const writable = this.writable.openFileSync(path, parseFlag('w')); | ||
writable.writeSync(data); | ||
writable.closeSync(); | ||
catch (e_1) { | ||
env_1.error = e_1; | ||
env_1.hasError = true; | ||
} | ||
finally { | ||
__disposeResources(env_1); | ||
} | ||
} | ||
async copyToWritable(path) { | ||
const stats = await this.stat(path); | ||
if (stats.isDirectory()) { | ||
await this.writable.mkdir(path, stats.mode); | ||
return; | ||
const env_2 = { stack: [], error: void 0, hasError: false }; | ||
try { | ||
const stats = await this.stat(path); | ||
if (stats.isDirectory()) { | ||
await this.writable.mkdir(path, stats.mode); | ||
return; | ||
} | ||
const data = new Uint8Array(stats.size); | ||
const readable = __addDisposableResource(env_2, await this.readable.openFile(path, 'r'), true); | ||
await readable.read(data); | ||
const writable = __addDisposableResource(env_2, await this.writable.createFile(path, 'w', stats.mode | 0o222), true); | ||
await writable.write(data); | ||
} | ||
const data = new Uint8Array(stats.size); | ||
const readable = await this.readable.openFile(path, parseFlag('r')); | ||
await readable.read(data); | ||
await readable.close(); | ||
const writable = await this.writable.openFile(path, parseFlag('w')); | ||
await writable.write(data); | ||
await writable.close(); | ||
catch (e_2) { | ||
env_2.error = e_2; | ||
env_2.hasError = true; | ||
} | ||
finally { | ||
const result_1 = __disposeResources(env_2); | ||
if (result_1) | ||
await result_1; | ||
} | ||
} | ||
@@ -470,0 +539,0 @@ } |
{ | ||
"name": "@zenfs/core", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "A filesystem, anywhere", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -78,3 +78,3 @@ import { dirname } from '../emulation/path.js'; | ||
public async sync(path: string, data: Uint8Array, stats: Readonly<Stats>): Promise<void> { | ||
await this.createParentDirectories(path); | ||
await this.copyForWrite(path); | ||
if (!(await this.writable.exists(path))) { | ||
@@ -87,3 +87,3 @@ await this.writable.createFile(path, 'w', 0o644); | ||
public syncSync(path: string, data: Uint8Array, stats: Readonly<Stats>): void { | ||
this.createParentDirectoriesSync(path); | ||
this.copyForWriteSync(path); | ||
this.writable.syncSync(path, data, stats); | ||
@@ -131,2 +131,4 @@ } | ||
await this.copyForWrite(oldPath); | ||
try { | ||
@@ -146,2 +148,4 @@ await this.writable.rename(oldPath, newPath); | ||
this.copyForWriteSync(oldPath); | ||
try { | ||
@@ -165,3 +169,3 @@ this.writable.renameSync(oldPath, newPath); | ||
const oldStat = new Stats(await this.readable.stat(path)); | ||
// Make the oldStat's mode writable. Preserve the topmost part of the mode, which specifies the type | ||
// Make the oldStat's mode writable. | ||
oldStat.mode |= 0o222; | ||
@@ -181,3 +185,3 @@ return oldStat; | ||
const oldStat = new Stats(this.readable.statSync(path)); | ||
// Make the oldStat's mode writable. Preserve the topmost part of the mode, which specifies the type. | ||
// Make the oldStat's mode writable. | ||
oldStat.mode |= 0o222; | ||
@@ -194,3 +198,3 @@ return oldStat; | ||
const file = await this.readable.openFile(path, parseFlag('r')); | ||
const stats = new Stats(await file.stat()); | ||
const stats = await file.stat(); | ||
const { buffer } = await file.read(new Uint8Array(stats.size)); | ||
@@ -206,3 +210,3 @@ return new PreloadFile(this, path, flag, stats, buffer); | ||
const file = this.readable.openFileSync(path, parseFlag('r')); | ||
const stats = new Stats(file.statSync()); | ||
const stats = file.statSync(); | ||
const data = new Uint8Array(stats.size); | ||
@@ -227,2 +231,3 @@ file.readSync(data); | ||
this.checkInitialized(); | ||
await this.copyForWrite(srcpath); | ||
await this.writable.link(srcpath, dstpath); | ||
@@ -233,2 +238,3 @@ } | ||
this.checkInitialized(); | ||
this.copyForWriteSync(srcpath); | ||
this.writable.linkSync(srcpath, dstpath); | ||
@@ -279,10 +285,10 @@ } | ||
} | ||
if (await this.exists(path)) { | ||
// Check if directory is empty. | ||
if ((await this.readdir(path)).length > 0) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} else { | ||
await this.deletePath(path); | ||
} | ||
if (!(await this.exists(path))) { | ||
return; | ||
} | ||
// Check if directory is empty. | ||
if ((await this.readdir(path)).length) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
await this.deletePath(path); | ||
} | ||
@@ -298,10 +304,10 @@ | ||
} | ||
if (this.existsSync(path)) { | ||
// Check if directory is empty. | ||
if (this.readdirSync(path).length > 0) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} else { | ||
void this.deletePath(path); | ||
} | ||
if (!this.existsSync(path)) { | ||
return; | ||
} | ||
// Check if directory is empty. | ||
if (this.readdirSync(path).length) { | ||
throw ErrnoError.With('ENOTEMPTY', path, 'rmdir'); | ||
} | ||
void this.deletePath(path); | ||
} | ||
@@ -478,9 +484,10 @@ | ||
*/ | ||
private operateOnWritable(path: string): void { | ||
private copyForWriteSync(path: string): void { | ||
if (!this.existsSync(path)) { | ||
throw ErrnoError.With('ENOENT', path, 'operateOnWriteable'); | ||
throw ErrnoError.With('ENOENT', path, 'copyForWrite'); | ||
} | ||
if (!this.writable.existsSync(dirname(path))) { | ||
this.createParentDirectoriesSync(path); | ||
} | ||
if (!this.writable.existsSync(path)) { | ||
// File is on readable storage. Copy to writable storage before | ||
// changing its mode. | ||
this.copyToWritableSync(path); | ||
@@ -490,7 +497,11 @@ } | ||
private async operateOnWritableAsync(path: string): Promise<void> { | ||
private async copyForWrite(path: string): Promise<void> { | ||
if (!(await this.exists(path))) { | ||
throw ErrnoError.With('ENOENT', path, 'operateOnWritable'); | ||
throw ErrnoError.With('ENOENT', path, 'copyForWrite'); | ||
} | ||
if (!(await this.writable.exists(dirname(path)))) { | ||
await this.createParentDirectories(path); | ||
} | ||
if (!(await this.writable.exists(path))) { | ||
@@ -513,8 +524,6 @@ return this.copyToWritable(path); | ||
const data = new Uint8Array(stats.size); | ||
const readable = this.readable.openFileSync(path, parseFlag('r')); | ||
using readable = this.readable.openFileSync(path, 'r'); | ||
readable.readSync(data); | ||
readable.closeSync(); | ||
const writable = this.writable.openFileSync(path, parseFlag('w')); | ||
using writable = this.writable.createFileSync(path, 'w', stats.mode | 0o222); | ||
writable.writeSync(data); | ||
writable.closeSync(); | ||
} | ||
@@ -530,8 +539,6 @@ | ||
const data = new Uint8Array(stats.size); | ||
const readable = await this.readable.openFile(path, parseFlag('r')); | ||
await using readable = await this.readable.openFile(path, 'r'); | ||
await readable.read(data); | ||
await readable.close(); | ||
const writable = await this.writable.openFile(path, parseFlag('w')); | ||
await using writable = await this.writable.createFile(path, 'w', stats.mode | 0o222); | ||
await writable.write(data); | ||
await writable.close(); | ||
} | ||
@@ -538,0 +545,0 @@ } |
746188
21239