Skip to content

Commit 1468bd4

Browse files
iabervongitster
authored andcommitted
Use a single implementation and API for copy_file()
Originally by Kristian Hᅵgsberg; I fixed the conversion of rerere, which had a different API. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ed10d9a commit 1468bd4

File tree

5 files changed

+27
-43
lines changed

5 files changed

+27
-43
lines changed

builtin-init-db.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,6 @@ static void safe_create_dir(const char *dir, int share)
2929
die("Could not make %s writable by group\n", dir);
3030
}
3131

32-
static int copy_file(const char *dst, const char *src, int mode)
33-
{
34-
int fdi, fdo, status;
35-
36-
mode = (mode & 0111) ? 0777 : 0666;
37-
if ((fdi = open(src, O_RDONLY)) < 0)
38-
return fdi;
39-
if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
40-
close(fdi);
41-
return fdo;
42-
}
43-
status = copy_fd(fdi, fdo);
44-
if (close(fdo) != 0)
45-
return error("%s: write error: %s", dst, strerror(errno));
46-
47-
if (!status && adjust_shared_perm(dst))
48-
return -1;
49-
50-
return status;
51-
}
52-
5332
static void copy_templates_1(char *path, int baselen,
5433
char *template, int template_baselen,
5534
DIR *dir)

builtin-rerere.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,6 @@ static int diff_two(const char *file1, const char *label1,
267267
return 0;
268268
}
269269

270-
static int copy_file(const char *src, const char *dest)
271-
{
272-
FILE *in, *out;
273-
char buffer[32768];
274-
int count;
275-
276-
if (!(in = fopen(src, "r")))
277-
return error("Could not open %s", src);
278-
if (!(out = fopen(dest, "w")))
279-
return error("Could not open %s", dest);
280-
while ((count = fread(buffer, 1, sizeof(buffer), in)))
281-
fwrite(buffer, 1, count, out);
282-
fclose(in);
283-
fclose(out);
284-
return 0;
285-
}
286-
287270
static int do_plain_rerere(struct path_list *rr, int fd)
288271
{
289272
struct path_list conflict = { NULL, 0, 0, 1 };
@@ -343,7 +326,7 @@ static int do_plain_rerere(struct path_list *rr, int fd)
343326
continue;
344327

345328
fprintf(stderr, "Recorded resolution for '%s'.\n", path);
346-
copy_file(path, rr_path(name, "postimage"));
329+
copy_file(rr_path(name, "postimage"), path, 0666);
347330
tail_optimization:
348331
if (i < rr->nr - 1)
349332
memmove(rr->items + i,

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ extern const char *git_log_output_encoding;
602602
/* IO helper functions */
603603
extern void maybe_flush_or_die(FILE *, const char *);
604604
extern int copy_fd(int ifd, int ofd);
605+
extern int copy_file(const char *dst, const char *src, int mode);
605606
extern int read_in_full(int fd, void *buf, size_t count);
606607
extern int write_in_full(int fd, const void *buf, size_t count);
607608
extern void write_or_die(int fd, const void *buf, size_t count);

copy.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ int copy_fd(int ifd, int ofd)
3434
close(ifd);
3535
return 0;
3636
}
37+
38+
int copy_file(const char *dst, const char *src, int mode)
39+
{
40+
int fdi, fdo, status;
41+
42+
mode = (mode & 0111) ? 0777 : 0666;
43+
if ((fdi = open(src, O_RDONLY)) < 0)
44+
return fdi;
45+
if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
46+
close(fdi);
47+
return fdo;
48+
}
49+
status = copy_fd(fdi, fdo);
50+
if (close(fdo) != 0)
51+
return error("%s: write error: %s", dst, strerror(errno));
52+
53+
if (!status && adjust_shared_perm(dst))
54+
return -1;
55+
56+
return status;
57+
}

diff.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ static void print_line_count(int count)
272272
}
273273
}
274274

275-
static void copy_file(int prefix, const char *data, int size,
276-
const char *set, const char *reset)
275+
static void copy_file_with_prefix(int prefix, const char *data, int size,
276+
const char *set, const char *reset)
277277
{
278278
int ch, nl_just_seen = 1;
279279
while (0 < size--) {
@@ -331,9 +331,9 @@ static void emit_rewrite_diff(const char *name_a,
331331
print_line_count(lc_b);
332332
printf(" @@%s\n", reset);
333333
if (lc_a)
334-
copy_file('-', one->data, one->size, old, reset);
334+
copy_file_with_prefix('-', one->data, one->size, old, reset);
335335
if (lc_b)
336-
copy_file('+', two->data, two->size, new, reset);
336+
copy_file_with_prefix('+', two->data, two->size, new, reset);
337337
}
338338

339339
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)

0 commit comments

Comments
 (0)