Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: readdir and readdirSync failing on valid empty directory if another fs fails #787

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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