@@ -166,49 +166,66 @@ async function handle_chunk_data(
166
166
compute_server_id : number ,
167
167
) : Promise < void > {
168
168
if ( index === 0 ) {
169
- // move chunk to the temp file
169
+ logger . debug ( "handle_chunk_data: move chunk to the temp file" ) ;
170
170
await moveFile ( chunk , temp ) ;
171
171
} else {
172
- // append chunk to the temp file
172
+ logger . debug ( "handle_chunk_data: append chunk to the temp file" ) ;
173
173
const data = await readFile ( chunk ) ;
174
174
await appendFile ( temp , data ) ;
175
175
await unlink ( chunk ) ;
176
176
}
177
- // if it's the last chunk, move temp to actual file.
178
177
if ( index === total - 1 ) {
178
+ logger . debug (
179
+ "handle_chunk_data: it's the last chunk, move temp to actual file." ,
180
+ ) ;
179
181
await moveFile ( temp , dest , dest_dir , compute_server_id ) ;
180
182
}
181
183
}
182
184
183
185
async function moveFile (
184
- src : string ,
186
+ temp : string ,
185
187
dest : string ,
186
188
dest_dir ?: string ,
187
189
compute_server_id ?: number ,
188
190
) : Promise < void > {
189
191
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
+
190
207
if ( compute_server_id ) {
191
208
// 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
193
210
// the temp file.
194
211
// TODO: it would obviously be much more efficient to upload directly
195
212
// to the compute server without going through cocalc at all. For
196
213
// 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 ,
199
216
dest,
200
217
dest_dir,
201
218
compute_server_id,
202
219
} ) ;
203
220
204
221
// 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
206
223
// in the front of them:
207
224
const { HOME } = process . env ;
208
225
if ( ! HOME ) {
209
226
throw Error ( "HOME env var must be set" ) ;
210
227
}
211
- await rename ( src , dest ) ;
228
+ await rename ( temp , dest ) ;
212
229
await handleCopy ( {
213
230
event : "copy_from_project_to_compute_server" ,
214
231
compute_server_id,
@@ -217,24 +234,9 @@ async function moveFile(
217
234
} ) ;
218
235
return ;
219
236
}
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
- }
235
237
} finally {
236
238
try {
237
- await unlink ( src ) ;
239
+ await unlink ( temp ) ;
238
240
} catch ( _ ) { }
239
241
}
240
242
}
0 commit comments