Skip to content

Commit

Permalink
Move init_command to init.c
Browse files Browse the repository at this point in the history
  • Loading branch information
gittup committed Oct 23, 2013
1 parent 4838a6f commit caf954c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 138 deletions.
140 changes: 140 additions & 0 deletions src/tup/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
#include "variant.h"
#include "version.h"
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>

#ifdef _WIN32
#define mkdir(a,b) mkdir(a)
#endif

int tup_init(void)
{
Expand Down Expand Up @@ -112,3 +120,135 @@ void tup_valgrind_cleanup(void)
close(STDIN_FILENO);
}
}

static int mkdirtree(const char *dirname)
{
char *dirpart = strdup(dirname);
char *p;

if(!dirpart) {
perror("strdup");
return -1;
}

p = dirpart;
while(1) {
char *slash = p;
char slash_found = 0;

while(*slash && !is_path_sep(slash)) {
slash++;
}
if(*slash) {
slash_found = *slash;
*slash = 0;
}
if(mkdir(dirpart, 0777) < 0) {
if(errno != EEXIST) {
perror(dirpart);
fprintf(stderr, "tup error: Unable to create directory '%s' for a tup repository.\n", dirname);
return -1;
}
}
if(slash_found) {
*slash = slash_found;
p = slash + 1;
} else {
break;
}
}
free(dirpart);
return 0;
}

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;
}
3 changes: 2 additions & 1 deletion src/tup/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*
* tup - A file-based build system
*
* Copyright (C) 2009-2012 Mike Shal <marfey@gmail.com>
* Copyright (C) 2009-2013 Mike Shal <marfey@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand All @@ -21,3 +21,4 @@
int tup_init(void);
int tup_cleanup(void);
void tup_valgrind_cleanup(void);
int init_command(int argc, char **argv);
4 changes: 1 addition & 3 deletions src/tup/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "option.h"
#include "vardb.h"
#include "inih/ini.h"
#include "init.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
Expand Down Expand Up @@ -107,9 +108,6 @@ static struct sigaction sigact = {
};
#endif

/* From tup/main.c, used to invoke `tup init' */
int init_command(int argc, char **argv);

int tup_option_process_ini(void) {
int cur_dir;
int best_root = -1; // file descriptor -> best root candidate
Expand Down
134 changes: 0 additions & 134 deletions src/tup/tup/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#define mkdir(a,b) mkdir(a)
#endif

int init_command(int argc, char **argv);
static int graph_cb(void *arg, struct tup_entry *tent);
static int graph(int argc, char **argv);
/* Testing commands */
Expand Down Expand Up @@ -275,139 +274,6 @@ int main(int argc, char **argv)
return rc;
}

static int mkdirtree(const char *dirname)
{
char *dirpart = strdup(dirname);
char *p;

if(!dirpart) {
perror("strdup");
return -1;
}

p = dirpart;
while(1) {
char *slash = p;
char slash_found = 0;

while(*slash && !is_path_sep(slash)) {
slash++;
}
if(*slash) {
slash_found = *slash;
*slash = 0;
}
if(mkdir(dirpart, 0777) < 0) {
if(errno != EEXIST) {
perror(dirpart);
fprintf(stderr, "tup error: Unable to create directory '%s' for a tup repository.\n", dirname);
return -1;
}
}
if(slash_found) {
*slash = slash_found;
p = slash + 1;
} else {
break;
}
}
free(dirpart);
return 0;
}

/* Symbol is exported so that option can call it to automatically run tup init */
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;
}

static int show_dirs;
static int show_ghosts;
static int show_env;
Expand Down

0 comments on commit caf954c

Please sign in to comment.