@@ -36,10 +36,11 @@ static void safe_create_dir(const char *dir, int share)
36
36
die (_ ("Could not make %s writable by group" ), dir );
37
37
}
38
38
39
- static void copy_templates_1 (char * path , int baselen ,
40
- char * template , int template_baselen ,
39
+ static void copy_templates_1 (struct strbuf * path , struct strbuf * template ,
41
40
DIR * dir )
42
41
{
42
+ size_t path_baselen = path -> len ;
43
+ size_t template_baselen = template -> len ;
43
44
struct dirent * de ;
44
45
45
46
/* Note: if ".git/hooks" file exists in the repository being
@@ -49,77 +50,64 @@ static void copy_templates_1(char *path, int baselen,
49
50
* with the way the namespace under .git/ is organized, should
50
51
* be really carefully chosen.
51
52
*/
52
- safe_create_dir (path , 1 );
53
+ safe_create_dir (path -> buf , 1 );
53
54
while ((de = readdir (dir )) != NULL ) {
54
55
struct stat st_git , st_template ;
55
- int namelen ;
56
56
int exists = 0 ;
57
57
58
+ strbuf_setlen (path , path_baselen );
59
+ strbuf_setlen (template , template_baselen );
60
+
58
61
if (de -> d_name [0 ] == '.' )
59
62
continue ;
60
- namelen = strlen (de -> d_name );
61
- if ((PATH_MAX <= baselen + namelen ) ||
62
- (PATH_MAX <= template_baselen + namelen ))
63
- die (_ ("insanely long template name %s" ), de -> d_name );
64
- memcpy (path + baselen , de -> d_name , namelen + 1 );
65
- memcpy (template + template_baselen , de -> d_name , namelen + 1 );
66
- if (lstat (path , & st_git )) {
63
+ strbuf_addstr (path , de -> d_name );
64
+ strbuf_addstr (template , de -> d_name );
65
+ if (lstat (path -> buf , & st_git )) {
67
66
if (errno != ENOENT )
68
- die_errno (_ ("cannot stat '%s'" ), path );
67
+ die_errno (_ ("cannot stat '%s'" ), path -> buf );
69
68
}
70
69
else
71
70
exists = 1 ;
72
71
73
- if (lstat (template , & st_template ))
74
- die_errno (_ ("cannot stat template '%s'" ), template );
72
+ if (lstat (template -> buf , & st_template ))
73
+ die_errno (_ ("cannot stat template '%s'" ), template -> buf );
75
74
76
75
if (S_ISDIR (st_template .st_mode )) {
77
- DIR * subdir = opendir (template );
78
- int baselen_sub = baselen + namelen ;
79
- int template_baselen_sub = template_baselen + namelen ;
76
+ DIR * subdir = opendir (template -> buf );
80
77
if (!subdir )
81
- die_errno (_ ("cannot opendir '%s'" ), template );
82
- path [baselen_sub ++ ] =
83
- template [template_baselen_sub ++ ] = '/' ;
84
- path [baselen_sub ] =
85
- template [template_baselen_sub ] = 0 ;
86
- copy_templates_1 (path , baselen_sub ,
87
- template , template_baselen_sub ,
88
- subdir );
78
+ die_errno (_ ("cannot opendir '%s'" ), template -> buf );
79
+ strbuf_addch (path , '/' );
80
+ strbuf_addch (template , '/' );
81
+ copy_templates_1 (path , template , subdir );
89
82
closedir (subdir );
90
83
}
91
84
else if (exists )
92
85
continue ;
93
86
else if (S_ISLNK (st_template .st_mode )) {
94
- char lnk [256 ];
95
- int len ;
96
- len = readlink (template , lnk , sizeof (lnk ));
97
- if (len < 0 )
98
- die_errno (_ ("cannot readlink '%s'" ), template );
99
- if (sizeof (lnk ) <= len )
100
- die (_ ("insanely long symlink %s" ), template );
101
- lnk [len ] = 0 ;
102
- if (symlink (lnk , path ))
103
- die_errno (_ ("cannot symlink '%s' '%s'" ), lnk , path );
87
+ struct strbuf lnk = STRBUF_INIT ;
88
+ if (strbuf_readlink (& lnk , template -> buf , 0 ) < 0 )
89
+ die_errno (_ ("cannot readlink '%s'" ), template -> buf );
90
+ if (symlink (lnk .buf , path -> buf ))
91
+ die_errno (_ ("cannot symlink '%s' '%s'" ),
92
+ lnk .buf , path -> buf );
93
+ strbuf_release (& lnk );
104
94
}
105
95
else if (S_ISREG (st_template .st_mode )) {
106
- if (copy_file (path , template , st_template .st_mode ))
107
- die_errno (_ ("cannot copy '%s' to '%s'" ), template ,
108
- path );
96
+ if (copy_file (path -> buf , template -> buf , st_template .st_mode ))
97
+ die_errno (_ ("cannot copy '%s' to '%s'" ),
98
+ template -> buf , path -> buf );
109
99
}
110
100
else
111
- error (_ ("ignoring template %s" ), template );
101
+ error (_ ("ignoring template %s" ), template -> buf );
112
102
}
113
103
}
114
104
115
105
static void copy_templates (const char * template_dir )
116
106
{
117
- char path [ PATH_MAX ] ;
118
- char template_path [ PATH_MAX ] ;
119
- int template_len ;
107
+ struct strbuf path = STRBUF_INIT ;
108
+ struct strbuf template_path = STRBUF_INIT ;
109
+ size_t template_len ;
120
110
DIR * dir ;
121
- const char * git_dir = get_git_dir ();
122
- int len = strlen (git_dir );
123
111
char * to_free = NULL ;
124
112
125
113
if (!template_dir )
@@ -132,26 +120,23 @@ static void copy_templates(const char *template_dir)
132
120
free (to_free );
133
121
return ;
134
122
}
135
- template_len = strlen (template_dir );
136
- if (PATH_MAX <= (template_len + strlen ("/config" )))
137
- die (_ ("insanely long template path %s" ), template_dir );
138
- strcpy (template_path , template_dir );
139
- if (template_path [template_len - 1 ] != '/' ) {
140
- template_path [template_len ++ ] = '/' ;
141
- template_path [template_len ] = 0 ;
142
- }
143
- dir = opendir (template_path );
123
+
124
+ strbuf_addstr (& template_path , template_dir );
125
+ strbuf_complete (& template_path , '/' );
126
+ template_len = template_path .len ;
127
+
128
+ dir = opendir (template_path .buf );
144
129
if (!dir ) {
145
130
warning (_ ("templates not found %s" ), template_dir );
146
131
goto free_return ;
147
132
}
148
133
149
134
/* Make sure that template is from the correct vintage */
150
- strcpy ( template_path + template_len , "config" );
135
+ strbuf_addstr ( & template_path , "config" );
151
136
repository_format_version = 0 ;
152
137
git_config_from_file (check_repository_format_version ,
153
- template_path , NULL );
154
- template_path [ template_len ] = 0 ;
138
+ template_path . buf , NULL );
139
+ strbuf_setlen ( & template_path , template_len ) ;
155
140
156
141
if (repository_format_version &&
157
142
repository_format_version != GIT_REPO_VERSION ) {
@@ -162,17 +147,15 @@ static void copy_templates(const char *template_dir)
162
147
goto close_free_return ;
163
148
}
164
149
165
- memcpy (path , git_dir , len );
166
- if (len && path [len - 1 ] != '/' )
167
- path [len ++ ] = '/' ;
168
- path [len ] = 0 ;
169
- copy_templates_1 (path , len ,
170
- template_path , template_len ,
171
- dir );
150
+ strbuf_addstr (& path , get_git_dir ());
151
+ strbuf_complete (& path , '/' );
152
+ copy_templates_1 (& path , & template_path , dir );
172
153
close_free_return :
173
154
closedir (dir );
174
155
free_return :
175
156
free (to_free );
157
+ strbuf_release (& path );
158
+ strbuf_release (& template_path );
176
159
}
177
160
178
161
static int git_init_db_config (const char * k , const char * v , void * cb )
@@ -199,28 +182,20 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
199
182
200
183
static int create_default_files (const char * template_path )
201
184
{
202
- const char * git_dir = get_git_dir ();
203
- unsigned len = strlen (git_dir );
204
- static char path [PATH_MAX ];
205
185
struct stat st1 ;
186
+ struct strbuf buf = STRBUF_INIT ;
187
+ char * path ;
206
188
char repo_version_string [10 ];
207
189
char junk [2 ];
208
190
int reinit ;
209
191
int filemode ;
210
192
211
- if (len > sizeof (path )- 50 )
212
- die (_ ("insane git directory %s" ), git_dir );
213
- memcpy (path , git_dir , len );
214
-
215
- if (len && path [len - 1 ] != '/' )
216
- path [len ++ ] = '/' ;
217
-
218
193
/*
219
194
* Create .git/refs/{heads,tags}
220
195
*/
221
- safe_create_dir (git_path ( "refs" ), 1 );
222
- safe_create_dir (git_path ( "refs/heads" ), 1 );
223
- safe_create_dir (git_path ( "refs/tags" ), 1 );
196
+ safe_create_dir (git_path_buf ( & buf , "refs" ), 1 );
197
+ safe_create_dir (git_path_buf ( & buf , "refs/heads" ), 1 );
198
+ safe_create_dir (git_path_buf ( & buf , "refs/tags" ), 1 );
224
199
225
200
/* Just look for `init.templatedir` */
226
201
git_config (git_init_db_config , NULL );
@@ -244,16 +219,16 @@ static int create_default_files(const char *template_path)
244
219
*/
245
220
if (shared_repository ) {
246
221
adjust_shared_perm (get_git_dir ());
247
- adjust_shared_perm (git_path ( "refs" ));
248
- adjust_shared_perm (git_path ( "refs/heads" ));
249
- adjust_shared_perm (git_path ( "refs/tags" ));
222
+ adjust_shared_perm (git_path_buf ( & buf , "refs" ));
223
+ adjust_shared_perm (git_path_buf ( & buf , "refs/heads" ));
224
+ adjust_shared_perm (git_path_buf ( & buf , "refs/tags" ));
250
225
}
251
226
252
227
/*
253
228
* Create the default symlink from ".git/HEAD" to the "master"
254
229
* branch, if it does not exist yet.
255
230
*/
256
- strcpy ( path + len , "HEAD" );
231
+ path = git_path_buf ( & buf , "HEAD" );
257
232
reinit = (!access (path , R_OK )
258
233
|| readlink (path , junk , sizeof (junk )- 1 ) != -1 );
259
234
if (!reinit ) {
@@ -266,10 +241,8 @@ static int create_default_files(const char *template_path)
266
241
"%d" , GIT_REPO_VERSION );
267
242
git_config_set ("core.repositoryformatversion" , repo_version_string );
268
243
269
- path [len ] = 0 ;
270
- strcpy (path + len , "config" );
271
-
272
244
/* Check filemode trustability */
245
+ path = git_path_buf (& buf , "config" );
273
246
filemode = TEST_FILEMODE ;
274
247
if (TEST_FILEMODE && !lstat (path , & st1 )) {
275
248
struct stat st2 ;
@@ -290,14 +263,13 @@ static int create_default_files(const char *template_path)
290
263
/* allow template config file to override the default */
291
264
if (log_all_ref_updates == -1 )
292
265
git_config_set ("core.logallrefupdates" , "true" );
293
- if (needs_work_tree_config (git_dir , work_tree ))
266
+ if (needs_work_tree_config (get_git_dir () , work_tree ))
294
267
git_config_set ("core.worktree" , work_tree );
295
268
}
296
269
297
270
if (!reinit ) {
298
271
/* Check if symlink is supported in the work tree */
299
- path [len ] = 0 ;
300
- strcpy (path + len , "tXXXXXX" );
272
+ path = git_path_buf (& buf , "tXXXXXX" );
301
273
if (!close (xmkstemp (path )) &&
302
274
!unlink (path ) &&
303
275
!symlink ("testing" , path ) &&
@@ -308,31 +280,35 @@ static int create_default_files(const char *template_path)
308
280
git_config_set ("core.symlinks" , "false" );
309
281
310
282
/* Check if the filesystem is case-insensitive */
311
- path [len ] = 0 ;
312
- strcpy (path + len , "CoNfIg" );
283
+ path = git_path_buf (& buf , "CoNfIg" );
313
284
if (!access (path , F_OK ))
314
285
git_config_set ("core.ignorecase" , "true" );
315
286
probe_utf8_pathname_composition ();
316
287
}
317
288
289
+ strbuf_release (& buf );
318
290
return reinit ;
319
291
}
320
292
321
293
static void create_object_directory (void )
322
294
{
323
- const char * object_directory = get_object_directory ();
324
- int len = strlen (object_directory );
325
- char * path = xmalloc (len + 40 );
295
+ struct strbuf path = STRBUF_INIT ;
296
+ size_t baselen ;
297
+
298
+ strbuf_addstr (& path , get_object_directory ());
299
+ baselen = path .len ;
300
+
301
+ safe_create_dir (path .buf , 1 );
326
302
327
- memcpy (path , object_directory , len );
303
+ strbuf_setlen (& path , baselen );
304
+ strbuf_addstr (& path , "/pack" );
305
+ safe_create_dir (path .buf , 1 );
328
306
329
- safe_create_dir (object_directory , 1 );
330
- strcpy (path + len , "/pack" );
331
- safe_create_dir (path , 1 );
332
- strcpy (path + len , "/info" );
333
- safe_create_dir (path , 1 );
307
+ strbuf_setlen (& path , baselen );
308
+ strbuf_addstr (& path , "/info" );
309
+ safe_create_dir (path .buf , 1 );
334
310
335
- free ( path );
311
+ strbuf_release ( & path );
336
312
}
337
313
338
314
int set_git_dir_init (const char * git_dir , const char * real_git_dir ,
0 commit comments