4747 * failed attempt to lock, or a failed close_lock_file()). In this
4848 * state:
4949 * - active is unset
50- * - filename[0] == '\0' (usually, though there are transitory states
51- * in which this condition doesn't hold). Client code should *not*
52- * rely on this fact!
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.
5353 * - fd is -1
5454 * - the object is left registered in the lock_file_list, and
5555 * on_list is set.
@@ -170,13 +170,6 @@ static char *resolve_symlink(char *p, size_t s)
170170/* Make sure errno contains a meaningful value on error */
171171static int lock_file (struct lock_file * lk , const char * path , int flags )
172172{
173- /*
174- * subtract LOCK_SUFFIX_LEN from size to make sure there's
175- * room for adding ".lock" for the lock file name:
176- */
177- static const size_t max_path_len = sizeof (lk -> filename ) -
178- LOCK_SUFFIX_LEN ;
179-
180173 if (!lock_file_list ) {
181174 /* One-time initialization */
182175 sigchain_push_common (remove_lock_file_on_signal );
@@ -191,30 +184,32 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
191184 lk -> fd = -1 ;
192185 lk -> active = 0 ;
193186 lk -> owner = 0 ;
194- lk -> filename [ 0 ] = 0 ;
187+ strbuf_init ( & lk -> filename , PATH_MAX ) ;
195188 lk -> next = lock_file_list ;
196189 lock_file_list = lk ;
197190 lk -> on_list = 1 ;
191+ } else if (lk -> filename .len ) {
192+ /* This shouldn't happen, but better safe than sorry. */
193+ die ("BUG: lock_file(\"%s\") called with improperly-reset lock_file object" ,
194+ path );
198195 }
199196
200- if (strlen (path ) >= max_path_len ) {
201- errno = ENAMETOOLONG ;
202- return -1 ;
197+ strbuf_addstr (& lk -> filename , path );
198+ if (!(flags & LOCK_NODEREF )) {
199+ resolve_symlink (lk -> filename .buf , lk -> filename .alloc );
200+ strbuf_setlen (& lk -> filename , strlen (lk -> filename .buf ));
203201 }
204- strcpy (lk -> filename , path );
205- if (!(flags & LOCK_NODEREF ))
206- resolve_symlink (lk -> filename , max_path_len );
207- strcat (lk -> filename , LOCK_SUFFIX );
208- lk -> fd = open (lk -> filename , O_RDWR | O_CREAT | O_EXCL , 0666 );
202+ strbuf_addstr (& lk -> filename , LOCK_SUFFIX );
203+ lk -> fd = open (lk -> filename .buf , O_RDWR | O_CREAT | O_EXCL , 0666 );
209204 if (lk -> fd < 0 ) {
210- lk -> filename [ 0 ] = 0 ;
205+ strbuf_reset ( & lk -> filename ) ;
211206 return -1 ;
212207 }
213208 lk -> owner = getpid ();
214209 lk -> active = 1 ;
215- if (adjust_shared_perm (lk -> filename )) {
210+ if (adjust_shared_perm (lk -> filename . buf )) {
216211 int save_errno = errno ;
217- error ("cannot fix permission bits on %s" , lk -> filename );
212+ error ("cannot fix permission bits on %s" , lk -> filename . buf );
218213 rollback_lock_file (lk );
219214 errno = save_errno ;
220215 return -1 ;
@@ -313,7 +308,7 @@ int reopen_lock_file(struct lock_file *lk)
313308 die (_ ("BUG: reopen a lockfile that is still open" ));
314309 if (!lk -> active )
315310 die (_ ("BUG: reopen a lockfile that has been committed" ));
316- lk -> fd = open (lk -> filename , O_WRONLY );
311+ lk -> fd = open (lk -> filename . buf , O_WRONLY );
317312 return lk -> fd ;
318313}
319314
@@ -329,9 +324,9 @@ int commit_lock_file(struct lock_file *lk)
329324 return -1 ;
330325
331326 /* remove ".lock": */
332- strbuf_add (& result_file , lk -> filename ,
333- strlen ( lk -> filename ) - LOCK_SUFFIX_LEN );
334- err = rename (lk -> filename , result_file .buf );
327+ strbuf_add (& result_file , lk -> filename . buf ,
328+ lk -> filename . len - LOCK_SUFFIX_LEN );
329+ err = rename (lk -> filename . buf , result_file .buf );
335330 strbuf_reset (& result_file );
336331 if (err ) {
337332 int save_errno = errno ;
@@ -341,7 +336,7 @@ int commit_lock_file(struct lock_file *lk)
341336 }
342337
343338 lk -> active = 0 ;
344- lk -> filename [ 0 ] = 0 ;
339+ strbuf_reset ( & lk -> filename ) ;
345340 return 0 ;
346341}
347342
@@ -359,8 +354,8 @@ void rollback_lock_file(struct lock_file *lk)
359354 return ;
360355
361356 if (!close_lock_file (lk )) {
362- unlink_or_warn (lk -> filename );
357+ unlink_or_warn (lk -> filename . buf );
363358 lk -> active = 0 ;
364- lk -> filename [ 0 ] = 0 ;
359+ strbuf_reset ( & lk -> filename ) ;
365360 }
366361}
0 commit comments