aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd-instance.c
blob: 6982eb352d66acff415799a959b13f7278ebca72 (plain) (blame)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#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>

unsigned cmd_instance_add(struct l2_context_node *ctx, char **args)
{
  int res = l2_instance_load_all();
  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 {
      fprintf(stderr, "fatal: Unknown argument '%s'.\n", *cur);
      return CMD_RESULT_FAIL;
    }
  }

  if (res != INSTANCE_SUCCESS) {
    if (res == INSTANCE_ERRNO) {
      fprintf(stderr, "fatal: Error loading instances: %s\n", strerror(errno));
    } else {
      fprintf(stderr, "fatal: Error loading instances (%d)\n", res);
    }
    return CMD_RESULT_FAIL;
  }
  
  /* get the name and path (if it exists) */
  for (struct l2_context_node *cur = ctx; cur; cur = cur->next) {
    if (cur->node->type != CMD_NODE_TYPE_ARGUMENT) continue;
    if (!strcmp("name", cur->node->name)) {
      name = cur->value;
    } else if (!strcmp("location", cur->node->name)) {
      path = cur->value;
    }
  }

  /* make sure the name and path make sense (path should be an absolute path, name shouldn't be empty) */
  if (!name || !*name) {
    fputs("fatal: The specified name is invalid\n", stderr);
    return CMD_RESULT_FAIL;
  }

  for (char *cur = name; *cur; ++cur) {
    if (*cur < ' ') {
      fputs("fatal: The specified name contains invalid characters\n", stderr);
      return CMD_RESULT_FAIL;
    }
  }

  /* make sure there is not a profile with that name already */
  for (struct l2_instance *cur = l2_state.instance_head; cur; cur = cur->next) {
    if (!strcmp(cur->name, name)) {
      fprintf(stderr, "fatal: An instance by the name '%s' already exists.\n", cur->name);
      return CMD_RESULT_FAIL;
    }
  }

  /* also find an unused UUID through rejection sampling */
  bool found;
  do {
    found = false;
    l2_uuid_random(&uuid);
    for (struct l2_instance *cur = l2_state.instance_head; cur; cur = cur->next) {
      if (!l2_uuid_compare(&cur->uuid, &uuid)) {
        found = true;
        break;
      }
    }
  } while (found);

  /* create the directory at "path" (if it isn't empty, complain) */
  if (path) {
    newpath = realpath(path, NULL);
    if (!newpath) {
      fprintf(stderr, "fatal: Failed to canonicalize specified path: %s\n", strerror(errno));
      return CMD_RESULT_FAIL;
    }
  } else if (!path) {
    newpath = strdup(l2_state.paths.data);
    if (!newpath) {
    newpath_append_fail:
      free(newpath);
      fprintf(stderr, "fatal: Allocation failed: %s\n", strerror(errno));
      return CMD_RESULT_FAIL;
    }

    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) {
    fprintf(stderr, "fatal: Could not create instance directory: %s\n", strerror(errno));
    res = CMD_RESULT_FAIL;
    goto cleanup;
  }

  if (!flag_force) {
    dir = opendir(path);
    if (!dir) {
      fprintf(stderr, "fatal: Could not check instance directory: %s\n", strerror(errno));
      res = CMD_RESULT_FAIL;
      goto cleanup;
    }

    struct dirent *ent;
    errno = 0;
    while ((ent = readdir(dir))) {
      if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) {
        fprintf(stderr, "fatal: Instance directory '%s' is not empty. If this is intentional, rerun the command with --force.\n", path);
        res = CMD_RESULT_FAIL;
        goto cleanup;
      }
    }

    closedir(dir);
    dir = NULL;

    if (errno) {
      fprintf(stderr, "fatal: Error scanning instance directory: %s\n", strerror(errno));
      res = CMD_RESULT_FAIL;
      goto cleanup;
    }
  }

  /* add the instance */

  struct l2_instance inst;

  inst.uuid = uuid;
  inst.name = name;
  inst.path = path;

  if ((res = l2_instance_add_instance(&inst)) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to add instance (%d)\n", res);
    res = CMD_RESULT_FAIL;
    goto cleanup;
  }

  if ((res = l2_instance_save_all()) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to add instance (%d)\n", res);
    res = CMD_RESULT_FAIL;
    goto cleanup;
  }

  res = CMD_RESULT_SUCCESS;

