@matheusbsbs / matheus-bagliano / commits / 4ed7cd3ab0
Fork 0

Improve use of lockfile API

Remove remaining double close(2)'s. i.e. close() before commit_locked_index() or commit_lock_file(). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Jan 16, 2008 at 13:12 UTC 4ed7cd3ab07f7c721daf4241fe1dac306fefd1fb
20 files changed +54 -45
builtin-add.c
+1 -1
@@ -259,7 +259,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
259 finish:
260 if (active_cache_changed) {
261 if (write_cache(newfd, active_cache, active_nr) ||
262 - close(newfd) || commit_locked_index(&lock_file))
262 + commit_locked_index(&lock_file))
263 die("Unable to write new index file");
264 }
265
builtin-apply.c
+1 -1
@@ -2921,7 +2921,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2921
2922 if (update_index) {
2923 if (write_cache(newfd, active_cache, active_nr) ||
2924 - close(newfd) || commit_locked_index(&lock_file))
2924 + commit_locked_index(&lock_file))
2925 die("Unable to write new index file");
2926 }
2927
builtin-checkout-index.c
+2 -2
@@ -246,8 +246,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
246 * want to update cache.
247 */
248 if (state.refresh_cache) {
249 - close(newfd); newfd = -1;
249 rollback_lock_file(&lock_file);
250 + newfd = -1;
251 }
252 state.refresh_cache = 0;
253 }
@@ -297,7 +297,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
297
298 if (0 <= newfd &&
299 (write_cache(newfd, active_cache, active_nr) ||
300 - close(newfd) || commit_locked_index(&lock_file)))
300 + commit_locked_index(&lock_file)))
301 die("Unable to write new index file");
302 return 0;
303 }
builtin-commit.c
+6 -3
@@ -237,7 +237,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
237 int fd = hold_locked_index(&index_lock, 1);
238 add_files_to_cache(0, also ? prefix : NULL, pathspec);
239 refresh_cache(REFRESH_QUIET);
240 - if (write_cache(fd, active_cache, active_nr))
240 + if (write_cache(fd, active_cache, active_nr) ||
241 + close_lock_file(&index_lock))
242 die("unable to write new_index file");
243 commit_style = COMMIT_NORMAL;
244 return index_lock.filename;
@@ -298,7 +299,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
299 fd = hold_locked_index(&index_lock, 1);
300 add_remove_files(&partial);
301 refresh_cache(REFRESH_QUIET);
301 - if (write_cache(fd, active_cache, active_nr))
302 + if (write_cache(fd, active_cache, active_nr) ||
303 + close_lock_file(&index_lock))
304 die("unable to write new_index file");
305
306 fd = hold_lock_file_for_update(&false_lock,
@@ -308,7 +310,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
310 add_remove_files(&partial);
311 refresh_cache(REFRESH_QUIET);
312
311 - if (write_cache(fd, active_cache, active_nr))
313 + if (write_cache(fd, active_cache, active_nr) ||
314 + close_lock_file(&false_lock))
315 die("unable to write temporary index file");
316 return false_lock.filename;
317 }
builtin-diff.c
+1 -1
@@ -190,7 +190,7 @@ static void refresh_index_quietly(void)
190 refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
191
192 if (active_cache_changed &&
193 - !write_cache(fd, active_cache, active_nr) && !close(fd))
193 + !write_cache(fd, active_cache, active_nr))
194 commit_locked_index(lock_file);
195
196 rollback_lock_file(lock_file);
builtin-fetch-pack.c
-1
@@ -783,7 +783,6 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
783 unlink(shallow);
784 rollback_lock_file(&lock);
785 } else {
786 - close(fd);
786 commit_lock_file(&lock);
787 }
788 }
builtin-mv.c
-1
@@ -264,7 +264,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
264
265 if (active_cache_changed) {
266 if (write_cache(newfd, active_cache, active_nr) ||
267 - close(newfd) ||
267 commit_locked_index(&lock_file))
268 die("Unable to write new index file");
269 }
builtin-pack-refs.c
+6
@@ -108,6 +108,12 @@ static int pack_refs(unsigned int flags)
108 die("failed to write ref-pack file");
109 if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
110 die("failed to write ref-pack file (%s)", strerror(errno));
111 + /*
112 + * Since the lock file was fdopen()'ed and then fclose()'ed above,
113 + * assign -1 to the lock file descriptor so that commit_lock_file()
114 + * won't try to close() it.
115 + */
116 + packed.fd = -1;
117 if (commit_lock_file(&packed) < 0)
118 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
119 if (cbdata.flags & PACK_REFS_PRUNE)
builtin-read-tree.c
+1 -1
@@ -283,7 +283,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
283 }
284
285 if (write_cache(newfd, active_cache, active_nr) ||
286 - close(newfd) || commit_locked_index(&lock_file))
286 + commit_locked_index(&lock_file))
287 die("unable to write new index file");
288 return 0;
289 }
builtin-rerere.c
+2 -2
@@ -61,9 +61,9 @@ static int write_rr(struct path_list *rr, int out_fd)
61 write_in_full(out_fd, path, length) != length)
62 die("unable to write rerere record");
63 }
64 - if (close(out_fd) != 0)
64 + if (commit_lock_file(&write_lock) != 0)
65 die("unable to write rerere record");
66 - return commit_lock_file(&write_lock);
66 + return 0;
67 }
68
69 static int handle_file(const char *path,
builtin-reset.c
-1
@@ -108,7 +108,6 @@ static int update_index_refresh(int fd, struct lock_file *index_lock)
108 return error("Could not read index");
109 result = refresh_cache(0) ? 1 : 0;
110 if (write_cache(fd, active_cache, active_nr) ||
111 - close(fd) ||
111 commit_locked_index(index_lock))
112 return error ("Could not refresh index");
113 return result;
builtin-revert.c
+2 -2
@@ -371,13 +371,13 @@ static int revert_or_cherry_pick(int argc, const char **argv)
371 i++;
372 }
373 }
374 - if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
374 + if (commit_lock_file(&msg_file) < 0)
375 die ("Error wrapping up %s", defmsg);
376 fprintf(stderr, "Automatic %s failed.%s\n",
377 me, help_msg(commit->object.sha1));
378 exit(1);
379 }
380 - if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
380 + if (commit_lock_file(&msg_file) < 0)
381 die ("Error wrapping up %s", defmsg);
382 fprintf(stderr, "Finished one %s.\n", me);
383
builtin-rm.c
+1 -1
@@ -250,7 +250,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
250
251 if (active_cache_changed) {
252 if (write_cache(newfd, active_cache, active_nr) ||
253 - close(newfd) || commit_locked_index(&lock_file))
253 + commit_locked_index(&lock_file))
254 die("Unable to write new index file");
255 }
256
builtin-update-index.c
+1 -1
@@ -738,7 +738,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
738 get_index_file(), strerror(lock_error));
739 }
740 if (write_cache(newfd, active_cache, active_nr) ||
741 - close(newfd) || commit_locked_index(lock_file))
741 + commit_locked_index(lock_file))
742 die("Unable to write new index file");
743 }
744
builtin-write-tree.c
+3 -6
@@ -35,11 +35,9 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
35 missing_ok, 0) < 0)
36 die("git-write-tree: error building trees");
37 if (0 <= newfd) {
38 - if (!write_cache(newfd, active_cache, active_nr)
39 - && !close(newfd)) {
40 - commit_lock_file(lock_file);
38 + if (!write_cache(newfd, active_cache, active_nr) &&
39 + !commit_lock_file(lock_file))
40 newfd = -1;
42 - }
41 }
42 /* Not being able to write is fine -- we are only interested
43 * in updating the cache-tree part, and if the next caller
@@ -60,8 +58,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
58 hashcpy(sha1, active_cache_tree->sha1);
59
60 if (0 <= newfd)
63 - close(newfd);
64 - rollback_lock_file(lock_file);
61 + rollback_lock_file(lock_file);
62
63 return 0;
64 }
bundle.c
+10 -4
@@ -317,6 +317,14 @@ int create_bundle(struct bundle_header *header, const char *path,
317 rls.git_cmd = 1;
318 if (start_command(&rls))
319 return error("Could not spawn pack-objects");
320 +
321 + /*
322 + * start_command closed bundle_fd if it was > 1
323 + * so set the lock fd to -1 so commit_lock_file()
324 + * won't fail trying to close it.
325 + */
326 + lock.fd = -1;
327 +
328 for (i = 0; i < revs.pending.nr; i++) {
329 struct object *object = revs.pending.objects[i].item;
330 if (object->flags & UNINTERESTING)
@@ -326,10 +334,8 @@ int create_bundle(struct bundle_header *header, const char *path,
334 }
335 if (finish_command(&rls))
336 return error ("pack-objects died");
329 - close(bundle_fd);
330 - if (!bundle_to_stdout)
331 - commit_lock_file(&lock);
332 - return 0;
337 +
338 + return bundle_to_stdout ? close(bundle_fd) : commit_lock_file(&lock);
339 }
340
341 int unbundle(struct bundle_header *header, int bundle_fd)
config.c
+2 -6
@@ -955,14 +955,12 @@ int git_config_set_multivar(const char* key, const char* value,
955 munmap(contents, contents_sz);
956 }
957
958 - if (close(fd) || commit_lock_file(lock) < 0) {
958 + if (commit_lock_file(lock) < 0) {
959 fprintf(stderr, "Cannot commit config file!\n");
960 ret = 4;
961 goto out_free;
962 }
963
964 - /* fd is closed, so don't try to close it below. */
965 - fd = -1;
964 /*
965 * lock is committed, so don't try to roll it back below.
966 * NOTE: Since lockfile.c keeps a linked list of all created
@@ -973,8 +971,6 @@ int git_config_set_multivar(const char* key, const char* value,
971 ret = 0;
972
973 out_free:
976 - if (0 <= fd)
977 - close(fd);
974 if (lock)
975 rollback_lock_file(lock);
976 free(config_filename);
@@ -1072,7 +1068,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1068 }
1069 fclose(config_file);
1070 unlock_and_out:
1075 - if (close(out_fd) || commit_lock_file(lock) < 0)
1071 + if (commit_lock_file(lock) < 0)
1072 ret = error("Cannot commit config file!");
1073 out:
1074 free(config_filename);
fast-import.c
+10 -2
@@ -1546,10 +1546,18 @@ static void dump_marks(void)
1546 }
1547
1548 dump_marks_helper(f, 0, marks);
1549 - fclose(f);
1550 - if (commit_lock_file(&mark_lock))
1549 + if (ferror(f) || fclose(f))
1550 failure |= error("Unable to write marks file %s: %s",
1551 mark_file, strerror(errno));
1552 + /*
1553 + * Since the lock file was fdopen()'ed and then fclose()'ed above,
1554 + * assign -1 to the lock file descriptor so that commit_lock_file()
1555 + * won't try to close() it.
1556 + */
1557 + mark_lock.fd = -1;
1558 + if (commit_lock_file(&mark_lock))
1559 + failure |= error("Unable to write commit file %s: %s",
1560 + mark_file, strerror(errno));
1561 }
1562
1563 static int read_next_command(void)
merge-recursive.c
+1 -1
@@ -1753,7 +1753,7 @@ int main(int argc, char *argv[])
1753
1754 if (active_cache_changed &&
1755 (write_cache(index_fd, active_cache, active_nr) ||
1756 - close(index_fd) || commit_locked_index(lock)))
1756 + commit_locked_index(lock)))
1757 die ("unable to write %s", get_index_file());
1758
1759 return clean ? 0: 1;
refs.c
+4 -8
@@ -864,7 +864,6 @@ static int repack_without_ref(const char *refname)
864 die("too long a refname '%s'", list->name);
865 write_or_die(fd, line, len);
866 }
867 - close(fd);
867 return commit_lock_file(&packlock);
868 }
869
@@ -1021,12 +1020,9 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1020
1021 void unlock_ref(struct ref_lock *lock)
1022 {
1024 - if (lock->lock_fd >= 0) {
1025 - close(lock->lock_fd);
1026 - /* Do not free lock->lk -- atexit() still looks at them */
1027 - if (lock->lk)
1028 - rollback_lock_file(lock->lk);
1029 - }
1023 + /* Do not free lock->lk -- atexit() still looks at them */
1024 + if (lock->lk)
1025 + rollback_lock_file(lock->lk);
1026 free(lock->ref_name);
1027 free(lock->orig_ref_name);
1028 free(lock);
@@ -1151,7 +1147,7 @@ int write_ref_sha1(struct ref_lock *lock,
1147 }
1148 if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
1149 write_in_full(lock->lock_fd, &term, 1) != 1
1154 - || close(lock->lock_fd) < 0) {
1150 + || close_lock_file(lock->lk) < 0) {
1151 error("Couldn't write %s", lock->lk->filename);
1152 unlock_ref(lock);
1153 return -1;