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