cleanup:
  if (dir) closedir(dir);
  free(newpath);
  return res;
}

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;

  for (char **cur = args; *cur; ++cur) {
    if (!strcmp("--delete", *cur)) {
      flag_remove = true;
    } else if (!strcmp("--id", *cur)) {
      flag_byid = true;
    } else {
      fprintf(stderr, "fatal: Unknown argument '%s'\n", *cur);
      return CMD_RESULT_FAIL;
    }
  }

  for (struct l2_context_node *cur = ctx; cur; cur = cur->next) {
    if (cur->node->type != CMD_NODE_TYPE_ARGUMENT) continue;

    if (!strcmp("name", cur->node->name)) {
      name = cur->value;
    }
  }

  if (!name) {
    fputs("fatal: Name not specified\n", stderr);
    return CMD_RESULT_FAIL;
  }

  if (flag_byid && !l2_uuid_from_string(&instid, name)) {
    fprintf(stderr, "Failed to parse instance UUID '%s'.\n", name);
    return CMD_RESULT_FAIL;
  }

  res = l2_instance_load_all();
  if (res != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to load instances (%d).\n", res);
    return CMD_RESULT_FAIL;
  }

  /* find the thing */
  if (flag_byid) {
    for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) {
      if (!l2_uuid_compare(&inst->uuid, &instid)) {
        to_remove = inst;
        break;
      }
    }
  } else {
    for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) {
      if (!strcmp(inst->name, name)) {
        to_remove = inst;
        break;
      }
    }
  }

  if (!to_remove) {
    fputs("fatal: Instance not found.\n", stderr);
    return CMD_RESULT_FAIL;
  }

  if (flag_remove) {
    removepath = strdup(to_remove->path);
    if (!removepath) {
      fputs("fatal: Failed to create path.\n", stderr);
      return CMD_RESULT_FAIL;
    }
  }

  /* remove the thing */
  if ((res = l2_instance_del_instance(to_remove)) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to remove instance (%d).\n", res);
    return CMD_RESULT_FAIL;
  }

  to_remove = NULL; /* del_instance frees the instance */

  if ((res = l2_instance_save_all()) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to save instances (%d).\n", res);
    return CMD_RESULT_FAIL;
  }

  /* 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:
        fputs("Unreadable file/folder encountered while walking file tree.\n", stderr);
        res = CMD_RESULT_FAIL;
        goto cleanup;
      case -1:
        fputs("Error walking file tree.\n", stderr);
        res = CMD_RESULT_FAIL;
        goto cleanup;
      case 0:
        break;
      default:
        fprintf(stderr, "Error removing a file: %s\n", strerror(res));
        res = CMD_RESULT_FAIL;
        goto cleanup;
    }
  }

  res = CMD_RESULT_SUCCESS;

cleanup:
  if (removepath) free(removepath);
  return res;
}

unsigned cmd_instance_list(struct l2_context_node *ctx, char **args)
{
  L2_UNUSED(ctx);

  if (*args) {
    fprintf(stderr, "fatal: Unknown argument '%s'.\n", *args);
    return CMD_RESULT_FAIL;
  }

  int res = l2_instance_load_all();

  if (res != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to load instances. (%d)\n", res);
    return CMD_RESULT_FAIL;
  }

  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) {
    fprintf(stderr, "fatal: Unknown argument '%s'.\n", *args);
    return CMD_RESULT_FAIL;
  }

  res = l2_instance_load_all();

  if (res != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to load instances. (%d)\n", res);
    return CMD_RESULT_FAIL;
  }

  for (struct l2_context_node *cur = ctx; cur; cur = cur->next) {
    if (!strcmp("oldname", cur->node->name)) {
      oldname = cur->value;
    } else if (!strcmp("newname", cur->node->name)) {
      newname = cur->value;
    }
  }

  if (!(oldname && newname)) { /* TODO: sanitize newname like in "instance add" */
    fputs("fatal: Invalid usage.\n", stderr);
    return CMD_RESULT_FAIL;
  }

  for (struct l2_instance *cur = l2_state.instance_head; cur; cur = cur->next) {
    if (!strcmp(cur->name, oldname)) {
      to_rename = cur;
    } else if (!strcmp(cur->name, newname)) {
      fprintf(stderr, "fatal: An instance by the name '%s' already exists.\n", newname);
      return CMD_RESULT_FAIL;
    }
  }

  if (!to_rename) {
    fprintf(stderr, "fatal: No instance found by the name '%s'.\n", oldname);
    return CMD_RESULT_FAIL;
  }

  if ((res = l2_instance_rename_instance(to_rename, newname)) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to rename the instance. (%d)\n", res);
    return CMD_RESULT_FAIL;
  }

  if ((res = l2_instance_save_all()) != INSTANCE_SUCCESS) {
    fprintf(stderr, "fatal: Failed to save instances. (%d)\n", res);
    return CMD_RESULT_FAIL;
  }

  return CMD_RESULT_SUCCESS;
}