]> git.ozlabs.org Git - petitboot/blob - lib/system/system.c
discover/grub2: Allow to separate the --id argument using a space char
[petitboot] / lib / system / system.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14
15 #include "log/log.h"
16 #include <talloc/talloc.h>
17 #include "system.h"
18
19 const struct pb_system_apps pb_system_apps = {
20         .prefix         = PREFIX,
21         .cp             = HOST_PROG_CP,
22         .kexec          = HOST_PROG_KEXEC,
23         .mount          = HOST_PROG_MOUNT,
24         .shutdown       = HOST_PROG_SHUTDOWN,
25         .sftp           = HOST_PROG_SFTP,
26         .tftp           = HOST_PROG_TFTP,
27         .umount         = HOST_PROG_UMOUNT,
28         .wget           = HOST_PROG_WGET,
29         .ip             = HOST_PROG_IP,
30         .udhcpc         = HOST_PROG_UDHCPC,
31         .udhcpc6        = HOST_PROG_UDHCPC6,
32         .vgscan         = HOST_PROG_VGSCAN,
33         .vgchange       = HOST_PROG_VGCHANGE,
34         .pb_plugin      = HOST_PROG_PB_PLUGIN,
35         .pb_exec        = HOST_PROG_PB_EXEC,
36         .sh             = HOST_PROG_SH,
37         .scsi_rescan    = HOST_PROG_SCSI_RESCAN,
38         .dmidecode      = HOST_PROG_DMIDECODE,
39         .cryptsetup     = HOST_PROG_CRYPTSETUP,
40 };
41
42 #ifndef TFTP_TYPE
43 #define TFTP_TYPE TFTP_TYPE_UNKNOWN
44 #endif
45
46 enum tftp_type tftp_type = TFTP_TYPE;
47
48 int pb_mkdir_recursive(const char *dir)
49 {
50         struct stat statbuf;
51         int rc, mode = 0755;
52         char *str, *sep;
53
54         if (!*dir)
55                 return 0;
56
57         if (!stat(dir, &statbuf)) {
58                 if (!S_ISDIR(statbuf.st_mode)) {
59                         pb_log("%s: %s exists, but isn't a directory\n",
60                                         __func__, dir);
61                         return -1;
62                 }
63                 return 0;
64         }
65
66         str = talloc_strdup(NULL, dir);
67         sep = strchr(*str == '/' ? str + 1 : str, '/');
68
69         rc = 0;
70
71         while (1) {
72
73                 /* terminate the path at sep */
74                 if (sep)
75                         *sep = '\0';
76
77                 if (mkdir(str, mode) && errno != EEXIST) {
78                         pb_log("mkdir(%s): %s\n", str, strerror(errno));
79                         rc = -1;
80                         break;
81                 }
82
83                 if (!sep)
84                         break;
85
86                 /* reset dir to the full path */
87                 strcpy(str, dir);
88                 sep = strchr(sep + 1, '/');
89         }
90
91         talloc_free(str);
92
93         return rc;
94 }
95
96 int pb_rmdir_recursive(const char *base, const char *dir)
97 {
98         char *cur, *pos;
99
100         /* sanity check: make sure that dir is within base */
101         if (strncmp(base, dir, strlen(base)))
102                 return -1;
103
104         cur = talloc_strdup(NULL, dir);
105
106         while (strcmp(base, cur)) {
107
108                 rmdir(cur);
109
110                 /* null-terminate at the last slash */
111                 pos = strrchr(cur, '/');
112                 if (!pos)
113                         break;
114
115                 *pos = '\0';
116         }
117
118         talloc_free(cur);
119
120         return 0;
121 }