]> git.ozlabs.org Git - petitboot/blob - lib/system/system.c
discover/syslinux-parser: Fix missing comma in ignored names.
[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         .vgscan         = HOST_PROG_VGSCAN,
32         .vgchange       = HOST_PROG_VGCHANGE,
33         .pb_plugin      = HOST_PROG_PB_PLUGIN,
34         .pb_exec        = HOST_PROG_PB_EXEC,
35         .sh             = HOST_PROG_SH,
36 };
37
38 #ifndef TFTP_TYPE
39 #define TFTP_TYPE TFTP_TYPE_UNKNOWN
40 #endif
41
42 enum tftp_type tftp_type = TFTP_TYPE;
43
44 int pb_mkdir_recursive(const char *dir)
45 {
46         struct stat statbuf;
47         int rc, mode = 0755;
48         char *str, *sep;
49
50         if (!*dir)
51                 return 0;
52
53         if (!stat(dir, &statbuf)) {
54                 if (!S_ISDIR(statbuf.st_mode)) {
55                         pb_log("%s: %s exists, but isn't a directory\n",
56                                         __func__, dir);
57                         return -1;
58                 }
59                 return 0;
60         }
61
62         str = talloc_strdup(NULL, dir);
63         sep = strchr(*str == '/' ? str + 1 : str, '/');
64
65         rc = 0;
66
67         while (1) {
68
69                 /* terminate the path at sep */
70                 if (sep)
71                         *sep = '\0';
72
73                 if (mkdir(str, mode) && errno != EEXIST) {
74                         pb_log("mkdir(%s): %s\n", str, strerror(errno));
75                         rc = -1;
76                         break;
77                 }
78
79                 if (!sep)
80                         break;
81
82                 /* reset dir to the full path */
83                 strcpy(str, dir);
84                 sep = strchr(sep + 1, '/');
85         }
86
87         talloc_free(str);
88
89         return rc;
90 }
91
92 int pb_rmdir_recursive(const char *base, const char *dir)
93 {
94         char *cur, *pos;
95
96         /* sanity check: make sure that dir is within base */
97         if (strncmp(base, dir, strlen(base)))
98                 return -1;
99
100         cur = talloc_strdup(NULL, dir);
101
102         while (strcmp(base, cur)) {
103
104                 rmdir(cur);
105
106                 /* null-terminate at the last slash */
107                 pos = strrchr(cur, '/');
108                 if (!pos)
109                         break;
110
111                 *pos = '\0';
112         }
113
114         talloc_free(cur);
115
116         return 0;
117 }