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