|
47 | 47 | #define mkdir(a,b) mkdir(a) |
48 | 48 | #endif |
49 | 49 |
|
50 | | - int init_command(int argc, char **argv); |
51 | 50 | static int graph_cb(void *arg, struct tup_entry *tent); |
52 | 51 | static int graph(int argc, char **argv); |
53 | 52 | /* Testing commands */ |
@@ -275,139 +274,6 @@ int main(int argc, char **argv) |
275 | 274 | return rc; |
276 | 275 | } |
277 | 276 |
|
278 | | -static int mkdirtree(const char *dirname) |
279 | | -{ |
280 | | - char *dirpart = strdup(dirname); |
281 | | - char *p; |
282 | | - |
283 | | - if(!dirpart) { |
284 | | - perror("strdup"); |
285 | | - return -1; |
286 | | - } |
287 | | - |
288 | | - p = dirpart; |
289 | | - while(1) { |
290 | | - char *slash = p; |
291 | | - char slash_found = 0; |
292 | | - |
293 | | - while(*slash && !is_path_sep(slash)) { |
294 | | - slash++; |
295 | | - } |
296 | | - if(*slash) { |
297 | | - slash_found = *slash; |
298 | | - *slash = 0; |
299 | | - } |
300 | | - if(mkdir(dirpart, 0777) < 0) { |
301 | | - if(errno != EEXIST) { |
302 | | - perror(dirpart); |
303 | | - fprintf(stderr, "tup error: Unable to create directory '%s' for a tup repository.\n", dirname); |
304 | | - return -1; |
305 | | - } |
306 | | - } |
307 | | - if(slash_found) { |
308 | | - *slash = slash_found; |
309 | | - p = slash + 1; |
310 | | - } else { |
311 | | - break; |
312 | | - } |
313 | | - } |
314 | | - free(dirpart); |
315 | | - return 0; |
316 | | -} |
317 | | - |
318 | | -/* Symbol is exported so that option can call it to automatically run tup init */ |
319 | | -int init_command(int argc, char **argv) |
320 | | -{ |
321 | | - int x; |
322 | | - int db_sync = 1; |
323 | | - int force_init = 0; |
324 | | - int fd; |
325 | | - const char *dirname = NULL; |
326 | | - |
327 | | - for(x=1; x<argc; x++) { |
328 | | - if(strcmp(argv[x], "--no-sync") == 0) { |
329 | | - db_sync = 0; |
330 | | - } else if(strcmp(argv[x], "--force") == 0) { |
331 | | - /* force should only be used for tup/test */ |
332 | | - force_init = 1; |
333 | | - } else { |
334 | | - if(dirname) { |
335 | | - fprintf(stderr, "tup error: Expected only one directory name for 'tup init', but got '%s' and '%s'\n", dirname, argv[x]); |
336 | | - return -1; |
337 | | - } |
338 | | - dirname = argv[x]; |
339 | | - } |
340 | | - } |
341 | | - |
342 | | - if(dirname) { |
343 | | - if(mkdirtree(dirname) < 0) |
344 | | - return -1; |
345 | | - } else { |
346 | | - dirname = "."; |
347 | | - } |
348 | | - |
349 | | - fd = open(dirname, O_RDONLY); |
350 | | - if(fd < 0) { |
351 | | - perror(dirname); |
352 | | - return -1; |
353 | | - } |
354 | | - |
355 | | - if(!force_init && find_tup_dir() == 0) { |
356 | | - char wd[PATH_MAX]; |
357 | | - if(getcwd(wd, sizeof(wd)) == NULL) { |
358 | | - perror("getcwd"); |
359 | | - fprintf(stderr, "tup warning: database already exists somewhere up the tree.\n"); |
360 | | - } else { |
361 | | - fprintf(stderr, "tup warning: database already exists in directory: %s\n", wd); |
362 | | - } |
363 | | - close(fd); |
364 | | - return 0; |
365 | | - } |
366 | | - |
367 | | - if(fchdir(fd) < 0) { |
368 | | - perror("fchdir"); |
369 | | - close(fd); |
370 | | - return -1; |
371 | | - } |
372 | | - if(close(fd) < 0) { |
373 | | - perror("close(fd)"); |
374 | | - return -1; |
375 | | - } |
376 | | - |
377 | | - if(mkdir(TUP_DIR, 0777) != 0) { |
378 | | - perror(TUP_DIR); |
379 | | - return -1; |
380 | | - } |
381 | | - |
382 | | - if(tup_db_create(db_sync) != 0) { |
383 | | - return -1; |
384 | | - } |
385 | | - |
386 | | - if(creat(TUP_OBJECT_LOCK, 0666) < 0) { |
387 | | - perror(TUP_OBJECT_LOCK); |
388 | | - return -1; |
389 | | - } |
390 | | - if(creat(TUP_SHARED_LOCK, 0666) < 0) { |
391 | | - perror(TUP_SHARED_LOCK); |
392 | | - return -1; |
393 | | - } |
394 | | - if(creat(TUP_TRI_LOCK, 0666) < 0) { |
395 | | - perror(TUP_TRI_LOCK); |
396 | | - return -1; |
397 | | - } |
398 | | - if(!db_sync) { |
399 | | - FILE *f = fopen(TUP_OPTIONS_FILE, "w"); |
400 | | - if(!f) { |
401 | | - perror(TUP_OPTIONS_FILE); |
402 | | - return -1; |
403 | | - } |
404 | | - fprintf(f, "[db]\n"); |
405 | | - fprintf(f, "\tsync = false\n"); |
406 | | - fclose(f); |
407 | | - } |
408 | | - return 0; |
409 | | -} |
410 | | - |
411 | 277 | static int show_dirs; |
412 | 278 | static int show_ghosts; |
413 | 279 | static int show_env; |
|
0 commit comments