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

FileStore now recreates parent cache folders if a write fails #370

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/metro-cache/src/stores/FileStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,29 @@ class FileStore<T> {
}

set(key: Buffer, value: T): void {
const filePath = this._getFilePath(key);
try {
this._set(filePath, value);
} catch (err) {
if (err.code === 'ENOENT') {
mkdirp.sync(path.dirname(filePath));
this._set(filePath, value);
} else {
throw err;
}
}
}

_set(filePath: string, value: T): void {
if (value instanceof Buffer) {
const fd = fs.openSync(this._getFilePath(key), 'w');
const fd = fs.openSync(filePath, 'w');

fs.writeSync(fd, NULL_BYTE_BUFFER);
fs.writeSync(fd, value);

fs.closeSync(fd);
} else {
fs.writeFileSync(this._getFilePath(key), JSON.stringify(value));
fs.writeFileSync(filePath, JSON.stringify(value));
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/metro-cache/src/stores/__tests__/FileStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ describe('FileStore', () => {
expect(fileStore.get(cache)).toEqual(null);
});

it('writes into cache if folder is missing', () => {
const fileStore = new FileStore({root: '/root'});
const cache = Buffer.from([0xfa, 0xce, 0xb0, 0x0c]);
const data = Buffer.from([0xca, 0xc4, 0xe5]);

require('rimraf').sync('/root');
fileStore.set(cache, data);
expect(fileStore.get(cache)).toEqual(data);
});

it('reads and writes binary data', () => {
const fileStore = new FileStore({root: '/root'});
const cache = Buffer.from([0xfa, 0xce, 0xb0, 0x0c]);
Expand Down
17 changes: 17 additions & 0 deletions packages/metro-memory-fs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,23 @@ class MemoryFs {
dirNode.entries.set(basename, this._makeDir(mode));
};

rmdirSync = (dirPath: string | Buffer): void => {
dirPath = pathStr(dirPath);
const {dirNode, node, basename} = this._resolve(dirPath);
if (node == null) {
throw makeError('ENOENT', dirPath, 'directory does not exist');
} else if (node.type === 'file') {
if (this._platform === 'posix') {
throw makeError('ENOTDIR', dirPath, 'cannot rm a file');
} else {
throw makeError('ENOENT', dirPath, 'cannot rm a file');
}
} else if (node.type === 'directory' && node.entries.size) {
throw makeError('ENOTEMPTY', dirPath, 'directory not empty');
}
dirNode.entries.delete(basename);
};

symlinkSync = (
target: string | Buffer,
filePath: FilePath,
Expand Down