1. Stefan Saasen
  2. git

Commits

Junio C Hamano  committed 021b6e4

Make index file locking code reusable to others.

The framework to create lockfiles that are removed at exit is
first used to reliably write the index file, but it is
applicable to other things, so stop calling it "cache_file".

This also rewords a few remaining error message that called the
index file "cache file".

Signed-off-by: Junio C Hamano <junkio@cox.net>

  • Participants
  • Parent commits dd8239f
  • Branches master

Comments (0)

Files changed (11)

File Makefile

View file
 
 LIB_OBJS = \
 	blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
-	date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
+	date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
 	object.o pack-check.o patch-delta.o path.o pkt-line.o \
 	quote.o read-cache.o refs.o run-command.o dir.o \
 	server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \

File builtin-add.c

View file
 	return 0;
 }
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int cmd_add(int argc, const char **argv, char **envp)
 {
 
 	git_config(git_default_config);
 
-	newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	newfd = hold_lock_file_for_update(&lock_file, get_index_file());
 	if (newfd < 0)
-		die("unable to create new cachefile");
+		die("unable to create new index file");
 
 	if (read_cache() < 0)
 		die("index file corrupt");
 
 	if (active_cache_changed) {
 		if (write_cache(newfd, active_cache, active_nr) ||
-		    commit_index_file(&cache_file))
+		    commit_lock_file(&lock_file))
 			die("Unable to write new index file");
 	}
 

File builtin-apply.c

View file
 	}
 }
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 static struct excludes {
 	struct excludes *next;
 		apply = 0;
 
 	write_index = check_index && apply;
-	if (write_index && newfd < 0)
-		newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	if (write_index && newfd < 0) {
+		newfd = hold_lock_file_for_update(&lock_file,
+						  get_index_file());
+		if (newfd < 0)
+			die("unable to create new index file");
+	}
 	if (check_index) {
 		if (read_cache() < 0)
 			die("unable to read index file");
 
 	if (write_index) {
 		if (write_cache(newfd, active_cache, active_nr) ||
-		    commit_index_file(&cache_file))
-			die("Unable to write new cachefile");
+		    commit_lock_file(&lock_file))
+			die("Unable to write new index file");
 	}
 
 	return 0;

File builtin-read-tree.c

View file
 
 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int cmd_read_tree(int argc, const char **argv, char **envp)
 {
 	setup_git_directory();
 	git_config(git_default_config);
 
-	newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	newfd = hold_lock_file_for_update(&lock_file, get_index_file());
 	if (newfd < 0)
-		die("unable to create new cachefile");
+		die("unable to create new index file");
 
 	git_config(git_default_config);
 
 	}
 
 	if (write_cache(newfd, active_cache, active_nr) ||
-	    commit_index_file(&cache_file))
+	    commit_lock_file(&lock_file))
 		die("unable to write new index file");
 	return 0;
 }

File builtin-rm.c

View file
 	return ret;
 }
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int cmd_rm(int argc, const char **argv, char **envp)
 {
 
 	git_config(git_default_config);
 
-	newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	newfd = hold_lock_file_for_update(&lock_file, get_index_file());
 	if (newfd < 0)
 		die("unable to create new index file");
 
 
 	if (active_cache_changed) {
 		if (write_cache(newfd, active_cache, active_nr) ||
-		    commit_index_file(&cache_file))
+		    commit_lock_file(&lock_file))
 			die("Unable to write new index file");
 	}
 

File cache.h

View file
 #define REFRESH_IGNORE_MISSING	0x0008	/* ignore non-existent */
 extern int refresh_cache(unsigned int flags);
 
-struct cache_file {
-	struct cache_file *next;
-	char lockfile[PATH_MAX];
+struct lock_file {
+	struct lock_file *next;
+	char filename[PATH_MAX];
 };
-extern int hold_index_file_for_update(struct cache_file *, const char *path);
-extern int commit_index_file(struct cache_file *);
-extern void rollback_index_file(struct cache_file *);
+extern int hold_lock_file_for_update(struct lock_file *, const char *path);
+extern int commit_lock_file(struct lock_file *);
+extern void rollback_lock_file(struct lock_file *);
 
 /* Environment bits from configuration mechanism */
 extern int trust_executable_bit;

File checkout-index.c

View file
 static const char checkout_cache_usage[] =
 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int main(int argc, char **argv)
 {
 		if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
 			state.refresh_cache = 1;
 			if (newfd < 0)
-				newfd = hold_index_file_for_update
-					(&cache_file,
-					 get_index_file());
+				newfd = hold_lock_file_for_update
+					(&lock_file, get_index_file());
 			if (newfd < 0)
 				die("cannot open index.lock file.");
 			continue;
 		 */
 		if (state.refresh_cache) {
 			close(newfd); newfd = -1;
-			rollback_index_file(&cache_file);
+			rollback_lock_file(&lock_file);
 		}
 		state.refresh_cache = 0;
 	}
 
 	if (0 <= newfd &&
 	    (write_cache(newfd, active_cache, active_nr) ||
-	     commit_index_file(&cache_file)))
-		die("Unable to write new cachefile");
+	     commit_lock_file(&lock_file)))
+		die("Unable to write new index file");
 	return 0;
 }

