提交 2c5942db 编写于 作者: J Junio C Hamano

Merge branch 'ar/unlink-err' into maint

* ar/unlink-err:
  print unlink(2) errno in copy_or_link_directory
  replace direct calls to unlink(2) with unlink_or_warn
  Introduce an unlink(2) wrapper which gives warning if unlink failed
...@@ -2781,7 +2781,7 @@ static void remove_file(struct patch *patch, int rmdir_empty) ...@@ -2781,7 +2781,7 @@ static void remove_file(struct patch *patch, int rmdir_empty)
if (rmdir(patch->old_name)) if (rmdir(patch->old_name))
warning("unable to remove submodule %s", warning("unable to remove submodule %s",
patch->old_name); patch->old_name);
} else if (!unlink(patch->old_name) && rmdir_empty) { } else if (!unlink_or_warn(patch->old_name) && rmdir_empty) {
remove_path(patch->old_name); remove_path(patch->old_name);
} }
} }
...@@ -2891,7 +2891,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned ...@@ -2891,7 +2891,7 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
if (!try_create_file(newpath, mode, buf, size)) { if (!try_create_file(newpath, mode, buf, size)) {
if (!rename(newpath, path)) if (!rename(newpath, path))
return; return;
unlink(newpath); unlink_or_warn(newpath);
break; break;
} }
if (errno != EEXIST) if (errno != EEXIST)
......
...@@ -228,7 +228,8 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest) ...@@ -228,7 +228,8 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
} }
if (unlink(dest->buf) && errno != ENOENT) if (unlink(dest->buf) && errno != ENOENT)
die("failed to unlink %s", dest->buf); die("failed to unlink %s: %s",
dest->buf, strerror(errno));
if (!option_no_hardlinks) { if (!option_no_hardlinks) {
if (!link(src->buf, dest->buf)) if (!link(src->buf, dest->buf))
continue; continue;
......
...@@ -825,7 +825,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args, ...@@ -825,7 +825,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
fd = hold_lock_file_for_update(&lock, shallow, fd = hold_lock_file_for_update(&lock, shallow,
LOCK_DIE_ON_ERROR); LOCK_DIE_ON_ERROR);
if (!write_shallow_commits(fd, 0)) { if (!write_shallow_commits(fd, 0)) {
unlink(shallow); unlink_or_warn(shallow);
rollback_lock_file(&lock); rollback_lock_file(&lock);
} else { } else {
commit_lock_file(&lock); commit_lock_file(&lock);
......
...@@ -28,8 +28,8 @@ static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts) ...@@ -28,8 +28,8 @@ static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
memcpy(pathname + len, de->d_name, 38); memcpy(pathname + len, de->d_name, 38);
if (opts & DRY_RUN) if (opts & DRY_RUN)
printf("rm -f %s\n", pathname); printf("rm -f %s\n", pathname);
else if (unlink(pathname) < 0) else
error("unable to unlink %s", pathname); unlink_or_warn(pathname);
display_progress(progress, i + 1); display_progress(progress, i + 1);
} }
pathname[len] = 0; pathname[len] = 0;
......
...@@ -27,7 +27,7 @@ static int prune_tmp_object(const char *path, const char *filename) ...@@ -27,7 +27,7 @@ static int prune_tmp_object(const char *path, const char *filename)
} }
printf("Removing stale temporary file %s\n", fullpath); printf("Removing stale temporary file %s\n", fullpath);
if (!show_only) if (!show_only)
unlink(fullpath); unlink_or_warn(fullpath);
return 0; return 0;
} }
...@@ -47,7 +47,7 @@ static int prune_object(char *path, const char *filename, const unsigned char *s ...@@ -47,7 +47,7 @@ static int prune_object(char *path, const char *filename, const unsigned char *s
(type > 0) ? typename(type) : "unknown"); (type > 0) ? typename(type) : "unknown");
} }
if (!show_only) if (!show_only)
unlink(fullpath); unlink_or_warn(fullpath);
return 0; return 0;
} }
......
...@@ -702,7 +702,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) ...@@ -702,7 +702,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
unpack_status = unpack(); unpack_status = unpack();
execute_commands(unpack_status); execute_commands(unpack_status);
if (pack_lockfile) if (pack_lockfile)
unlink(pack_lockfile); unlink_or_warn(pack_lockfile);
if (report_status) if (report_status)
report(unpack_status); report(unpack_status);
run_receive_hook(post_receive_hook); run_receive_hook(post_receive_hook);
......
...@@ -525,8 +525,8 @@ static int migrate_file(struct remote *remote) ...@@ -525,8 +525,8 @@ static int migrate_file(struct remote *remote)
path = git_path("remotes/%s", remote->name); path = git_path("remotes/%s", remote->name);
else if (remote->origin == REMOTE_BRANCHES) else if (remote->origin == REMOTE_BRANCHES)
path = git_path("branches/%s", remote->name); path = git_path("branches/%s", remote->name);
if (path && unlink(path)) if (path)
warning("failed to remove '%s'", path); unlink_or_warn(path);
return 0; return 0;
} }
......
...@@ -116,7 +116,7 @@ int cmd_rerere(int argc, const char **argv, const char *prefix) ...@@ -116,7 +116,7 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
if (!has_rerere_resolution(name)) if (!has_rerere_resolution(name))
unlink_rr_item(name); unlink_rr_item(name);
} }
unlink(git_path("rr-cache/MERGE_RR")); unlink_or_warn(git_path("rr-cache/MERGE_RR"));
} else if (!strcmp(argv[1], "gc")) } else if (!strcmp(argv[1], "gc"))
garbage_collect(&merge_rr); garbage_collect(&merge_rr);
else if (!strcmp(argv[1], "status")) else if (!strcmp(argv[1], "status"))
......
...@@ -338,7 +338,7 @@ static void create_tag(const unsigned char *object, const char *tag, ...@@ -338,7 +338,7 @@ static void create_tag(const unsigned char *object, const char *tag,
exit(128); exit(128);
} }
if (path) { if (path) {
unlink(path); unlink_or_warn(path);
free(path); free(path);
} }
} }
......
...@@ -55,7 +55,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose) ...@@ -55,7 +55,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
close(gpg.in); close(gpg.in);
ret = finish_command(&gpg); ret = finish_command(&gpg);
unlink(path); unlink_or_warn(path);
return ret; return ret;
} }
......
...@@ -189,7 +189,7 @@ static void remove_tempfile(void) ...@@ -189,7 +189,7 @@ static void remove_tempfile(void)
int i; int i;
for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
if (diff_temp[i].name == diff_temp[i].tmp_path) if (diff_temp[i].name == diff_temp[i].tmp_path)
unlink(diff_temp[i].name); unlink_or_warn(diff_temp[i].name);
diff_temp[i].name = NULL; diff_temp[i].name = NULL;
} }
} }
......
...@@ -35,7 +35,7 @@ static void create_directories(const char *path, int path_len, ...@@ -35,7 +35,7 @@ static void create_directories(const char *path, int path_len,
*/ */
if (mkdir(buf, 0777)) { if (mkdir(buf, 0777)) {
if (errno == EEXIST && state->force && if (errno == EEXIST && state->force &&
!unlink(buf) && !mkdir(buf, 0777)) !unlink_or_warn(buf) && !mkdir(buf, 0777))
continue; continue;
die("cannot create directory at %s", buf); die("cannot create directory at %s", buf);
} }
......
...@@ -931,7 +931,7 @@ static void unkeep_all_packs(void) ...@@ -931,7 +931,7 @@ static void unkeep_all_packs(void)
struct packed_git *p = all_packs[k]; struct packed_git *p = all_packs[k];
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
get_object_directory(), sha1_to_hex(p->sha1)); get_object_directory(), sha1_to_hex(p->sha1));
unlink(name); unlink_or_warn(name);
} }
} }
...@@ -981,7 +981,7 @@ static void end_packfile(void) ...@@ -981,7 +981,7 @@ static void end_packfile(void)
} }
else { else {
close(old_p->pack_fd); close(old_p->pack_fd);
unlink(old_p->pack_name); unlink_or_warn(old_p->pack_name);
} }
free(old_p); free(old_p);
......
...@@ -415,4 +415,10 @@ void git_qsort(void *base, size_t nmemb, size_t size, ...@@ -415,4 +415,10 @@ void git_qsort(void *base, size_t nmemb, size_t size,
#define fstat_is_reliable() 1 #define fstat_is_reliable() 1
#endif #endif
/*
* Preserves errno, prints a message, but gives no warning for ENOENT.
* Always returns the return value of unlink(2).
*/
int unlink_or_warn(const char *path);
#endif #endif
...@@ -315,9 +315,9 @@ static void start_fetch_loose(struct transfer_request *request) ...@@ -315,9 +315,9 @@ static void start_fetch_loose(struct transfer_request *request)
"%s.temp", filename); "%s.temp", filename);
snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename); snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
unlink(prevfile); unlink_or_warn(prevfile);
rename(request->tmpfile, prevfile); rename(request->tmpfile, prevfile);
unlink(request->tmpfile); unlink_or_warn(request->tmpfile);
if (request->local_fileno != -1) if (request->local_fileno != -1)
error("fd leakage in start: %d", request->local_fileno); error("fd leakage in start: %d", request->local_fileno);
...@@ -372,7 +372,7 @@ static void start_fetch_loose(struct transfer_request *request) ...@@ -372,7 +372,7 @@ static void start_fetch_loose(struct transfer_request *request)
} while (prev_read > 0); } while (prev_read > 0);
close(prevlocal); close(prevlocal);
} }
unlink(prevfile); unlink_or_warn(prevfile);
/* Reset inflate/SHA1 if there was an error reading the previous temp /* Reset inflate/SHA1 if there was an error reading the previous temp
file; also rewind to the beginning of the local file. */ file; also rewind to the beginning of the local file. */
...@@ -784,7 +784,7 @@ static void finish_request(struct transfer_request *request) ...@@ -784,7 +784,7 @@ static void finish_request(struct transfer_request *request)
request->http_code != 416) { request->http_code != 416) {
if (stat(request->tmpfile, &st) == 0) { if (stat(request->tmpfile, &st) == 0) {
if (st.st_size == 0) if (st.st_size == 0)
unlink(request->tmpfile); unlink_or_warn(request->tmpfile);
} }
} else { } else {
if (request->http_code == 416) if (request->http_code == 416)
...@@ -793,9 +793,9 @@ static void finish_request(struct transfer_request *request) ...@@ -793,9 +793,9 @@ static void finish_request(struct transfer_request *request)
git_inflate_end(&request->stream); git_inflate_end(&request->stream);
git_SHA1_Final(request->real_sha1, &request->c); git_SHA1_Final(request->real_sha1, &request->c);
if (request->zret != Z_STREAM_END) { if (request->zret != Z_STREAM_END) {
unlink(request->tmpfile); unlink_or_warn(request->tmpfile);
} else if (hashcmp(request->obj->sha1, request->real_sha1)) { } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
unlink(request->tmpfile); unlink_or_warn(request->tmpfile);
} else { } else {
request->rename = request->rename =
move_temp_to_file( move_temp_to_file(
......
...@@ -111,9 +111,9 @@ static void start_object_request(struct walker *walker, ...@@ -111,9 +111,9 @@ static void start_object_request(struct walker *walker,
struct walker_data *data = walker->data; struct walker_data *data = walker->data;
snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename); snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
unlink(prevfile); unlink_or_warn(prevfile);
rename(obj_req->tmpfile, prevfile); rename(obj_req->tmpfile, prevfile);
unlink(obj_req->tmpfile); unlink_or_warn(obj_req->tmpfile);
if (obj_req->local != -1) if (obj_req->local != -1)
error("fd leakage in start: %d", obj_req->local); error("fd leakage in start: %d", obj_req->local);
...@@ -177,7 +177,7 @@ static void start_object_request(struct walker *walker, ...@@ -177,7 +177,7 @@ static void start_object_request(struct walker *walker,
} while (prev_read > 0); } while (prev_read > 0);
close(prevlocal); close(prevlocal);
} }
unlink(prevfile); unlink_or_warn(prevfile);
/* Reset inflate/SHA1 if there was an error reading the previous temp /* Reset inflate/SHA1 if there was an error reading the previous temp
file; also rewind to the beginning of the local file. */ 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,18 +238,18 @@ static void finish_object_request(struct object_request *obj_req)
} else if (obj_req->curl_result != CURLE_OK) { } else if (obj_req->curl_result != CURLE_OK) {
if (stat(obj_req->tmpfile, &st) == 0) if (stat(obj_req->tmpfile, &st) == 0)
if (st.st_size == 0) if (st.st_size == 0)
unlink(obj_req->tmpfile); unlink_or_warn(obj_req->tmpfile);
return; return;
} }
git_inflate_end(&obj_req->stream); git_inflate_end(&obj_req->stream);
git_SHA1_Final(obj_req->real_sha1, &obj_req->c); git_SHA1_Final(obj_req->real_sha1, &obj_req->c);
if (obj_req->zret != Z_STREAM_END) { if (obj_req->zret != Z_STREAM_END) {
unlink(obj_req->tmpfile); unlink_or_warn(obj_req->tmpfile);
return; return;
} }
if (hashcmp(obj_req->sha1, obj_req->real_sha1)) { if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
unlink(obj_req->tmpfile); unlink_or_warn(obj_req->tmpfile);
return; return;
} }
obj_req->rename = obj_req->rename =
...@@ -809,7 +809,7 @@ static void abort_object_request(struct object_request *obj_req) ...@@ -809,7 +809,7 @@ static void abort_object_request(struct object_request *obj_req)
close(obj_req->local); close(obj_req->local);
obj_req->local = -1; obj_req->local = -1;
} }
unlink(obj_req->tmpfile); unlink_or_warn(obj_req->tmpfile);
if (obj_req->slot) { if (obj_req->slot) {
release_active_slot(obj_req->slot); release_active_slot(obj_req->slot);
obj_req->slot = NULL; obj_req->slot = NULL;
......
...@@ -219,7 +219,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn, ...@@ -219,7 +219,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
close(fd); close(fd);
bad: bad:
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
unlink(temp[i]); unlink_or_warn(temp[i]);
strbuf_release(&cmd); strbuf_release(&cmd);
return status; return status;
} }
......
...@@ -16,7 +16,7 @@ static void remove_lock_file(void) ...@@ -16,7 +16,7 @@ static void remove_lock_file(void)
lock_file_list->filename[0]) { lock_file_list->filename[0]) {
if (lock_file_list->fd >= 0) if (lock_file_list->fd >= 0)
close(lock_file_list->fd); close(lock_file_list->fd);
unlink(lock_file_list->filename); unlink_or_warn(lock_file_list->filename);
} }
lock_file_list = lock_file_list->next; lock_file_list = lock_file_list->next;
} }
...@@ -259,7 +259,7 @@ void rollback_lock_file(struct lock_file *lk) ...@@ -259,7 +259,7 @@ void rollback_lock_file(struct lock_file *lk)
if (lk->filename[0]) { if (lk->filename[0]) {
if (lk->fd >= 0) if (lk->fd >= 0)
close(lk->fd); close(lk->fd);
unlink(lk->filename); unlink_or_warn(lk->filename);
} }
lk->filename[0] = 0; lk->filename[0] = 0;
} }
...@@ -66,7 +66,7 @@ static void prune_ref(struct ref_to_prune *r) ...@@ -66,7 +66,7 @@ static void prune_ref(struct ref_to_prune *r)
struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1); struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
if (lock) { if (lock) {
unlink(git_path("%s", r->name)); unlink_or_warn(git_path("%s", r->name));
unlock_ref(lock); unlock_ref(lock);
} }
} }
......
...@@ -1004,12 +1004,10 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt) ...@@ -1004,12 +1004,10 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
} else { } else {
path = git_path("%s", refname); path = git_path("%s", refname);
} }
err = unlink(path); err = unlink_or_warn(path);
if (err && errno != ENOENT) { if (err && errno != ENOENT)
ret = 1; ret = 1;
error("unlink(%s) failed: %s",
path, strerror(errno));
}
if (!(delopt & REF_NODEREF)) if (!(delopt & REF_NODEREF))
lock->lk->filename[i] = '.'; lock->lk->filename[i] = '.';
} }
...@@ -1019,10 +1017,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt) ...@@ -1019,10 +1017,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
*/ */
ret |= repack_without_ref(refname); ret |= repack_without_ref(refname);
err = unlink(git_path("logs/%s", lock->ref_name)); unlink_or_warn(git_path("logs/%s", lock->ref_name));
if (err && errno != ENOENT)
warning("unlink(%s) failed: %s",
git_path("logs/%s", lock->ref_name), strerror(errno));
invalidate_cached_refs(); invalidate_cached_refs();
unlock_ref(lock); unlock_ref(lock);
return ret; return ret;
...@@ -1383,7 +1378,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master, ...@@ -1383,7 +1378,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
if (adjust_shared_perm(git_HEAD)) { if (adjust_shared_perm(git_HEAD)) {
error("Unable to fix permissions on %s", lockpath); error("Unable to fix permissions on %s", lockpath);
error_unlink_return: error_unlink_return:
unlink(lockpath); unlink_or_warn(lockpath);
error_free_return: error_free_return:
free(git_HEAD); free(git_HEAD);
return -1; return -1;
......
...@@ -173,7 +173,7 @@ static int handle_file(const char *path, ...@@ -173,7 +173,7 @@ static int handle_file(const char *path,
git_SHA1_Final(sha1, &ctx); git_SHA1_Final(sha1, &ctx);
if (hunk != RR_CONTEXT) { if (hunk != RR_CONTEXT) {
if (output) if (output)
unlink(output); unlink_or_warn(output);
return error("Could not parse conflict hunks in %s", path); return error("Could not parse conflict hunks in %s", path);
} }
if (wrerror) if (wrerror)
......
...@@ -246,7 +246,7 @@ int update_server_info(int force) ...@@ -246,7 +246,7 @@ int update_server_info(int force)
errs = errs | update_info_packs(force); errs = errs | update_info_packs(force);
/* remove leftover rev-cache file if there is any */ /* remove leftover rev-cache file if there is any */
unlink(git_path("info/rev-cache")); unlink_or_warn(git_path("info/rev-cache"));
return errs; return errs;
} }
...@@ -2251,7 +2251,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename) ...@@ -2251,7 +2251,7 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
goto out; goto out;
ret = errno; ret = errno;
} }
unlink(tmpfile); unlink_or_warn(tmpfile);
if (ret) { if (ret) {
if (ret != EEXIST) { if (ret != EEXIST) {
return error("unable to write sha1 filename %s: %s\n", filename, strerror(ret)); return error("unable to write sha1 filename %s: %s\n", filename, strerror(ret));
......
...@@ -1069,7 +1069,7 @@ int transport_fetch_refs(struct transport *transport, const struct ref *refs) ...@@ -1069,7 +1069,7 @@ int transport_fetch_refs(struct transport *transport, const struct ref *refs)
void transport_unlock_pack(struct transport *transport) void transport_unlock_pack(struct transport *transport)
{ {
if (transport->pack_lockfile) { if (transport->pack_lockfile) {
unlink(transport->pack_lockfile); unlink_or_warn(transport->pack_lockfile);
free(transport->pack_lockfile); free(transport->pack_lockfile);
transport->pack_lockfile = NULL; transport->pack_lockfile = NULL;
} }
......
...@@ -61,7 +61,7 @@ static void unlink_entry(struct cache_entry *ce) ...@@ -61,7 +61,7 @@ static void unlink_entry(struct cache_entry *ce)
{ {
if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce))) if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
return; return;
if (unlink(ce->name)) if (unlink_or_warn(ce->name))
return; return;
schedule_dir_for_removal(ce->name, ce_namelen(ce)); schedule_dir_for_removal(ce->name, ce_namelen(ce));
} }
......
...@@ -289,3 +289,19 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1) ...@@ -289,3 +289,19 @@ int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
safe_create_leading_directories(name); safe_create_leading_directories(name);
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600); return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
} }
int unlink_or_warn(const char *file)
{
int rc = unlink(file);
if (rc < 0) {
int err = errno;
if (ENOENT != err) {
warning("unable to unlink %s: %s",
file, strerror(errno));
errno = err;
}
}
return rc;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册