int tup_init(void) { if(find_tup_dir() != 0) { fprintf(stderr, "tup %s usage: tup [args]\n", tup_version()); fprintf(stderr, "For information on Tupfiles and other commands, see the tup(1) man page.\n"); fprintf(stderr, "No .tup directory found. Either create a Tupfile.ini file at the top of your project, or manually run 'tup init' there.\n"); return -1; } if(tup_entry_init() < 0) { return -1; } if(server_pre_init() < 0) { return -1; } if(tup_drop_privs() < 0) { goto out_err; } if(open_tup_top() < 0) { goto out_err; } if(tup_lock_init() < 0) { goto out_err; } color_init(); if(tup_db_open() != 0) { goto out_unlock; } return 0; out_unlock: tup_lock_exit(); out_err: server_post_exit(); return -1; }
int init_command(int argc, char **argv) { int x; int db_sync = 1; int force_init = 0; int fd; const char *dirname = NULL; for(x=1; x<argc; x++) { if(strcmp(argv[x], "--no-sync") == 0) { db_sync = 0; } else if(strcmp(argv[x], "--force") == 0) { /* force should only be used for tup/test */ force_init = 1; } else { if(dirname) { fprintf(stderr, "tup error: Expected only one directory name for 'tup init', but got '%s' and '%s'\n", dirname, argv[x]); return -1; } dirname = argv[x]; } } if(dirname) { if(mkdirtree(dirname) < 0) return -1; } else { dirname = "."; } fd = open(dirname, O_RDONLY); if(fd < 0) { perror(dirname); return -1; } if(!force_init && find_tup_dir() == 0) { char wd[PATH_MAX]; if(getcwd(wd, sizeof(wd)) == NULL) { perror("getcwd"); fprintf(stderr, "tup warning: database already exists somewhere up the tree.\n"); } else { fprintf(stderr, "tup warning: database already exists in directory: %s\n", wd); } close(fd); return 0; } if(fchdir(fd) < 0) { perror("fchdir"); close(fd); return -1; } if(close(fd) < 0) { perror("close(fd)"); return -1; } if(mkdir(TUP_DIR, 0777) != 0) { perror(TUP_DIR); return -1; } if(tup_db_create(db_sync) != 0) { return -1; } if(creat(TUP_OBJECT_LOCK, 0666) < 0) { perror(TUP_OBJECT_LOCK); return -1; } if(creat(TUP_SHARED_LOCK, 0666) < 0) { perror(TUP_SHARED_LOCK); return -1; } if(creat(TUP_TRI_LOCK, 0666) < 0) { perror(TUP_TRI_LOCK); return -1; } if(!db_sync) { FILE *f = fopen(TUP_OPTIONS_FILE, "w"); if(!f) { perror(TUP_OPTIONS_FILE); return -1; } fprintf(f, "[db]\n"); fprintf(f, "\tsync = false\n"); fclose(f); } return 0; }