1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#include "commands.h"
#include "command.h"
#include "instance.h"
#include "l2su.h"
#include "uuid/uuid.h"
#include "macros.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <ftw.h>
#include <unistd.h>
#define CMD_MSG_INSTANCE_LOAD_ERR "Error loading instances"
#define CMD_MSG_INSTANCE_ADD_ERR "Error adding the instance"
#define CMD_MSG_INSTANCE_SAVE_ERR "Error saving the instance"
#define CMD_MSG_INSTANCE_DEL_ERR "Error removing the instance"
#define CMD_MSG_INSTANCE_RENAME_ERR "Error renaming the instance"
#define CMD_MSG_INSTANCE_INVALID_NAME "The instance name is invalid."
#define CMD_MSG_INSTANCE_DUPLICATE "An instance named '%s' already exists."
#define CMD_MSG_INSTANCE_DIR_CREATE_E "Error creating instance directory: %s"
#define CMD_MSG_INSTANCE_DIR_CHECK_E "Error checking instance directory: %s"
#define CMD_MSG_INSTANCE_DIR_DIRTY "The instance directory '%s' is not empty. If this is intentional, rerun the command with --force."
#define CMD_MSG_INSTANCE_NAME_NOT_FOUND "An instance by the name '%s' could not be found."
#define CMD_MSG_INSTANCE_UUID_NOT_FOUND "An instance by the UUID '%s' could not be found."
#define CMD_MSG_INSTANCE_DELDIR_ERR(_msg) "Error deleting instance directory: " _msg
#define CMD_INSTANCE_CHECK(_res, _v, _msg) do { \
switch (((_res) = (_v))) { \
case INSTANCE_SUCCESS: break; \
case INSTANCE_ERRNO: \
CMD_FATAL(_msg ": %s: %s", l2_instance_errormsg[(_res)], strerror(errno)); \
break; \
default: \
CMD_FATAL(_msg ": %s", l2_instance_errormsg[(_res)]); \
} \
} while (0)
void cmd_instance__check_name(const char *instname);
unsigned cmd_instance_add(struct l2_context_node *ctx, char **args)
{
int res;
char *name = NULL;
char *path = NULL;
char *newpath = NULL;
uuid_t uuid;
char uuidstr[UUID_STRLEN+1];
bool flag_force = false;
DIR *dir = NULL;
for (char **cur = args; *cur; ++cur) {
if (!strcmp(*cur, "--force")) {
flag_force = true;
} else {
CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *cur);
}
}
CMD_INSTANCE_CHECK(res, l2_instance_load_all(), CMD_MSG_INSTANCE_LOAD_ERR);
/* get the name and path (if it exists) */
l2_cmd_collect_args(ctx, 2, "name", &name, "#location", &path);
/* make sure the name and path make sense (path should be an absolute path, name shouldn't be empty) */
cmd_instance__check_name(name);
/* make sure there is not a profile with that name already */
if (l2_instance_find_by_name(name)) {
CMD_FATAL(CMD_MSG_INSTANCE_DUPLICATE, name);
}
/* also find an unused UUID through rejection sampling */
while (true) {
l2_uuid_random(&uuid);
if (!l2_instance_find_by_uuid(&uuid)) break;
}
/* create the directory at "path" (if it isn't empty, complain) */
if (path) {
newpath = realpath(path, NULL);
if (!newpath) {
CMD_FATAL0(CMD_MSG_ALLOCATION_FAILED);
}
} else if (!path) {
newpath = strdup(l2_state.paths.data);
if (!newpath) {
newpath_append_fail:
CMD_FATAL0(CMD_MSG_ALLOCATION_FAILED);
}
newpath = l2_launcher_strapp(newpath, "/instances/");
if (!newpath) goto newpath_append_fail;
l2_uuid_to_string(&uuid, uuidstr);
newpath = l2_launcher_strapp(newpath, uuidstr);
if (!newpath) goto newpath_append_fail;
path = newpath;
}
/* TODO: now create the directory for realsies */
if (l2_launcher_mkdir_parents(path) < 0) {
CMD_FATAL(CMD_MSG_INSTANCE_DIR_CREATE_E, strerror(errno));
}
if (!flag_force) {
dir = opendir(path);
if (!dir) {
CMD_FATAL(CMD_MSG_INSTANCE_DIR_CHECK_E, strerror(errno));
}
struct dirent *ent;
errno = 0; /* readdir can return NULL on success */
while ((ent = readdir(dir))) {
if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) {
CMD_FATAL(CMD_MSG_INSTANCE_DIR_DIRTY, path);
}
errno = 0;
}
if (errno) {
CMD_FATAL(CMD_MSG_INSTANCE_DIR_CHECK_E, strerror(errno));
}
closedir(dir);
dir = NULL;
}
/* add the instance */
struct l2_instance inst;
l2_uuid_copy(&inst.uuid, &uuid);
inst.name = name;
inst.path = path;
CMD_INSTANCE_CHECK(res, l2_instance_add_instance(&inst), CMD_MSG_INSTANCE_ADD_ERR);
CMD_INSTANCE_CHECK(res, l2_instance_save_all(), CMD_MSG_INSTANCE_SAVE_ERR);
if (dir) closedir(dir);
free(newpath);
return CMD_RESULT_SUCCESS;
}
int cmd__delete_with_reckless_abandon(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
L2_UNUSED(sb);
L2_UNUSED(ftwbuf);
switch (typeflag) {
case FTW_F:
case FTW_SL:
if (unlink(fpath) < 0) return errno;
break;
case FTW_DP:
if (rmdir(fpath) < 0) return errno;
break;
/* unlink */
case FTW_NS:
case FTW_DNR:
return -2;
/* error */
}
return 0;
}
unsigned cmd_instance_remove(struct l2_context_node *ctx, char **args)
{
char *name = NULL;
bool flag_remove = false, flag_byid = false;
int res;
uuid_t instid = UUID_NULL_INIT;
struct l2_instance *to_remove = NULL;
char *removepath = NULL;
char instid_pretty[UUID_STRLEN + 1];
for (char **cur = args; *cur; ++cur) {
if (!strcmp("--delete", *cur)) {
flag_remove = true;
} else if (!strcmp("--id", *cur)) {
flag_byid = true;
} else {
CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *cur);
}
}
l2_cmd_collect_args(ctx, 1, "name", &name);
if (flag_byid && !l2_uuid_from_string(&instid, name)) {
CMD_FATAL(CMD_MSG_INVALID_UUID, name);
}
CMD_INSTANCE_CHECK(res, l2_instance_load_all(), CMD_MSG_INSTANCE_LOAD_ERR);
/* find the thing */
if (flag_byid) {
to_remove = l2_instance_find_by_uuid(&instid);
if (!to_remove) {
l2_uuid_to_string(&instid, instid_pretty);
CMD_FATAL(CMD_MSG_INSTANCE_UUID_NOT_FOUND, instid_pretty);
}
} else {
to_remove = l2_instance_find_by_name(name);
if (!to_remove) CMD_FATAL(CMD_MSG_INSTANCE_NAME_NOT_FOUND, name);
}
if (flag_remove) {
removepath = strdup(to_remove->path);
if (!removepath) {
CMD_FATAL0(CMD_MSG_ALLOCATION_FAILED);
}
}
/* remove the thing */
CMD_INSTANCE_CHECK(res, l2_instance_del_instance(to_remove), CMD_MSG_INSTANCE_DEL_ERR);
to_remove = NULL; /* del_instance frees the instance */
CMD_INSTANCE_CHECK(res, l2_instance_save_all(), CMD_MSG_INSTANCE_SAVE_ERR);
/* delete the thing if it is necessary */
if (flag_remove) {
res = nftw(removepath, &cmd__delete_with_reckless_abandon, 128, FTW_DEPTH | FTW_PHYS);
switch (res) {
case -2:
CMD_FATAL0(CMD_MSG_INSTANCE_DELDIR_ERR("unreadable file/folder encountered"));
case -1:
CMD_FATAL0(CMD_MSG_INSTANCE_DELDIR_ERR("error walking file tree"));
case 0:
break;
default:
CMD_FATAL(CMD_MSG_INSTANCE_DELDIR_ERR("error removing a file: %s"), strerror(res));
}
}
if (removepath) free(removepath);
return CMD_RESULT_SUCCESS;
}
unsigned cmd_instance_list(struct l2_context_node *ctx, char **args)
{
L2_UNUSED(ctx);
int res;
if (*args) {
CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
}
CMD_INSTANCE_CHECK(res, l2_instance_load_all(), CMD_MSG_INSTANCE_LOAD_ERR);
if (l2_state.instance_head) {
char idstr[UUID_STRLEN + 1];
for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) {
l2_uuid_to_string(&inst->uuid, idstr);
printf("- %s (%s) at '%s'\n", inst->name, idstr, inst->path);
}
} else {
fputs("There are no instances.\n", stderr);
}
return CMD_RESULT_SUCCESS;
}
unsigned cmd_instance_rename(struct l2_context_node *ctx, char **args)
{
int res;
char *oldname = NULL;
char *newname = NULL;
struct l2_instance *to_rename = NULL;
if (*args) {
CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
}
CMD_INSTANCE_CHECK(res, l2_instance_load_all(), CMD_MSG_INSTANCE_LOAD_ERR);
l2_cmd_collect_args(ctx, 2, "oldname", &oldname, "newname", &newname);
cmd_instance__check_name(newname);
if (l2_instance_find_by_name(newname)) {
CMD_FATAL(CMD_MSG_INSTANCE_DUPLICATE, newname);
}
if (!(to_rename = l2_instance_find_by_name(oldname))) {
CMD_FATAL(CMD_MSG_INSTANCE_NAME_NOT_FOUND, oldname);
}
CMD_INSTANCE_CHECK(res, l2_instance_rename_instance(to_rename, newname), CMD_MSG_INSTANCE_RENAME_ERR);
CMD_INSTANCE_CHECK(res, l2_instance_save_all(), CMD_MSG_INSTANCE_SAVE_ERR);
return CMD_RESULT_SUCCESS;
}
void cmd_instance__check_name(const char *instname)
{
if (!*instname) goto failure;
for (const char *cur = instname; *cur; ++cur) {
if (*cur <= ' ') goto failure; /* not like you couldn't just use a unicode NBSP or something */
}
return;
failure:
CMD_FATAL0(CMD_MSG_INSTANCE_INVALID_NAME);
}
|