Skip to content

Commit caf954c

Browse files
committed
Move init_command to init.c
1 parent 4838a6f commit caf954c

File tree

4 files changed

+143
-138
lines changed

4 files changed

+143
-138
lines changed

src/tup/init.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030
#include "variant.h"
3131
#include "version.h"
3232
#include <stdlib.h>
33+
#include <string.h>
34+
#include <fcntl.h>
35+
#include <errno.h>
3336
#include <unistd.h>
37+
#include <sys/stat.h>
38+
39+
#ifdef _WIN32
40+
#define mkdir(a,b) mkdir(a)
41+
#endif
3442

3543
int tup_init(void)
3644
{
@@ -112,3 +120,135 @@ void tup_valgrind_cleanup(void)
112120
close(STDIN_FILENO);
113121
}
114122
}
123+
124+
static int mkdirtree(const char *dirname)
125+
{
126+
char *dirpart = strdup(dirname);
127+
char *p;
128+
129+
if(!dirpart) {
130+
perror("strdup");
131+
return -1;
132+
}
133+
134+
p = dirpart;
135+
while(1) {
136+
char *slash = p;
137+
char slash_found = 0;
138+
139+
while(*slash && !is_path_sep(slash)) {
140+
slash++;
141+
}
142+
if(*slash) {
143+
slash_found = *slash;
144+
*slash = 0;
145+
}
146+
if(mkdir(dirpart, 0777) < 0) {
147+
if(errno != EEXIST) {
148+
perror(dirpart);
149+
fprintf(stderr, "tup error: Unable to create directory '%s' for a tup repository.\n", dirname);
150+
return -1;
151+
}
152+
}
153+
if(slash_found) {
154+
*slash = slash_found;
155+
p = slash + 1;
156+
} else {
157+
break;
158+
}
159+
}
160+
free(dirpart);
161+
return 0;
162+
}
163+
164+
int init_command(int argc, char **argv)
165+
{
166+
int x;
167+
int db_sync = 1;
168+
int force_init = 0;
169+
int fd;
170+
const char *dirname = NULL;
171+
172+
for(x=1; x<argc; x++) {
173+
if(strcmp(argv[x], "--no-sync") == 0) {
174+
db_sync = 0;
175+
} else if(strcmp(argv[x], "--force") == 0) {
176+
/* force should only be used for tup/test */
177+
force_init = 1;
178+
} else {
179+
if(dirname) {
180+
fprintf(stderr, "tup error: Expected only one directory name for 'tup init', but got '%s' and '%s'\n", dirname, argv[x]);
181+
return -1;
182+
}
183+
dirname = argv[x];
184+
}
185+
}
186+
187+
if(dirname) {
188+
if(mkdirtree(dirname) < 0)
189+
return -1;
190+
} else {
191+
dirname = ".";
192+
}
193+
194+
fd = open(dirname, O_RDONLY);
195+
if(fd < 0) {
196+
perror(dirname);
197+
return -1;
198+
}
199+
200+
if(!force_init && find_tup_dir() == 0) {
201+
char wd[PATH_MAX];
202+
if(getcwd(wd, sizeof(wd)) == NULL) {
203+
perror("getcwd");
204+
fprintf(stderr, "tup warning: database already exists somewhere up the tree.\n");
205+
} else {
206+
fprintf(stderr, "tup warning: database already exists in directory: %s\n", wd);
207+
}
208+
close(fd);
209+
return 0;
210+
}
211+
212+
if(fchdir(fd) < 0) {
213+
perror("fchdir");
214+
close(fd);
215+
return -1;
216+
}
217+
if(close(fd) < 0) {
218+
perror("close(fd)");
219+
return -1;
220+
}
221+
222+
if(mkdir(TUP_DIR, 0777) != 0) {
223+
perror(TUP_DIR);
224+
return -1;
225+
}
226+
227+
if(tup_db_create(db_sync) != 0) {
228+
return -1;
229+
}
230+
231+
if(creat(TUP_OBJECT_LOCK, 0666) < 0) {
232+
perror(TUP_OBJECT_LOCK);
233+
return -1;
234+
}
235+
if(creat(TUP_SHARED_LOCK, 0666) < 0) {
236+
perror(TUP_SHARED_LOCK);
237+
return -1;
238+
}
239+
if(creat(TUP_TRI_LOCK, 0666) < 0) {
240+
perror(TUP_TRI_LOCK);
241+
return -1;
242+
}
243+
if(!db_sync) {
244+
FILE *f = fopen(TUP_OPTIONS_FILE, "w");
245+
if(!f) {
246+
perror(TUP_OPTIONS_FILE);
247+
return -1;
248+
}
249+
fprintf(f, "[db]\n");
250+
fprintf(f, "\tsync = false\n");
251+
fclose(f);
252+
}
253+
return 0;
254+
}

src/tup/init.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* tup - A file-based build system
44
*
5-
* Copyright (C) 2009-2012 Mike Shal <marfey@gmail.com>
5+
* Copyright (C) 2009-2013 Mike Shal <marfey@gmail.com>
66
*
77
* This program is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License version 2 as
@@ -21,3 +21,4 @@
2121
int tup_init(void);
2222
int tup_cleanup(void);
2323
void tup_valgrind_cleanup(void);
24+
int init_command(int argc, char **argv);

src/tup/option.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "option.h"
3232
#include "vardb.h"
3333
#include "inih/ini.h"
34+
#include "init.h"
3435
#include <assert.h>
3536
#include <errno.h>
3637
#include <fcntl.h>
@@ -107,9 +108,6 @@ static struct sigaction sigact = {
107108
};
108109
#endif
109110

110-
/* From tup/main.c, used to invoke `tup init' */
111-
int init_command(int argc, char **argv);
112-
113111
int tup_option_process_ini(void) {
114112
int cur_dir;
115113
int best_root = -1; // file descriptor -> best root candidate

src/tup/tup/main.c

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#define mkdir(a,b) mkdir(a)
4848
#endif
4949

50-
int init_command(int argc, char **argv);
5150
static int graph_cb(void *arg, struct tup_entry *tent);
5251
static int graph(int argc, char **argv);
5352
/* Testing commands */
@@ -275,139 +274,6 @@ int main(int argc, char **argv)
275274
return rc;
276275
}
277276

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-
411277
static int show_dirs;
412278
static int show_ghosts;
413279
static int show_env;

0 commit comments

Comments
 (0)