Skip to content

Commit c26fb9a

Browse files
committed
USB sync: make sure destination is writable
Change-Id: I9be82cac6000ba8683ef46d9d86331bee8f7821f
1 parent 9dc2f40 commit c26fb9a

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

assets/js/sync-to-drive.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,23 @@ class SyncToDrive {
9696
throw new Error('Error: USB device is not properly mounted.');
9797
}
9898

99-
log('Starting sync to USB drive ...');
100-
const distinationPath = path.join(device.mountpoint, this.destination);
101-
if (!fs.existsSync(distinationPath)) {
102-
log('Creating target directory' + distinationPath);
103-
fs.mkdirSync(distinationPath, { recursive: true });
99+
const destinationPath = path.join(device.mountpoint, this.destination);
100+
if (!fs.existsSync(destinationPath)) {
101+
log(`Creating target directory ${destinationPath}`);
102+
try {
103+
fs.mkdirSync(destinationPath, { recursive: true });
104+
} catch (err) {
105+
throw new Error(`Error: Failed to create directory ${destinationPath} - ${err.message}`);
106+
}
107+
}
108+
109+
if (!this.isWritable(destinationPath)) {
110+
throw new Error(`Error: Destination ${destinationPath} is not writable.`);
104111
}
105112

113+
log('Starting sync to USB drive ...');
106114
log('Source data folder ' + this.source);
107-
log('Syncing to drive ' + device.path + ' -> ' + distinationPath);
115+
log('Syncing to drive ' + device.path + ' -> ' + destinationPath);
108116

109117
const command = [
110118
'rsync',
@@ -242,6 +250,16 @@ class SyncToDrive {
242250
this.stop();
243251
}
244252

253+
isWritable(directory) {
254+
try {
255+
fs.accessSync(directory, fs.constants.W_OK);
256+
return true;
257+
// eslint-disable-next-line no-unused-vars
258+
} catch (err) {
259+
return false;
260+
}
261+
}
262+
245263
fetchConfig() {
246264
try {
247265
const cmd = 'bin/photobooth photobooth:config:list json';

0 commit comments

Comments
 (0)