1 #include "common.h"
2 #include "log.h"
3 
4 #include <getopt.h>
5 
6 #include "seafile-session.h"
7 #include "gc-core.h"
8 #include "verify.h"
9 
10 #include "utils.h"
11 
12 static char *ccnet_dir = NULL;
13 static char *seafile_dir = NULL;
14 static char *central_config_dir = NULL;
15 
16 SeafileSession *seaf;
17 
18 static const char *short_opts = "hvc:d:VDrF:";
19 static const struct option long_opts[] = {
20     { "help", no_argument, NULL, 'h', },
21     { "version", no_argument, NULL, 'v', },
22     { "config-file", required_argument, NULL, 'c', },
23     { "central-config-dir", required_argument, NULL, 'F' },
24     { "seafdir", required_argument, NULL, 'd', },
25     { "verbose", no_argument, NULL, 'V' },
26     { "dry-run", no_argument, NULL, 'D' },
27     { "rm-deleted", no_argument, NULL, 'r' },
28     { 0, 0, 0, 0 },
29 };
30 
usage()31 static void usage ()
32 {
33     fprintf (stderr,
34              "usage: seafserv-gc [-c config_dir] [-d seafile_dir] "
35              "[repo_id_1 [repo_id_2 ...]]\n"
36              "Additional options:\n"
37              "-r, --rm-deleted: remove garbaged repos\n"
38              "-D, --dry-run: report blocks that can be remove, but not remove them\n"
39              "-V, --verbose: verbose output messages\n");
40 }
41 
42 #ifdef WIN32
43 /* Get the commandline arguments in unicode, then convert them to utf8  */
44 static char **
get_argv_utf8(int * argc)45 get_argv_utf8 (int *argc)
46 {
47     int i = 0;
48     char **argv = NULL;
49     const wchar_t *cmdline = NULL;
50     wchar_t **argv_w = NULL;
51 
52     cmdline = GetCommandLineW();
53     argv_w = CommandLineToArgvW (cmdline, argc);
54     if (!argv_w) {
55         printf("failed to CommandLineToArgvW(), GLE=%lu\n", GetLastError());
56         return NULL;
57     }
58 
59     argv = (char **)malloc (sizeof(char*) * (*argc));
60     for (i = 0; i < *argc; i++) {
61         argv[i] = wchar_to_utf8 (argv_w[i]);
62     }
63 
64     return argv;
65 }
66 #endif
67 
68 int
main(int argc,char * argv[])69 main(int argc, char *argv[])
70 {
71     int c;
72     int verbose = 0;
73     int dry_run = 0;
74     int rm_garbage = 0;
75 
76 #ifdef WIN32
77     argv = get_argv_utf8 (&argc);
78 #endif
79 
80     ccnet_dir = DEFAULT_CONFIG_DIR;
81 
82     while ((c = getopt_long(argc, argv,
83                 short_opts, long_opts, NULL)) != EOF) {
84         switch (c) {
85         case 'h':
86             usage();
87             exit(0);
88         case 'v':
89             exit(-1);
90             break;
91         case 'c':
92             ccnet_dir = strdup(optarg);
93             break;
94         case 'd':
95             seafile_dir = strdup(optarg);
96             break;
97         case 'F':
98             central_config_dir = strdup(optarg);
99             break;
100         case 'V':
101             verbose = 1;
102             break;
103         case 'D':
104             dry_run = 1;
105             break;
106         case 'r':
107             rm_garbage = 1;
108             break;
109         default:
110             usage();
111             exit(-1);
112         }
113     }
114 
115 #if !GLIB_CHECK_VERSION(2, 35, 0)
116     g_type_init();
117 #endif
118 
119     if (seafile_log_init ("-", "info", "debug") < 0) {
120         seaf_warning ("Failed to init log.\n");
121         exit (1);
122     }
123 
124     if (seafile_dir == NULL)
125         seafile_dir = g_build_filename (ccnet_dir, "seafile-data", NULL);
126 
127     seaf = seafile_session_new(central_config_dir, seafile_dir, ccnet_dir, TRUE);
128     if (!seaf) {
129         seaf_warning ("Failed to create seafile session.\n");
130         exit (1);
131     }
132 
133     if (rm_garbage) {
134         delete_garbaged_repos (dry_run);
135         return 0;
136     }
137 
138     GList *repo_id_list = NULL;
139     int i;
140     for (i = optind; i < argc; i++)
141         repo_id_list = g_list_append (repo_id_list, g_strdup(argv[i]));
142 
143     gc_core_run (repo_id_list, dry_run, verbose);
144 
145     return 0;
146 }
147