1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup volsrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Filesystem creation
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include <loc.h>
|
---|
40 | #include <stdarg.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <task.h>
|
---|
45 | #include <types/vol.h>
|
---|
46 |
|
---|
47 | #include "mkfs.h"
|
---|
48 |
|
---|
49 | static errno_t cmd_runl(const char *path, ...)
|
---|
50 | {
|
---|
51 | va_list ap;
|
---|
52 | const char *arg;
|
---|
53 | int cnt = 0;
|
---|
54 |
|
---|
55 | va_start(ap, path);
|
---|
56 | do {
|
---|
57 | arg = va_arg(ap, const char *);
|
---|
58 | cnt++;
|
---|
59 | } while (arg != NULL);
|
---|
60 | va_end(ap);
|
---|
61 |
|
---|
62 | va_start(ap, path);
|
---|
63 | task_id_t id;
|
---|
64 | task_wait_t wait;
|
---|
65 | errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
|
---|
66 | va_end(ap);
|
---|
67 |
|
---|
68 | if (rc != EOK) {
|
---|
69 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error spawning %s (%s)",
|
---|
70 | path, str_error(rc));
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!id) {
|
---|
75 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error spawning %s "
|
---|
76 | "(invalid task ID)", path);
|
---|
77 | return EINVAL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | task_exit_t texit;
|
---|
81 | int retval;
|
---|
82 | rc = task_wait(&wait, &texit, &retval);
|
---|
83 | if (rc != EOK) {
|
---|
84 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error waiting for %s (%s)",
|
---|
85 | path, str_error(rc));
|
---|
86 | return rc;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (texit != TASK_EXIT_NORMAL) {
|
---|
90 | log_msg(LOG_DEFAULT, LVL_ERROR, "Command %s unexpectedly "
|
---|
91 | "terminated", path);
|
---|
92 | return EINVAL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (retval != 0) {
|
---|
96 | log_msg(LOG_DEFAULT, LVL_ERROR, "Command %s returned non-zero "
|
---|
97 | "exit code %d)", path, retval);
|
---|
98 | }
|
---|
99 |
|
---|
100 | return retval == 0 ? EOK : EPARTY;
|
---|
101 | }
|
---|
102 |
|
---|
103 | errno_t volsrv_part_mkfs(service_id_t sid, vol_fstype_t fstype, const char *label)
|
---|
104 | {
|
---|
105 | const char *cmd;
|
---|
106 | char *svc_name;
|
---|
107 | errno_t rc;
|
---|
108 |
|
---|
109 | cmd = NULL;
|
---|
110 | switch (fstype) {
|
---|
111 | case fs_exfat:
|
---|
112 | cmd = "/app/mkexfat";
|
---|
113 | break;
|
---|
114 | case fs_fat:
|
---|
115 | cmd = "/app/mkfat";
|
---|
116 | break;
|
---|
117 | case fs_minix:
|
---|
118 | cmd = "/app/mkmfs";
|
---|
119 | break;
|
---|
120 | case fs_ext4:
|
---|
121 | cmd = "/app/mkext4";
|
---|
122 | break;
|
---|
123 | case fs_cdfs:
|
---|
124 | cmd = NULL;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (cmd == NULL)
|
---|
129 | return ENOTSUP;
|
---|
130 |
|
---|
131 | rc = loc_service_get_name(sid, &svc_name);
|
---|
132 | if (rc != EOK)
|
---|
133 | return rc;
|
---|
134 |
|
---|
135 | if (str_size(label) > 0)
|
---|
136 | rc = cmd_runl(cmd, cmd, "--label", label, svc_name, NULL);
|
---|
137 | else
|
---|
138 | rc = cmd_runl(cmd, cmd, svc_name, NULL);
|
---|
139 |
|
---|
140 | free(svc_name);
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void volsrv_part_get_lsupp(vol_fstype_t fstype, vol_label_supp_t *vlsupp)
|
---|
145 | {
|
---|
146 | vlsupp->supported = false;
|
---|
147 |
|
---|
148 | switch (fstype) {
|
---|
149 | case fs_exfat:
|
---|
150 | case fs_ext4:
|
---|
151 | case fs_fat:
|
---|
152 | vlsupp->supported = true;
|
---|
153 | break;
|
---|
154 | case fs_minix:
|
---|
155 | case fs_cdfs:
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** @}
|
---|
161 | */
|
---|