Skip to content

Commit 72a6259

Browse files
committed
write to a unique-ish file and then rename at the end to avoid clashes
1 parent 5c2c4c7 commit 72a6259

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/diff.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,12 @@ int make_bsdiff_delta(char *old_filename, char *new_filename, char *delta_filena
396396
off_t first_block;
397397
int c_enc, d_enc, e_enc;
398398
enc_flags_t encodings;
399+
char delta_filename_unique[2 * PATH_MAX];
399400

400401
struct header_v20 large_header;
401402
struct header_v21 small_header;
403+
404+
sprintf(delta_filename_unique, "%s.%i", delta_filename, getpid());
402405
FILE *pf;
403406

404407
ret = lstat(old_filename, &old_stat);
@@ -411,6 +414,8 @@ int make_bsdiff_delta(char *old_filename, char *new_filename, char *delta_filena
411414
return -1;
412415
}
413416

417+
ret = 0;
418+
414419
if (S_ISDIR(new_stat.st_mode) || S_ISDIR(old_stat.st_mode)) {
415420
/* no delta on symlinks ! */
416421
return -1;
@@ -790,7 +795,7 @@ int make_bsdiff_delta(char *old_filename, char *new_filename, char *delta_filena
790795

791796
/* Create the patch file */
792797

793-
efd = open(delta_filename, O_CREAT | O_EXCL | O_WRONLY, 00644);
798+
efd = open(delta_filename_unique, O_CREAT | O_EXCL | O_WRONLY, 00644);
794799
if (efd < 0) {
795800
ret = -1;
796801
goto fulldl_free;
@@ -945,6 +950,7 @@ int make_bsdiff_delta(char *old_filename, char *new_filename, char *delta_filena
945950
if (fclose(pf)) {
946951
ret = -1;
947952
}
953+
rename(delta_filename_unique, delta_filename);
948954
fulldl_free:
949955
/* Free the memory we used */
950956
munmap(old_data, old_size);

0 commit comments

Comments
 (0)