Skip to content

Commit

Permalink
fix: readdir and readdirSync failing on valid empty directory if …
Browse files Browse the repository at this point in the history
…another fs fails
  • Loading branch information
dy-dx committed Mar 19, 2024
1 parent 40c188a commit d36321b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/__tests__/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ describe('union', () => {
expect(ufs.readdirSync('/bar')).toEqual(['baz', 'qux']);
});

// regression test for https://github.com/streamich/unionfs/issues/782
it('does not throw error when directory is empty and other fs fails', () => {
const vol = Volume.fromJSON({});
const vol2 = Volume.fromJSON({
'/bar': null,
});

const ufs = new Union();
ufs.use(vol as any);
ufs.use(vol2 as any);
expect(ufs.readdirSync('/bar')).toEqual([]);
});

it('honors the withFileTypes: true option', () => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
Expand Down Expand Up @@ -308,6 +321,23 @@ describe('union', () => {
});
});

// regression test for https://github.com/streamich/unionfs/issues/782
it('does not throw error when directory is empty and other fs fails', done => {
const vol = Volume.fromJSON({});
const vol2 = Volume.fromJSON({
'/bar': null,
});

const ufs = new Union();
ufs.use(vol as any);
ufs.use(vol2 as any);
ufs.readdir('/bar', (err, files) => {
expect(err).toBeNull();
expect(files).toEqual([]);
done();
});
});

it('honors the withFileTypes: true option', done => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
Expand Down
24 changes: 12 additions & 12 deletions src/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,39 +176,37 @@ export class Union {
lastarg++;
}

let numErrors = 0;
let lastError: IUnionFsError | null = null;
let result = new Map<string, readdirEntry>();
const iterate = (i = 0, error?: IUnionFsError | null) => {
if (error) {
error.prev = lastError;
lastError = error;
numErrors++;
}

// Already tried all file systems, return the last error.
// Already tried all file systems
if (i >= this.fss.length) {
// last one
if (cb) {
// If any previous file system succeeded, don't throw an error.
if (numErrors < this.fss.length) {
return cb(null, this.sortedArrayFromReaddirResult(result));
}
cb(error || Error('No file systems attached.'));
}
return;
}

// Replace `callback` with our intermediate function.
args[lastarg] = (err, resArg: readdirEntry[]) => {
if (result.size === 0 && err) {
return iterate(i + 1, err);
}
if (resArg) {
for (const res of resArg) {
result.set(this.pathFromReaddirEntry(res), res);
}
}

if (i === this.fss.length - 1) {
return cb(null, this.sortedArrayFromReaddirResult(result));
} else {
return iterate(i + 1, error);
}
return iterate(i + 1, err);
};

const j = this.fss.length - i - 1;
Expand All @@ -222,6 +220,7 @@ export class Union {
};

public readdirSync = (...args): Array<readdirEntry> => {
let numErrors = 0;
let lastError: IUnionFsError | null = null;
let result = new Map<string, readdirEntry>();
for (let i = this.fss.length - 1; i >= 0; i--) {
Expand All @@ -234,8 +233,9 @@ export class Union {
} catch (err) {
err.prev = lastError;
lastError = err;
if (result.size === 0 && !i) {
// last one
numErrors++;
if (numErrors === this.fss.length) {
// all filesystems errored
throw err;
} else {
// Ignore error...
Expand Down

0 comments on commit d36321b

Please sign in to comment.