File index.c

-/*
- * Copyright (c) 2005, Junio C Hamano
- */
-#include <signal.h>
-#include "cache.h"
-
-static struct cache_file *cache_file_list;
-
-static void remove_lock_file(void)
-{
-	while (cache_file_list) {
-		if (cache_file_list->lockfile[0])
-			unlink(cache_file_list->lockfile);
-		cache_file_list = cache_file_list->next;
-	}
-}
-
-static void remove_lock_file_on_signal(int signo)
-{
-	remove_lock_file();
-	signal(SIGINT, SIG_DFL);
-	raise(signo);
-}
-
-int hold_index_file_for_update(struct cache_file *cf, const char *path)
-{
-	int fd;
-	sprintf(cf->lockfile, "%s.lock", path);
-	fd = open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0666);
-	if (fd >=0 && !cf->next) {
-		cf->next = cache_file_list;
-		cache_file_list = cf;
-		signal(SIGINT, remove_lock_file_on_signal);
-		atexit(remove_lock_file);
-	}
-	return fd;
-}
-
-int commit_index_file(struct cache_file *cf)
-{
-	char indexfile[PATH_MAX];
-	int i;
-	strcpy(indexfile, cf->lockfile);
-	i = strlen(indexfile) - 5; /* .lock */
-	indexfile[i] = 0;
-	i = rename(cf->lockfile, indexfile);
-	cf->lockfile[0] = 0;
-	return i;
-}
-
-void rollback_index_file(struct cache_file *cf)
-{
-	if (cf->lockfile[0])
-		unlink(cf->lockfile);
-	cf->lockfile[0] = 0;
-}
-

File lockfile.c

View file
+/*
+ * Copyright (c) 2005, Junio C Hamano
+ */
+#include <signal.h>
+#include "cache.h"
+
+static struct lock_file *lock_file_list;
+
+static void remove_lock_file(void)
+{
+	while (lock_file_list) {
+		if (lock_file_list->filename[0])
+			unlink(lock_file_list->filename);
+		lock_file_list = lock_file_list->next;
+	}
+}
+
+static void remove_lock_file_on_signal(int signo)
+{
+	remove_lock_file();
+	signal(SIGINT, SIG_DFL);
+	raise(signo);
+}
+
+int hold_lock_file_for_update(struct lock_file *lk, const char *path)
+{
+	int fd;
+	sprintf(lk->filename, "%s.lock", path);
+	fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
+	if (fd >=0 && !lk->next) {
+		lk->next = lock_file_list;
+		lock_file_list = lk;
+		signal(SIGINT, remove_lock_file_on_signal);
+		atexit(remove_lock_file);
+	}
+	return fd;
+}
+
+int commit_lock_file(struct lock_file *lk)
+{
+	char result_file[PATH_MAX];
+	int i;
+	strcpy(result_file, lk->filename);
+	i = strlen(result_file) - 5; /* .lock */
+	result_file[i] = 0;
+	i = rename(lk->filename, result_file);
+	lk->filename[0] = 0;
+	return i;
+}
+
+void rollback_lock_file(struct lock_file *lk)
+{
+	if (lk->filename[0])
+		unlink(lk->filename);
+	lk->filename[0] = 0;
+}
+

File update-index.c

View file
 	die("git-update-index: cannot chmod %cx '%s'", flip, path);
 }
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 static void update_one(const char *path, const char *prefix, int prefix_length)
 {
 
 	git_config(git_default_config);
 
-	newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	newfd = hold_lock_file_for_update(&lock_file, get_index_file());
 	if (newfd < 0)
-		die("unable to create new cachefile");
+		die("unable to create new index file");
 
 	entries = read_cache();
 	if (entries < 0)
  finish:
 	if (active_cache_changed) {
 		if (write_cache(newfd, active_cache, active_nr) ||
-		    commit_index_file(&cache_file))
-			die("Unable to write new cachefile");
+		    commit_lock_file(&lock_file))
+			die("Unable to write new index file");
 	}
 
 	return has_errors ? 1 : 0;

File write-tree.c

View file
 
 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
 
-static struct cache_file cache_file;
+static struct lock_file lock_file;
 
 int main(int argc, char **argv)
 {
 
 	setup_git_directory();
 
-	newfd = hold_index_file_for_update(&cache_file, get_index_file());
+	newfd = hold_lock_file_for_update(&lock_file, get_index_file());
 	entries = read_cache();
 	if (argc == 2) {
 		if (!strcmp(argv[1], "--missing-ok"))
 			die("git-write-tree: error building trees");
 		if (0 <= newfd) {
 			if (!write_cache(newfd, active_cache, active_nr))
-				commit_index_file(&cache_file);
+				commit_lock_file(&lock_file);
 		}
 		/* Not being able to write is fine -- we are only interested
 		 * in updating the cache-tree part, and if the next caller