Skip to content

Commit 9039f77

Browse files
committed
project file upload (to compute server): fall back to copy if move didn't work in all cases
1 parent 2729c01 commit 9039f77

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/packages/project/upload.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,49 +166,66 @@ async function handle_chunk_data(
166166
compute_server_id: number,
167167
): Promise<void> {
168168
if (index === 0) {
169-
// move chunk to the temp file
169+
logger.debug("handle_chunk_data: move chunk to the temp file");
170170
await moveFile(chunk, temp);
171171
} else {
172-
// append chunk to the temp file
172+
logger.debug("handle_chunk_data: append chunk to the temp file");
173173
const data = await readFile(chunk);
174174
await appendFile(temp, data);
175175
await unlink(chunk);
176176
}
177-
// if it's the last chunk, move temp to actual file.
178177
if (index === total - 1) {
178+
logger.debug(
179+
"handle_chunk_data: it's the last chunk, move temp to actual file.",
180+
);
179181
await moveFile(temp, dest, dest_dir, compute_server_id);
180182
}
181183
}
182184

183185
async function moveFile(
184-
src: string,
186+
temp: string,
185187
dest: string,
186188
dest_dir?: string,
187189
compute_server_id?: number,
188190
): Promise<void> {
189191
try {
192+
logger.debug("move temporary file to dest", {
193+
temp,
194+
dest,
195+
});
196+
try {
197+
await rename(temp, dest);
198+
} catch (_) {
199+
// in some cases, e.g., cocalc-docker, this happens:
200+
// "EXDEV: cross-device link not permitted"
201+
// so we just try again the slower way. This is slightly
202+
// inefficient, maybe, but more robust than trying to determine
203+
// if we are doing a cross device rename.
204+
await copyFile(temp, dest);
205+
}
206+
190207
if (compute_server_id) {
191208
// The final destination of this file upload is a compute server.
192-
// We copy the temp file (src) to the compute server, then remove
209+
// We copy the temp file (temp) to the compute server, then remove
193210
// the temp file.
194211
// TODO: it would obviously be much more efficient to upload directly
195212
// to the compute server without going through cocalc at all. For
196213
// various reasons that is simply impossible in general, unfortunately.
197-
logger.debug("move temporary file to compute server", {
198-
src,
214+
logger.debug("moveFile: move temporary file to compute server", {
215+
temp,
199216
dest,
200217
dest_dir,
201218
compute_server_id,
202219
});
203220

204221
// input to handleCopy must be relative to home directory,
205-
// but src and dest are absolute paths got by putting HOME
222+
// but temp and dest are absolute paths got by putting HOME
206223
// in the front of them:
207224
const { HOME } = process.env;
208225
if (!HOME) {
209226
throw Error("HOME env var must be set");
210227
}
211-
await rename(src, dest);
228+
await rename(temp, dest);
212229
await handleCopy({
213230
event: "copy_from_project_to_compute_server",
214231
compute_server_id,
@@ -217,24 +234,9 @@ async function moveFile(
217234
});
218235
return;
219236
}
220-
221-
logger.debug("move temporary file to dest", {
222-
src,
223-
dest,
224-
});
225-
try {
226-
await rename(src, dest);
227-
} catch (_) {
228-
// in some cases, e.g., cocalc-docker, this happens:
229-
// "EXDEV: cross-device link not permitted"
230-
// so we just try again the slower way. This is slightly
231-
// inefficient, maybe, but more robust than trying to determine
232-
// if we are doing a cross device rename.
233-
await copyFile(src, dest);
234-
}
235237
} finally {
236238
try {
237-
await unlink(src);
239+
await unlink(temp);
238240
} catch (_) {}
239241
}
240242
}

0 commit comments

Comments
 (0)