1/* 2 * Copyright (c) 2005, Junio C Hamano 3 */ 4#include"cache.h" 5#include"sigchain.h" 6 7/* 8 * File write-locks as used by Git. 9 * 10 * For an overview of how to use the lockfile API, please see 11 * 12 * Documentation/technical/api-lockfile.txt 13 * 14 * This module keeps track of all locked files in lock_file_list for 15 * use at cleanup. This list and the lock_file objects that comprise 16 * it must be kept in self-consistent states at all time, because the 17 * program can be interrupted any time by a signal, in which case the 18 * signal handler will walk through the list attempting to clean up 19 * any open lock files. 20 * 21 * A lockfile is owned by the process that created it. The lock_file 22 * object has an "owner" field that records its owner. This field is 23 * used to prevent a forked process from closing a lockfile created by 24 * its parent. 25 * 26 * The possible states of a lock_file object are as follows: 27 * 28 * - Uninitialized. In this state the object's on_list field must be 29 * zero but the rest of its contents need not be initialized. As 30 * soon as the object is used in any way, it is irrevocably 31 * registered in the lock_file_list, and on_list is set. 32 * 33 * - Locked, lockfile open (after hold_lock_file_for_update(), 34 * hold_lock_file_for_append(), or reopen_lock_file()). In this 35 * state: 36 * - the lockfile exists 37 * - active is set 38 * - filename holds the filename of the lockfile 39 * - fd holds a file descriptor open for writing to the lockfile 40 * - owner holds the PID of the process that locked the file 41 * 42 * - Locked, lockfile closed (after successful close_lock_file()). 43 * Same as the previous state, except that the lockfile is closed 44 * and fd is -1. 45 * 46 * - Unlocked (after commit_lock_file(), commit_lock_file_to(), 47 * rollback_lock_file(), a failed attempt to lock, or a failed 48 * close_lock_file()). In this state: 49 * - active is unset 50 * - filename is empty (usually, though there are transitory 51 * states in which this condition doesn't hold). Client code should 52 * *not* rely on the filename being empty in this state. 53 * - fd is -1 54 * - the object is left registered in the lock_file_list, and 55 * on_list is set. 56 */ 57 58static struct lock_file *volatile lock_file_list; 59 60static voidremove_lock_files(void) 61{ 62 pid_t me =getpid(); 63 64while(lock_file_list) { 65if(lock_file_list->owner == me) 66rollback_lock_file(lock_file_list); 67 lock_file_list = lock_file_list->next; 68} 69} 70 71static voidremove_lock_files_on_signal(int signo) 72{ 73remove_lock_files(); 74sigchain_pop(signo); 75raise(signo); 76} 77 78/* 79 * path = absolute or relative path name 80 * 81 * Remove the last path name element from path (leaving the preceding 82 * "/", if any). If path is empty or the root directory ("/"), set 83 * path to the empty string. 84 */ 85static voidtrim_last_path_component(struct strbuf *path) 86{ 87int i = path->len; 88 89/* back up past trailing slashes, if any */ 90while(i && path->buf[i -1] =='/') 91 i--; 92 93/* 94 * then go backwards until a slash, or the beginning of the 95 * string 96 */ 97while(i && path->buf[i -1] !='/') 98 i--; 99 100strbuf_setlen(path, i); 101} 102 103 104/* We allow "recursive" symbolic links. Only within reason, though */ 105#define MAXDEPTH 5 106 107/* 108 * path contains a path that might be a symlink. 109 * 110 * If path is a symlink, attempt to overwrite it with a path to the 111 * real file or directory (which may or may not exist), following a 112 * chain of symlinks if necessary. Otherwise, leave path unmodified. 113 * 114 * This is a best-effort routine. If an error occurs, path will 115 * either be left unmodified or will name a different symlink in a 116 * symlink chain that started with the original path. 117 */ 118static voidresolve_symlink(struct strbuf *path) 119{ 120int depth = MAXDEPTH; 121static struct strbuf link = STRBUF_INIT; 122 123while(depth--) { 124if(strbuf_readlink(&link, path->buf, path->len) <0) 125break; 126 127if(is_absolute_path(link.buf)) 128/* absolute path simply replaces p */ 129strbuf_reset(path); 130else 131/* 132 * link is a relative path, so replace the 133 * last element of p with it. 134 */ 135trim_last_path_component(path); 136 137strbuf_addbuf(path, &link); 138} 139strbuf_reset(&link); 140} 141 142/* Make sure errno contains a meaningful value on error */ 143static intlock_file(struct lock_file *lk,const char*path,int flags) 144{ 145size_t pathlen =strlen(path); 146 147if(!lock_file_list) { 148/* One-time initialization */ 149sigchain_push_common(remove_lock_files_on_signal); 150atexit(remove_lock_files); 151} 152 153if(lk->active) 154die("BUG: cannot lock_file(\"%s\") using active struct lock_file", 155 path); 156if(!lk->on_list) { 157/* Initialize *lk and add it to lock_file_list: */ 158 lk->fd = -1; 159 lk->active =0; 160 lk->owner =0; 161strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN); 162 lk->next = lock_file_list; 163 lock_file_list = lk; 164 lk->on_list =1; 165}else if(lk->filename.len) { 166/* This shouldn't happen, but better safe than sorry. */ 167die("BUG: lock_file(\"%s\") called with improperly-reset lock_file object", 168 path); 169} 170 171strbuf_add(&lk->filename, path, pathlen); 172if(!(flags & LOCK_NO_DEREF)) 173resolve_symlink(&lk->filename); 174strbuf_addstr(&lk->filename, LOCK_SUFFIX); 175 lk->fd =open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL,0666); 176if(lk->fd <0) { 177strbuf_reset(&lk->filename); 178return-1; 179} 180 lk->owner =getpid(); 181 lk->active =1; 182if(adjust_shared_perm(lk->filename.buf)) { 183int save_errno = errno; 184error("cannot fix permission bits on%s", lk->filename.buf); 185rollback_lock_file(lk); 186 errno = save_errno; 187return-1; 188} 189return lk->fd; 190} 191 192voidunable_to_lock_message(const char*path,int err,struct strbuf *buf) 193{ 194if(err == EEXIST) { 195strbuf_addf(buf,"Unable to create '%s.lock':%s.\n\n" 196"If no other git process is currently running, this probably means a\n" 197"git process crashed in this repository earlier. Make sure no other git\n" 198"process is running and remove the file manually to continue.", 199absolute_path(path),strerror(err)); 200}else 201strbuf_addf(buf,"Unable to create '%s.lock':%s", 202absolute_path(path),strerror(err)); 203} 204 205intunable_to_lock_error(const char*path,int err) 206{ 207struct strbuf buf = STRBUF_INIT; 208 209unable_to_lock_message(path, err, &buf); 210error("%s", buf.buf); 211strbuf_release(&buf); 212return-1; 213} 214 215NORETURN voidunable_to_lock_die(const char*path,int err) 216{ 217struct strbuf buf = STRBUF_INIT; 218 219unable_to_lock_message(path, err, &buf); 220die("%s", buf.buf); 221} 222 223/* This should return a meaningful errno on failure */ 224inthold_lock_file_for_update(struct lock_file *lk,const char*path,int flags) 225{ 226int fd =lock_file(lk, path, flags); 227if(fd <0&& (flags & LOCK_DIE_ON_ERROR)) 228unable_to_lock_die(path, errno); 229return fd; 230} 231 232inthold_lock_file_for_append(struct lock_file *lk,const char*path,int flags) 233{ 234int fd, orig_fd; 235 236 fd =lock_file(lk, path, flags); 237if(fd <0) { 238if(flags & LOCK_DIE_ON_ERROR) 239unable_to_lock_die(path, errno); 240return fd; 241} 242 243 orig_fd =open(path, O_RDONLY); 244if(orig_fd <0) { 245if(errno != ENOENT) { 246if(flags & LOCK_DIE_ON_ERROR) 247die("cannot open '%s' for copying", path); 248rollback_lock_file(lk); 249returnerror("cannot open '%s' for copying", path); 250} 251}else if(copy_fd(orig_fd, fd)) { 252if(flags & LOCK_DIE_ON_ERROR) 253exit(128); 254rollback_lock_file(lk); 255return-1; 256} 257return fd; 258} 259 260intclose_lock_file(struct lock_file *lk) 261{ 262int fd = lk->fd; 263 264if(fd <0) 265return0; 266 267 lk->fd = -1; 268if(close(fd)) { 269int save_errno = errno; 270rollback_lock_file(lk); 271 errno = save_errno; 272return-1; 273} 274return0; 275} 276 277intreopen_lock_file(struct lock_file *lk) 278{ 279if(0<= lk->fd) 280die(_("BUG: reopen a lockfile that is still open")); 281if(!lk->active) 282die(_("BUG: reopen a lockfile that has been committed")); 283 lk->fd =open(lk->filename.buf, O_WRONLY); 284return lk->fd; 285} 286 287intcommit_lock_file_to(struct lock_file *lk,const char*path) 288{ 289if(!lk->active) 290die("BUG: attempt to commit unlocked object to\"%s\"", path); 291 292if(close_lock_file(lk)) 293return-1; 294 295if(rename(lk->filename.buf, path)) { 296int save_errno = errno; 297rollback_lock_file(lk); 298 errno = save_errno; 299return-1; 300} 301 302 lk->active =0; 303strbuf_reset(&lk->filename); 304return0; 305} 306 307intcommit_lock_file(struct lock_file *lk) 308{ 309static struct strbuf result_file = STRBUF_INIT; 310int err; 311 312if(!lk->active) 313die("BUG: attempt to commit unlocked object"); 314 315if(lk->filename.len <= LOCK_SUFFIX_LEN || 316strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX)) 317die("BUG: lockfile filename corrupt"); 318 319/* remove ".lock": */ 320strbuf_add(&result_file, lk->filename.buf, 321 lk->filename.len - LOCK_SUFFIX_LEN); 322 err =commit_lock_file_to(lk, result_file.buf); 323strbuf_reset(&result_file); 324return err; 325} 326 327inthold_locked_index(struct lock_file *lk,int die_on_error) 328{ 329returnhold_lock_file_for_update(lk,get_index_file(), 330 die_on_error 331? LOCK_DIE_ON_ERROR 332:0); 333} 334 335voidrollback_lock_file(struct lock_file *lk) 336{ 337if(!lk->active) 338return; 339 340if(!close_lock_file(lk)) { 341unlink_or_warn(lk->filename.buf); 342 lk->active =0; 343strbuf_reset(&lk->filename); 344} 345}