cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN

There are a few places that use these values, so define constants for them. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC 7108ad232fc7a4c889e82b40c52125adc9796ff5
3 files changed +14 -8
cache.h
+4
@@ -570,6 +570,10 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
570 #define REFRESH_IN_PORCELAIN 0x0020 /* user friendly output, not "needs update" */
571 extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
572
573 +/* String appended to a filename to derive the lockfile name: */
574 +#define LOCK_SUFFIX ".lock"
575 +#define LOCK_SUFFIX_LEN 5
576 +
577 struct lock_file {
578 struct lock_file *next;
579 int fd;
lockfile.c
+6 -5
@@ -166,10 +166,11 @@ static char *resolve_symlink(char *p, size_t s)
166 static int lock_file(struct lock_file *lk, const char *path, int flags)
167 {
168 /*
169 - * subtract 5 from size to make sure there's room for adding
170 - * ".lock" for the lock file name
169 + * subtract LOCK_SUFFIX_LEN from size to make sure there's
170 + * room for adding ".lock" for the lock file name:
171 */
172 - static const size_t max_path_len = sizeof(lk->filename) - 5;
172 + static const size_t max_path_len = sizeof(lk->filename) -
173 + LOCK_SUFFIX_LEN;
174
175 if (!lock_file_list) {
176 /* One-time initialization */
@@ -194,7 +195,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
195 strcpy(lk->filename, path);
196 if (!(flags & LOCK_NODEREF))
197 resolve_symlink(lk->filename, max_path_len);
197 - strcat(lk->filename, ".lock");
198 + strcat(lk->filename, LOCK_SUFFIX);
199 lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
200 if (0 <= lk->fd) {
201 lk->owner = getpid();
@@ -308,7 +309,7 @@ int commit_lock_file(struct lock_file *lk)
309 if (close_lock_file(lk))
310 return -1;
311 strcpy(result_file, lk->filename);
311 - i = strlen(result_file) - 5; /* .lock */
312 + i = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */
313 result_file[i] = 0;
314 if (rename(lk->filename, result_file))
315 return -1;
refs.c
+4 -3
@@ -79,7 +79,8 @@ out:
79 if (refname[1] == '\0')
80 return -1; /* Component equals ".". */
81 }
82 - if (cp - refname >= 5 && !memcmp(cp - 5, ".lock", 5))
82 + if (cp - refname >= LOCK_SUFFIX_LEN &&
83 + !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
84 return -1; /* Refname ends with ".lock". */
85 return cp - refname;
86 }
@@ -2602,11 +2603,11 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
2603 {
2604 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
2605 /* loose */
2605 - int err, i = strlen(lock->lk->filename) - 5; /* .lock */
2606 + int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
2607
2608 lock->lk->filename[i] = 0;
2609 err = unlink_or_warn(lock->lk->filename);
2609 - lock->lk->filename[i] = '.';
2610 + lock->lk->filename[i] = LOCK_SUFFIX[0];
2611 if (err && errno != ENOENT)
2612 return 1;
2613 }