@matheusbsbs / matheus-bagliano / commits / 691f1a28bf
Fork 0

replace direct calls to unlink(2) with unlink_or_warn

This helps to notice when something's going wrong, especially on systems which lock open files. I used the following criteria when selecting the code for replacement: - it was already printing a warning for the unlink failures - it is in a function which already printing something or is called from such a function - it is in a static function, returning void and the function is only called from a builtin main function (cmd_) - it is in a function which handles emergency exit (signal handlers) - it is in a function which is obvously cleaning up the lockfiles Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alex Riesen committed Apr 29, 2009 at 23:22 UTC 691f1a28bf57618d8b44a193b1d28013c858aba6
23 files changed +44 -49
builtin-apply.c
+2 -2
@@ -2781,7 +2781,7 @@ static void remove_file(struct patch *patch, int rmdir_empty)
2781 if (rmdir(patch->old_name))
2782 warning("unable to remove submodule %s",
2783 patch->old_name);
2784 - } else if (!unlink(patch->old_name) && rmdir_empty) {
2784 + } else if (!unlink_or_warn(patch->old_name) && rmdir_empty) {
2785 remove_path(patch->old_name);
2786 }
2787 }
@@ -2891,7 +2891,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
2891 if (!try_create_file(newpath, mode, buf, size)) {
2892 if (!rename(newpath, path))
2893 return;
2894 - unlink(newpath);
2894 + unlink_or_warn(newpath);
2895 break;
2896 }
2897 if (errno != EEXIST)
builtin-fetch-pack.c
+1 -1
@@ -814,7 +814,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
814 fd = hold_lock_file_for_update(&lock, shallow,
815 LOCK_DIE_ON_ERROR);
816 if (!write_shallow_commits(fd, 0)) {
817 - unlink(shallow);
817 + unlink_or_warn(shallow);
818 rollback_lock_file(&lock);
819 } else {
820 commit_lock_file(&lock);
builtin-prune-packed.c
+2 -2
@@ -28,8 +28,8 @@ static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
28 memcpy(pathname + len, de->d_name, 38);
29 if (opts & DRY_RUN)
30 printf("rm -f %s\n", pathname);
31 - else if (unlink(pathname) < 0)
32 - error("unable to unlink %s", pathname);
31 + else
32 + unlink_or_warn(pathname);
33 display_progress(progress, i + 1);
34 }
35 pathname[len] = 0;
builtin-prune.c
+2 -2
@@ -27,7 +27,7 @@ static int prune_tmp_object(const char *path, const char *filename)
27 }
28 printf("Removing stale temporary file %s\n", fullpath);
29 if (!show_only)
30 - unlink(fullpath);
30 + unlink_or_warn(fullpath);
31 return 0;
32 }
33
@@ -47,7 +47,7 @@ static int prune_object(char *path, const char *filename, const unsigned char *s
47 (type > 0) ? typename(type) : "unknown");
48 }
49 if (!show_only)
50 - unlink(fullpath);
50 + unlink_or_warn(fullpath);
51 return 0;
52 }
53
builtin-receive-pack.c
+1 -1
@@ -702,7 +702,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
702 unpack_status = unpack();
703 execute_commands(unpack_status);
704 if (pack_lockfile)
705 - unlink(pack_lockfile);
705 + unlink_or_warn(pack_lockfile);
706 if (report_status)
707 report(unpack_status);
708 run_receive_hook(post_receive_hook);
builtin-remote.c
+2 -2
@@ -525,8 +525,8 @@ static int migrate_file(struct remote *remote)
525 path = git_path("remotes/%s", remote->name);
526 else if (remote->origin == REMOTE_BRANCHES)
527 path = git_path("branches/%s", remote->name);
528 - if (path && unlink(path))
529 - warning("failed to remove '%s'", path);
528 + if (path)
529 + unlink_or_warn(path);
530 return 0;
531 }
532
builtin-rerere.c
+1 -1
@@ -116,7 +116,7 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
116 if (!has_rerere_resolution(name))
117 unlink_rr_item(name);
118 }
119 - unlink(git_path("rr-cache/MERGE_RR"));
119 + unlink_or_warn(git_path("rr-cache/MERGE_RR"));
120 } else if (!strcmp(argv[1], "gc"))
121 garbage_collect(&merge_rr);
122 else if (!strcmp(argv[1], "status"))
builtin-tag.c
+1 -1
@@ -338,7 +338,7 @@ static void create_tag(const unsigned char *object, const char *tag,
338 exit(128);
339 }
340 if (path) {
341 - unlink(path);
341 + unlink_or_warn(path);
342 free(path);
343 }
344 }
builtin-verify-tag.c
+1 -1
@@ -55,7 +55,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
55 close(gpg.in);
56 ret = finish_command(&gpg);
57
58 - unlink(path);
58 + unlink_or_warn(path);
59
60 return ret;
61 }
diff.c
+1 -1
@@ -189,7 +189,7 @@ static void remove_tempfile(void)
189 int i;
190 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
191 if (diff_temp[i].name == diff_temp[i].tmp_path)
192 - unlink(diff_temp[i].name);
192 + unlink_or_warn(diff_temp[i].name);
193 diff_temp[i].name = NULL;
194 }
195 }
entry.c
+1 -1
@@ -35,7 +35,7 @@ static void create_directories(const char *path, int path_len,
35 */
36 if (mkdir(buf, 0777)) {
37 if (errno == EEXIST && state->force &&
38 - !unlink(buf) && !mkdir(buf, 0777))
38 + !unlink_or_warn(buf) && !mkdir(buf, 0777))
39 continue;
40 die("cannot create directory at %s", buf);
41 }
fast-import.c
+2 -2
@@ -931,7 +931,7 @@ static void unkeep_all_packs(void)
931 struct packed_git *p = all_packs[k];
932 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
933 get_object_directory(), sha1_to_hex(p->sha1));
934 - unlink(name);
934 + unlink_or_warn(name);
935 }
936 }
937
@@ -981,7 +981,7 @@ static void end_packfile(void)
981 }
982 else {
983 close(old_p->pack_fd);
984 - unlink(old_p->pack_name);
984 + unlink_or_warn(old_p->pack_name);
985 }
986 free(old_p);
987
http-push.c
+6 -6
@@ -315,9 +315,9 @@ static void start_fetch_loose(struct transfer_request *request)
315 "%s.temp", filename);
316
317 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
318 - unlink(prevfile);
318 + unlink_or_warn(prevfile);
319 rename(request->tmpfile, prevfile);
320 - unlink(request->tmpfile);
320 + unlink_or_warn(request->tmpfile);
321
322 if (request->local_fileno != -1)
323 error("fd leakage in start: %d", request->local_fileno);
@@ -372,7 +372,7 @@ static void start_fetch_loose(struct transfer_request *request)
372 } while (prev_read > 0);
373 close(prevlocal);
374 }
375 - unlink(prevfile);
375 + unlink_or_warn(prevfile);
376
377 /* Reset inflate/SHA1 if there was an error reading the previous temp
378 file; also rewind to the beginning of the local file. */
@@ -784,7 +784,7 @@ static void finish_request(struct transfer_request *request)
784 request->http_code != 416) {
785 if (stat(request->tmpfile, &st) == 0) {
786 if (st.st_size == 0)
787 - unlink(request->tmpfile);
787 + unlink_or_warn(request->tmpfile);
788 }
789 } else {
790 if (request->http_code == 416)
@@ -793,9 +793,9 @@ static void finish_request(struct transfer_request *request)
793 git_inflate_end(&request->stream);
794 git_SHA1_Final(request->real_sha1, &request->c);
795 if (request->zret != Z_STREAM_END) {
796 - unlink(request->tmpfile);
796 + unlink_or_warn(request->tmpfile);
797 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
798 - unlink(request->tmpfile);
798 + unlink_or_warn(request->tmpfile);
799 } else {
800 request->rename =
801 move_temp_to_file(
http-walker.c
+7 -7
@@ -111,9 +111,9 @@ static void start_object_request(struct walker *walker,
111 struct walker_data *data = walker->data;
112
113 snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
114 - unlink(prevfile);
114 + unlink_or_warn(prevfile);
115 rename(obj_req->tmpfile, prevfile);
116 - unlink(obj_req->tmpfile);
116 + unlink_or_warn(obj_req->tmpfile);
117
118 if (obj_req->local != -1)
119 error("fd leakage in start: %d", obj_req->local);
@@ -177,7 +177,7 @@ static void start_object_request(struct walker *walker,
177 } while (prev_read > 0);
178 close(prevlocal);
179 }
180 - unlink(prevfile);
180 + unlink_or_warn(prevfile);
181
182 /* Reset inflate/SHA1 if there was an error reading the previous temp
183 file; also rewind to the beginning of the local file. */
@@ -238,18 +238,18 @@ static void finish_object_request(struct object_request *obj_req)
238 } else if (obj_req->curl_result != CURLE_OK) {
239 if (stat(obj_req->tmpfile, &st) == 0)
240 if (st.st_size == 0)
241 - unlink(obj_req->tmpfile);
241 + unlink_or_warn(obj_req->tmpfile);
242 return;
243 }
244
245 git_inflate_end(&obj_req->stream);
246 git_SHA1_Final(obj_req->real_sha1, &obj_req->c);
247 if (obj_req->zret != Z_STREAM_END) {
248 - unlink(obj_req->tmpfile);
248 + unlink_or_warn(obj_req->tmpfile);
249 return;
250 }
251 if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
252 - unlink(obj_req->tmpfile);
252 + unlink_or_warn(obj_req->tmpfile);
253 return;
254 }
255 obj_req->rename =
@@ -809,7 +809,7 @@ static void abort_object_request(struct object_request *obj_req)
809 close(obj_req->local);
810 obj_req->local = -1;
811 }
812 - unlink(obj_req->tmpfile);
812 + unlink_or_warn(obj_req->tmpfile);
813 if (obj_req->slot) {
814 release_active_slot(obj_req->slot);
815 obj_req->slot = NULL;
ll-merge.c
+1 -1
@@ -219,7 +219,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
219 close(fd);
220 bad:
221 for (i = 0; i < 3; i++)
222 - unlink(temp[i]);
222 + unlink_or_warn(temp[i]);
223 strbuf_release(&cmd);
224 return status;
225 }
lockfile.c
+2 -2
@@ -16,7 +16,7 @@ static void remove_lock_file(void)
16 lock_file_list->filename[0]) {
17 if (lock_file_list->fd >= 0)
18 close(lock_file_list->fd);
19 - unlink(lock_file_list->filename);
19 + unlink_or_warn(lock_file_list->filename);
20 }
21 lock_file_list = lock_file_list->next;
22 }
@@ -259,7 +259,7 @@ void rollback_lock_file(struct lock_file *lk)
259 if (lk->filename[0]) {
260 if (lk->fd >= 0)
261 close(lk->fd);
262 - unlink(lk->filename);
262 + unlink_or_warn(lk->filename);
263 }
264 lk->filename[0] = 0;
265 }
pack-refs.c
+1 -1
@@ -66,7 +66,7 @@ static void prune_ref(struct ref_to_prune *r)
66 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
67
68 if (lock) {
69 - unlink(git_path("%s", r->name));
69 + unlink_or_warn(git_path("%s", r->name));
70 unlock_ref(lock);
71 }
72 }
refs.c
+5 -10
@@ -1002,12 +1002,10 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
1002 } else {
1003 path = git_path("%s", refname);
1004 }
1005 - err = unlink(path);
1006 - if (err && errno != ENOENT) {
1005 + err = unlink_or_warn(path);
1006 + if (err && errno != ENOENT)
1007 ret = 1;
1008 - error("unlink(%s) failed: %s",
1009 - path, strerror(errno));
1010 - }
1008 +
1009 if (!(delopt & REF_NODEREF))
1010 lock->lk->filename[i] = '.';
1011 }
@@ -1017,10 +1015,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
1015 */
1016 ret |= repack_without_ref(refname);
1017
1020 - err = unlink(git_path("logs/%s", lock->ref_name));
1021 - if (err && errno != ENOENT)
1022 - warning("unlink(%s) failed: %s",
1023 - git_path("logs/%s", lock->ref_name), strerror(errno));
1018 + unlink_or_warn(git_path("logs/%s", lock->ref_name));
1019 invalidate_cached_refs();
1020 unlock_ref(lock);
1021 return ret;
@@ -1381,7 +1376,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
1376 if (adjust_shared_perm(git_HEAD)) {
1377 error("Unable to fix permissions on %s", lockpath);
1378 error_unlink_return:
1384 - unlink(lockpath);
1379 + unlink_or_warn(lockpath);
1380 error_free_return:
1381 free(git_HEAD);
1382 return -1;
rerere.c
+1 -1
@@ -173,7 +173,7 @@ static int handle_file(const char *path,
173 git_SHA1_Final(sha1, &ctx);
174 if (hunk != RR_CONTEXT) {
175 if (output)
176 - unlink(output);
176 + unlink_or_warn(output);
177 return error("Could not parse conflict hunks in %s", path);
178 }
179 if (wrerror)
server-info.c
+1 -1
@@ -246,7 +246,7 @@ int update_server_info(int force)
246 errs = errs | update_info_packs(force);
247
248 /* remove leftover rev-cache file if there is any */
249 - unlink(git_path("info/rev-cache"));
249 + unlink_or_warn(git_path("info/rev-cache"));
250
251 return errs;
252 }
sha1_file.c
+1 -1
@@ -2247,7 +2247,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
2247 goto out;
2248 ret = errno;
2249 }
2250 - unlink(tmpfile);
2250 + unlink_or_warn(tmpfile);
2251 if (ret) {
2252 if (ret != EEXIST) {
2253 return error("unable to write sha1 filename %s: %s\n", filename, strerror(ret));
transport.c
+1 -1
@@ -1069,7 +1069,7 @@ int transport_fetch_refs(struct transport *transport, const struct ref *refs)
1069 void transport_unlock_pack(struct transport *transport)
1070 {
1071 if (transport->pack_lockfile) {
1072 - unlink(transport->pack_lockfile);
1072 + unlink_or_warn(transport->pack_lockfile);
1073 free(transport->pack_lockfile);
1074 transport->pack_lockfile = NULL;
1075 }
unpack-trees.c
+1 -1
@@ -61,7 +61,7 @@ static void unlink_entry(struct cache_entry *ce)
61 {
62 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
63 return;
64 - if (unlink(ce->name))
64 + if (unlink_or_warn(ce->name))
65 return;
66 schedule_dir_for_removal(ce->name, ce_namelen(ce));
67 }