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