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