Skip to content

Commit

Permalink
Merge branch 'jc/lockfile'
Browse files Browse the repository at this point in the history
* jc/lockfile:
  ref-log: style fixes.
  refs.c: convert it to use lockfile interface.
  Make index file locking code reusable to others.
  • Loading branch information
Junio C Hamano committed Jun 6, 2006
2 parents 44fe4f5 + e5f38ec commit 9941afc
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ DIFF_OBJS = \

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 \
Expand Down
8 changes: 4 additions & 4 deletions builtin-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static int add_file_to_index(const char *path, int verbose)
return 0;
}

static struct cache_file cache_file;
static struct lock_file lock_file;

int cmd_add(int argc, const char **argv, char **envp)
{
Expand All @@ -134,9 +134,9 @@ 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");
Expand Down Expand Up @@ -181,7 +181,7 @@ int cmd_add(int argc, const char **argv, char **envp)

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");
}

Expand Down
14 changes: 9 additions & 5 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ static void write_out_results(struct patch *list, int skipped_patch)
}
}

static struct cache_file cache_file;
static struct lock_file lock_file;

static struct excludes {
struct excludes *next;
Expand Down Expand Up @@ -2133,8 +2133,12 @@ static int apply_patch(int fd, const char *filename)
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");
Expand Down Expand Up @@ -2312,8 +2316,8 @@ int cmd_apply(int argc, const char **argv, char **envp)

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;
Expand Down
8 changes: 4 additions & 4 deletions builtin-read-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ static void prime_cache_tree(void)

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)
{
Expand All @@ -864,9 +864,9 @@ 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);

Expand Down Expand Up @@ -981,7 +981,7 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
}

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;
}
6 changes: 3 additions & 3 deletions builtin-rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static int remove_file(const char *name)
return ret;
}

static struct cache_file cache_file;
static struct lock_file lock_file;

int cmd_rm(int argc, const char **argv, char **envp)
{
Expand All @@ -53,7 +53,7 @@ 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");

Expand Down Expand Up @@ -144,7 +144,7 @@ int cmd_rm(int argc, const char **argv, char **envp)

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");
}

Expand Down
12 changes: 6 additions & 6 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
#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;
Expand Down
13 changes: 6 additions & 7 deletions checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int checkout_all(void)
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)
{
Expand Down Expand Up @@ -211,9 +211,8 @@ 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;
Expand Down Expand Up @@ -262,7 +261,7 @@ int main(int argc, char **argv)
*/
if (state.refresh_cache) {
close(newfd); newfd = -1;
rollback_index_file(&cache_file);
rollback_lock_file(&lock_file);
}
state.refresh_cache = 0;
}
Expand Down Expand Up @@ -312,7 +311,7 @@ int main(int argc, char **argv)

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;
}
6 changes: 4 additions & 2 deletions fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ static int process(struct object *obj)
if (has_sha1_file(obj->sha1)) {
/* We already have it, so we should scan it now. */
obj->flags |= TO_SCAN;
} else {
}
else {
if (obj->flags & COMPLETE)
return 0;
prefetch(obj->sha1);
Expand Down Expand Up @@ -255,7 +256,8 @@ int pull(char *target)
if (write_ref_log_details) {
msg = xmalloc(strlen(write_ref_log_details) + 12);
sprintf(msg, "fetch from %s", write_ref_log_details);
} else
}
else
msg = NULL;
ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
if (msg)
Expand Down
57 changes: 0 additions & 57 deletions index.c

This file was deleted.

57 changes: 57 additions & 0 deletions lockfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;
}

Loading

0 comments on commit 9941afc

Please sign in to comment.