From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH 03/14] lockfile: remove some redundant functions
Date: Mon, 8 Jun 2015 11:07:34 +0200 [thread overview]
Message-ID: <0d17c04842bef618ecd474c56f2dbcf6a613a379.1433751986.git.mhagger@alum.mit.edu> (raw)
In-Reply-To: <cover.1433751986.git.mhagger@alum.mit.edu>
Remove the following functions and rewrite their callers to use the
equivalent tempfile functions directly:
* fdopen_lock_file() -> fdopen_tempfile()
* reopen_lock_file() -> reopen_tempfile()
* close_lock_file() -> close_tempfile()
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
builtin/commit.c | 2 +-
fast-import.c | 2 +-
lockfile.c | 15 ------------
lockfile.h | 70 ++++++++++++--------------------------------------------
read-cache.c | 2 +-
refs.c | 8 +++----
6 files changed, 22 insertions(+), 77 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index c9fbe42..970565c 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -358,7 +358,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
discard_cache();
read_cache_from(index_lock.tempfile.filename.buf);
if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
- if (reopen_lock_file(&index_lock) < 0)
+ if (reopen_tempfile(&index_lock.tempfile) < 0)
die(_("unable to write index file"));
if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
die(_("unable to update temporary index"));
diff --git a/fast-import.c b/fast-import.c
index 32a3e21..ca30fe9 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1807,7 +1807,7 @@ static void dump_marks(void)
return;
}
- f = fdopen_lock_file(&mark_lock, "w");
+ f = fdopen_tempfile(&mark_lock.tempfile, "w");
if (!f) {
int saved_errno = errno;
rollback_lock_file(&mark_lock);
diff --git a/lockfile.c b/lockfile.c
index b453016..c2d6ad1 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -228,11 +228,6 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
return fd;
}
-FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
-{
- return fdopen_tempfile(&lk->tempfile, mode);
-}
-
char *get_locked_file_path(struct lock_file *lk)
{
if (!lk->tempfile.active)
@@ -242,16 +237,6 @@ char *get_locked_file_path(struct lock_file *lk)
return xmemdupz(lk->tempfile.filename.buf, lk->tempfile.filename.len - LOCK_SUFFIX_LEN);
}
-int close_lock_file(struct lock_file *lk)
-{
- return close_tempfile(&lk->tempfile);
-}
-
-int reopen_lock_file(struct lock_file *lk)
-{
- return reopen_tempfile(&lk->tempfile);
-}
-
int commit_lock_file_to(struct lock_file *lk, const char *path)
{
return rename_tempfile(&lk->tempfile, path);
diff --git a/lockfile.h b/lockfile.h
index 5b313ef..3a212e9 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -53,8 +53,8 @@
* `hold_lock_file_for_*()` functions (also available via
* `lock->fd`).
*
- * * calling `fdopen_lock_file()` to get a `FILE` pointer for the
- * open file and writing to the file using stdio.
+ * * calling `fdopen_tempfile(lk->tempfile)` to get a `FILE` pointer
+ * for the open file and writing to the file using stdio.
*
* When finished writing, the caller can:
*
@@ -65,10 +65,16 @@
* * Close the file descriptor and remove the lockfile by calling
* `rollback_lock_file()`.
*
- * * Close the file descriptor without removing or renaming the
- * lockfile by calling `close_lock_file()`, and later call
- * `commit_lock_file()`, `commit_lock_file_to()`,
- * `rollback_lock_file()`, or `reopen_lock_file()`.
+ * It is also permissable to call the following functions on the
+ * underlying tempfile object:
+ *
+ * * close_tempfile(lk->tempfile)
+ *
+ * * reopen_tempfile(lk->tempfile)
+ *
+ * * fdopen_tempfile(lk->tempfile, mode)
+ *
+ * See "tempfile.h" for more information.
*
* Even after the lockfile is committed or rolled back, the
* `lock_file` object must not be freed or altered by the caller.
@@ -80,11 +86,6 @@
* tempfile module will close and remove the lockfile, thereby rolling
* back any uncommitted changes.
*
- * If you need to close the file descriptor you obtained from a
- * `hold_lock_file_for_*()` function yourself, do so by calling
- * `close_lock_file()`. See "tempfile.h" for more information.
- *
- *
* Under the covers, a lockfile is just a tempfile with a few helper
* functions. In particular, the state diagram and the cleanup
* machinery are all implemented in the tempfile module.
@@ -99,10 +100,9 @@
* failure. Errors can be reported by passing `errno` to
* `unable_to_lock_message()` or `unable_to_lock_die()`.
*
- * Similarly, `commit_lock_file`, `commit_lock_file_to`, and
- * `close_lock_file` return 0 on success. On failure they set `errno`
- * appropriately, do their best to roll back the lockfile, and return
- * -1.
+ * Similarly, `commit_lock_file` and `commit_lock_file_to` return 0 on
+ * success. On failure they set `errno` appropriately, do their best
+ * to roll back the lockfile, and return -1.
*/
struct lock_file {
@@ -192,52 +192,12 @@ extern void unable_to_lock_message(const char *path, int err,
extern NORETURN void unable_to_lock_die(const char *path, int err);
/*
- * Associate a stdio stream with the lockfile (which must still be
- * open). Return `NULL` (*without* rolling back the lockfile) on
- * error. The stream is closed automatically when `close_lock_file()`
- * is called or when the file is committed or rolled back.
- */
-extern FILE *fdopen_lock_file(struct lock_file *lk, const char *mode);
-
-/*
* Return the path of the file that is locked by the specified
* lock_file object. The caller must free the memory.
*/
extern char *get_locked_file_path(struct lock_file *lk);
/*
- * If the lockfile is still open, close it (and the file pointer if it
- * has been opened using `fdopen_lock_file()`) without renaming the
- * lockfile over the file being locked. Return 0 upon success. On
- * failure to `close(2)`, return a negative value and roll back the
- * lock file. Usually `commit_lock_file()`, `commit_lock_file_to()`,
- * or `rollback_lock_file()` should eventually be called if
- * `close_lock_file()` succeeds.
- */
-extern int close_lock_file(struct lock_file *lk);
-
-/*
- * Re-open a lockfile that has been closed using `close_lock_file()`
- * but not yet committed or rolled back. This can be used to implement
- * a sequence of operations like the following:
- *
- * * Lock file.
- *
- * * Write new contents to lockfile, then `close_lock_file()` to
- * cause the contents to be written to disk.
- *
- * * Pass the name of the lockfile to another program to allow it (and
- * nobody else) to inspect the contents you wrote, while still
- * holding the lock yourself.
- *
- * * `reopen_lock_file()` to reopen the lockfile. Make further updates
- * to the contents.
- *
- * * `commit_lock_file()` to make the final version permanent.
- */
-extern int reopen_lock_file(struct lock_file *lk);
-
-/*
* Commit the change represented by `lk`: close the file descriptor
* and/or file pointer if they are still open and rename the lockfile
* to its final destination. Return 0 upon success. On failure, roll
diff --git a/read-cache.c b/read-cache.c
index e20e003..3e49c49 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2121,7 +2121,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
if (flags & COMMIT_LOCK)
return commit_locked_index(lock);
else if (flags & CLOSE_LOCK)
- return close_lock_file(lock);
+ return close_tempfile(&lock->tempfile);
else
return ret;
}
diff --git a/refs.c b/refs.c
index d98b0f5..b46deba 100644
--- a/refs.c
+++ b/refs.c
@@ -2549,7 +2549,7 @@ int commit_packed_refs(void)
if (!packed_ref_cache->lock)
die("internal error: packed-refs not locked");
- out = fdopen_lock_file(packed_ref_cache->lock, "w");
+ out = fdopen_tempfile(&packed_ref_cache->lock->tempfile, "w");
if (!out)
die_errno("unable to fdopen packed-refs descriptor");
@@ -2984,7 +2984,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
static int close_ref(struct ref_lock *lock)
{
- if (close_lock_file(lock->lk))
+ if (close_tempfile(&lock->lk->tempfile))
return -1;
return 0;
}
@@ -4237,7 +4237,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
strbuf_release(&err);
goto failure;
}
- cb.newlog = fdopen_lock_file(&reflog_lock, "w");
+ cb.newlog = fdopen_tempfile(&reflog_lock.tempfile, "w");
if (!cb.newlog) {
error("cannot fdopen %s (%s)",
reflog_lock.tempfile.filename.buf, strerror(errno));
@@ -4261,7 +4261,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
!(type & REF_ISSYMREF) &&
!is_null_sha1(cb.last_kept_sha1);
- if (close_lock_file(&reflog_lock)) {
+ if (close_tempfile(&reflog_lock.tempfile)) {
status |= error("couldn't write %s: %s", log_file,
strerror(errno));
} else if (update &&
--
2.1.4
next prev parent reply other threads:[~2015-06-08 9:08 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-08 9:07 [PATCH 00/14] Introduce a tempfile module Michael Haggerty
2015-06-08 9:07 ` [PATCH 01/14] Move lockfile API documentation to lockfile.h Michael Haggerty
2015-06-08 9:07 ` [PATCH 02/14] tempfile: a new module for handling temporary files Michael Haggerty
2015-06-10 17:36 ` Junio C Hamano
2015-06-10 20:56 ` Michael Haggerty
2015-06-10 21:35 ` Junio C Hamano
2015-06-08 9:07 ` Michael Haggerty [this message]
2015-06-10 17:40 ` [PATCH 03/14] lockfile: remove some redundant functions Junio C Hamano
2015-06-10 18:27 ` Johannes Sixt
2015-06-08 9:07 ` [PATCH 04/14] commit_lock_file(): use get_locked_file_path() Michael Haggerty
2015-06-08 9:07 ` [PATCH 05/14] register_tempfile_object(): new function, extracted from create_tempfile() Michael Haggerty
2015-06-08 9:07 ` [PATCH 06/14] tempfile: add several functions for creating temporary files Michael Haggerty
2015-06-10 17:48 ` Junio C Hamano
2015-08-10 3:08 ` Michael Haggerty
2015-06-08 9:07 ` [PATCH 07/14] register_tempfile(): new function to handle an existing temporary file Michael Haggerty
2015-06-10 17:55 ` Junio C Hamano
2015-08-10 3:40 ` Michael Haggerty
2015-06-08 9:07 ` [PATCH 08/14] write_shared_index(): use tempfile module Michael Haggerty
2015-06-10 17:56 ` Junio C Hamano
2015-06-08 9:07 ` [PATCH 09/14] setup_temporary_shallow(): " Michael Haggerty
2015-06-08 9:07 ` [PATCH 10/14] diff: " Michael Haggerty
2015-06-08 9:07 ` [PATCH 11/14] lock_repo_for_gc(): compute the path to "gc.pid" only once Michael Haggerty
2015-06-08 9:07 ` [PATCH 12/14] gc: use tempfile module to handle gc.pid file Michael Haggerty
2015-06-08 9:07 ` [PATCH 13/14] credential-cache--daemon: delete socket from main() Michael Haggerty
2015-06-08 9:07 ` [PATCH 14/14] credential-cache--daemon: use tempfile module Michael Haggerty
2015-06-10 18:34 ` [PATCH 00/14] Introduce a " Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0d17c04842bef618ecd474c56f2dbcf6a613a379.1433751986.git.mhagger@alum.mit.edu \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).