@@ -109,58 +109,47 @@ static char *last_path_elm(char *p)
109
109
#define MAXDEPTH 5
110
110
111
111
/*
112
- * p = path that may be a symlink
113
- * s = full size of p
112
+ * path contains a path that might be a symlink.
114
113
*
115
- * If p is a symlink, attempt to overwrite p with a path to the real
116
- * file or directory (which may or may not exist), following a chain of
117
- * symlinks if necessary. Otherwise, leave p unmodified.
114
+ * If path is a symlink, attempt to overwrite it with a path to the
115
+ * real file or directory (which may or may not exist), following a
116
+ * chain of symlinks if necessary. Otherwise, leave path unmodified.
118
117
*
119
- * This is a best-effort routine. If an error occurs, p will either be
120
- * left unmodified or will name a different symlink in a symlink chain
121
- * that started with p's initial contents.
122
- *
123
- * Always returns p.
118
+ * This is a best-effort routine. If an error occurs, path will
119
+ * either be left unmodified or will name a different symlink in a
120
+ * symlink chain that started with the original path.
124
121
*/
125
-
126
- static char * resolve_symlink (char * p , size_t s )
122
+ static void resolve_symlink (struct strbuf * path )
127
123
{
128
124
int depth = MAXDEPTH ;
129
125
static struct strbuf link = STRBUF_INIT ;
130
126
131
127
while (depth -- ) {
132
- if (strbuf_readlink (& link , p , strlen ( p ) ) < 0 )
128
+ if (strbuf_readlink (& link , path -> buf , path -> len ) < 0 )
133
129
break ;
134
130
135
- if (is_absolute_path (link .buf )) {
131
+ if (is_absolute_path (link .buf ))
136
132
/* absolute path simply replaces p */
137
- if (link .len < s )
138
- strcpy (p , link .buf );
139
- else {
140
- warning ("%s: symlink too long" , p );
141
- break ;
142
- }
143
- } else {
133
+ strbuf_reset (path );
134
+ else {
144
135
/*
145
136
* link is a relative path, so replace the
146
137
* last element of p with it.
147
138
*/
148
- char * r = (char * )last_path_elm (p );
149
- if (r - p + link .len < s )
150
- strcpy (r , link .buf );
151
- else {
152
- warning ("%s: symlink too long" , p );
153
- break ;
154
- }
139
+ char * r = last_path_elm (path -> buf );
140
+ strbuf_setlen (path , r - path -> buf );
155
141
}
142
+
143
+ strbuf_addbuf (path , & link );
156
144
}
157
145
strbuf_reset (& link );
158
- return p ;
159
146
}
160
147
161
148
/* Make sure errno contains a meaningful value on error */
162
149
static int lock_file (struct lock_file * lk , const char * path , int flags )
163
150
{
151
+ size_t pathlen = strlen (path );
152
+
164
153
if (!lock_file_list ) {
165
154
/* One-time initialization */
166
155
sigchain_push_common (remove_lock_file_on_signal );
@@ -175,7 +164,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
175
164
lk -> fd = -1 ;
176
165
lk -> active = 0 ;
177
166
lk -> owner = 0 ;
178
- strbuf_init (& lk -> filename , PATH_MAX );
167
+ strbuf_init (& lk -> filename , pathlen + LOCK_SUFFIX_LEN );
179
168
lk -> next = lock_file_list ;
180
169
lock_file_list = lk ;
181
170
lk -> on_list = 1 ;
@@ -185,11 +174,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
185
174
path );
186
175
}
187
176
188
- strbuf_addstr (& lk -> filename , path );
189
- if (!(flags & LOCK_NODEREF )) {
190
- resolve_symlink (lk -> filename .buf , lk -> filename .alloc );
191
- strbuf_setlen (& lk -> filename , strlen (lk -> filename .buf ));
192
- }
177
+ strbuf_add (& lk -> filename , path , pathlen );
178
+ if (!(flags & LOCK_NODEREF ))
179
+ resolve_symlink (& lk -> filename );
193
180
strbuf_addstr (& lk -> filename , LOCK_SUFFIX );
194
181
lk -> fd = open (lk -> filename .buf , O_RDWR | O_CREAT | O_EXCL , 0666 );
195
182
if (lk -> fd < 0 ) {
0 commit comments