Skip to content

Commit 4ed7cd3

Browse files
drafnelgitster
authored andcommitted
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>
1 parent d6cf61b commit 4ed7cd3

20 files changed

+54
-45
lines changed

builtin-add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
259259
finish:
260260
if (active_cache_changed) {
261261
if (write_cache(newfd, active_cache, active_nr) ||
262-
close(newfd) || commit_locked_index(&lock_file))
262+
commit_locked_index(&lock_file))
263263
die("Unable to write new index file");
264264
}
265265

builtin-apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2921,7 +2921,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
29212921

29222922
if (update_index) {
29232923
if (write_cache(newfd, active_cache, active_nr) ||
2924-
close(newfd) || commit_locked_index(&lock_file))
2924+
commit_locked_index(&lock_file))
29252925
die("Unable to write new index file");
29262926
}
29272927

builtin-checkout-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
246246
* want to update cache.
247247
*/
248248
if (state.refresh_cache) {
249-
close(newfd); newfd = -1;
250249
rollback_lock_file(&lock_file);
250+
newfd = -1;
251251
}
252252
state.refresh_cache = 0;
253253
}
@@ -297,7 +297,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
297297

298298
if (0 <= newfd &&
299299
(write_cache(newfd, active_cache, active_nr) ||
300-
close(newfd) || commit_locked_index(&lock_file)))
300+
commit_locked_index(&lock_file)))
301301
die("Unable to write new index file");
302302
return 0;
303303
}

builtin-commit.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
237237
int fd = hold_locked_index(&index_lock, 1);
238238
add_files_to_cache(0, also ? prefix : NULL, pathspec);
239239
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))
241242
die("unable to write new_index file");
242243
commit_style = COMMIT_NORMAL;
243244
return index_lock.filename;
@@ -298,7 +299,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
298299
fd = hold_locked_index(&index_lock, 1);
299300
add_remove_files(&partial);
300301
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))
302304
die("unable to write new_index file");
303305

304306
fd = hold_lock_file_for_update(&false_lock,
@@ -308,7 +310,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
308310
add_remove_files(&partial);
309311
refresh_cache(REFRESH_QUIET);
310312

311-
if (write_cache(fd, active_cache, active_nr))
313+
if (write_cache(fd, active_cache, active_nr) ||
314+
close_lock_file(&false_lock))
312315
die("unable to write temporary index file");
313316
return false_lock.filename;
314317
}

builtin-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static void refresh_index_quietly(void)
190190
refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
191191

192192
if (active_cache_changed &&
193-
!write_cache(fd, active_cache, active_nr) && !close(fd))
193+
!write_cache(fd, active_cache, active_nr))
194194
commit_locked_index(lock_file);
195195

196196
rollback_lock_file(lock_file);

builtin-fetch-pack.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,6 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
783783
unlink(shallow);
784784
rollback_lock_file(&lock);
785785
} else {
786-
close(fd);
787786
commit_lock_file(&lock);
788787
}
789788
}

builtin-mv.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
264264

265265
if (active_cache_changed) {
266266
if (write_cache(newfd, active_cache, active_nr) ||
267-
close(newfd) ||
268267
commit_locked_index(&lock_file))
269268
die("Unable to write new index file");
270269
}

builtin-pack-refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ static int pack_refs(unsigned int flags)
108108
die("failed to write ref-pack file");
109109
if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
110110
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;
111117
if (commit_lock_file(&packed) < 0)
112118
die("unable to overwrite old ref-pack file (%s)", strerror(errno));
113119
if (cbdata.flags & PACK_REFS_PRUNE)

builtin-read-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
283283
}
284284

285285
if (write_cache(newfd, active_cache, active_nr) ||
286-
close(newfd) || commit_locked_index(&lock_file))
286+
commit_locked_index(&lock_file))
287287
die("unable to write new index file");
288288
return 0;
289289
}

builtin-rerere.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ static int write_rr(struct path_list *rr, int out_fd)
6161
write_in_full(out_fd, path, length) != length)
6262
die("unable to write rerere record");
6363
}
64-
if (close(out_fd) != 0)
64+
if (commit_lock_file(&write_lock) != 0)
6565
die("unable to write rerere record");
66-
return commit_lock_file(&write_lock);
66+
return 0;
6767
}
6868

6969
static int handle_file(const char *path,

0 commit comments

Comments
 (0)