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>
Daniel Barkalow committed
Feb 25, 2008 at 14:24 UTC
1468bd47833c6ec3c85620d6af1d910e9378f714
5 files changed
+27
-43
builtin-init-db.c
-21
@@ -29,27 +29,6 @@ static void safe_create_dir(const char *dir, int share)
29
die("Could not make %s writable by group\n", dir);
30
}
31
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
-
32
static void copy_templates_1(char *path, int baselen,
33
char *template, int template_baselen,
34
DIR *dir)
builtin-rerere.c
+1
-18
@@ -267,23 +267,6 @@ static int diff_two(const char *file1, const char *label1,
267
return 0;
268
}
269
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
-
270
static int do_plain_rerere(struct path_list *rr, int fd)
271
{
272
struct path_list conflict = { NULL, 0, 0, 1 };
@@ -343,7 +326,7 @@ static int do_plain_rerere(struct path_list *rr, int fd)
326
continue;
327
328
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);
330
tail_optimization:
331
if (i < rr->nr - 1)
332
memmove(rr->items + i,
cache.h
+1
@@ -602,6 +602,7 @@ extern const char *git_log_output_encoding;
602
/* IO helper functions */
603
extern void maybe_flush_or_die(FILE *, const char *);
604
extern int copy_fd(int ifd, int ofd);
605
+extern int copy_file(const char *dst, const char *src, int mode);
606
extern int read_in_full(int fd, void *buf, size_t count);
607
extern int write_in_full(int fd, const void *buf, size_t count);
608
extern void write_or_die(int fd, const void *buf, size_t count);
copy.c
+21
@@ -34,3 +34,24 @@ int copy_fd(int ifd, int ofd)
34
close(ifd);
35
return 0;
36
}
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
+4
-4
@@ -272,8 +272,8 @@ static void print_line_count(int count)
272
}
273
}
274
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)
277
{
278
int ch, nl_just_seen = 1;
279
while (0 < size--) {
@@ -331,9 +331,9 @@ static void emit_rewrite_diff(const char *name_a,
331
print_line_count(lc_b);
332
printf(" @@%s\n", reset);
333
if (lc_a)
334
- copy_file('-', one->data, one->size, old, reset);
334
+ copy_file_with_prefix('-', one->data, one->size, old, reset);
335
if (lc_b)
336
- copy_file('+', two->data, two->size, new, reset);
336
+ copy_file_with_prefix('+', two->data, two->size, new, reset);
337
}
338
339
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)