source: mainline/uspace/app/vol/vol.c@ 73401643

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 73401643 was 1a9174e, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Volume server needs to react to partitions being removed.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 * Copyright (c) 2017 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 vol
30 * @{
31 */
32/** @file Volume administration (interface to volsrv).
33 */
34
35#include <errno.h>
36#include <io/table.h>
37#include <loc.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <str.h>
41#include <vfs/vfs.h>
42#include <vol.h>
43
44#define NAME "vol"
45
46static char *volspec;
47
48typedef enum {
49 vcmd_eject,
50 vcmd_help,
51 vcmd_list,
52} vol_cmd_t;
53
54/** Find volume by current mount point. */
55static errno_t vol_cmd_part_by_mp(vol_t *vol, const char *mp,
56 service_id_t *rid)
57{
58 vol_part_info_t vinfo;
59 service_id_t *part_ids = NULL;
60 char *canon_mp_buf = NULL;
61 char *canon_mp;
62 size_t nparts;
63 size_t i;
64 errno_t rc;
65
66 canon_mp_buf = str_dup(mp);
67 if (canon_mp_buf == NULL) {
68 printf("Out of memory.\n");
69 rc = ENOMEM;
70 goto out;
71 }
72
73 canon_mp = vfs_absolutize(canon_mp_buf, NULL);
74 if (canon_mp == NULL) {
75 printf("Invalid volume path '%s'.\n", mp);
76 rc = EINVAL;
77 goto out;
78 }
79
80 rc = vol_get_parts(vol, &part_ids, &nparts);
81 if (rc != EOK) {
82 printf("Error getting list of volumes.\n");
83 goto out;
84 }
85
86 for (i = 0; i < nparts; i++) {
87 rc = vol_part_info(vol, part_ids[i], &vinfo);
88 if (rc != EOK) {
89 printf("Error getting volume information.\n");
90 rc = EIO;
91 goto out;
92 }
93
94 if (str_cmp(vinfo.cur_mp, canon_mp) == 0) {
95 *rid = part_ids[i];
96 rc = EOK;
97 goto out;
98 }
99 }
100
101 rc = ENOENT;
102out:
103 free(part_ids);
104 free(canon_mp_buf);
105 return rc;
106}
107
108static errno_t vol_cmd_eject(const char *volspec)
109{
110 vol_t *vol = NULL;
111 service_id_t part_id;
112 errno_t rc;
113
114 rc = vol_create(&vol);
115 if (rc != EOK) {
116 printf("Error contacting volume service.\n");
117 goto out;
118 }
119
120 rc = vol_cmd_part_by_mp(vol, volspec, &part_id);
121 if (rc != EOK) {
122 printf("Error looking up volume '%s'.\n", volspec);
123 goto out;
124 }
125
126 rc = vol_part_eject(vol, part_id);
127 if (rc != EOK) {
128 printf("Error ejecting volume.\n");
129 goto out;
130 }
131
132 rc = EOK;
133out:
134 vol_destroy(vol);
135 return rc;
136}
137
138static errno_t vol_cmd_list(void)
139{
140 vol_t *vol = NULL;
141 vol_part_info_t vinfo;
142 service_id_t *part_ids = NULL;
143 char *svc_name;
144 char *sfstype;
145 size_t nparts;
146 size_t i;
147 table_t *table = NULL;
148 errno_t rc;
149
150 rc = vol_create(&vol);
151 if (rc != EOK) {
152 printf("Error contacting volume service.\n");
153 goto out;
154 }
155
156 rc = vol_get_parts(vol, &part_ids, &nparts);
157 if (rc != EOK) {
158 printf("Error getting list of volumes.\n");
159 goto out;
160 }
161
162 rc = table_create(&table);
163 if (rc != EOK) {
164 printf("Out of memory.\n");
165 goto out;
166 }
167
168 table_header_row(table);
169 table_printf(table, "Volume Name\t" "Resource\t" "Content\t" "Auto\t"
170 "Mounted at\n");
171
172 for (i = 0; i < nparts; i++) {
173 rc = vol_part_info(vol, part_ids[i], &vinfo);
174 if (rc != EOK) {
175 printf("Error getting volume information.\n");
176 return EIO;
177 }
178
179 rc = loc_service_get_name(part_ids[i], &svc_name);
180 if (rc != EOK) {
181 printf("Error getting service name.\n");
182 return EIO;
183 }
184
185 rc = vol_pcnt_fs_format(vinfo.pcnt, vinfo.fstype, &sfstype);
186 if (rc != EOK) {
187 printf("Out of memory.\n");
188 free(svc_name);
189 return ENOMEM;
190 }
191
192 table_printf(table, "%s\t" "%s\t" "%s\t" "%s\t" "%s\n",
193 vinfo.label, svc_name, sfstype,
194 vinfo.cur_mp_auto ? "Yes" : "", vinfo.cur_mp);
195
196 free(svc_name);
197 free(sfstype);
198 svc_name = NULL;
199 }
200
201 rc = table_print_out(table, stdout);
202 if (rc != EOK)
203 printf("Error printing table.\n");
204out:
205 table_destroy(table);
206 vol_destroy(vol);
207 free(part_ids);
208
209 return rc;
210}
211
212static void print_syntax(void)
213{
214 printf("Syntax:\n");
215 printf(" %s List volumes\n", NAME);
216 printf(" %s -h Print help\n", NAME);
217 printf(" %s eject <volume> Eject volume\n", NAME);
218}
219
220int main(int argc, char *argv[])
221{
222 char *cmd;
223 vol_cmd_t vcmd;
224 int i;
225 errno_t rc;
226
227 if (argc < 2) {
228 vcmd = vcmd_list;
229 i = 1;
230 } else {
231 cmd = argv[1];
232 i = 2;
233 if (str_cmp(cmd, "-h") == 0) {
234 vcmd = vcmd_help;
235 } else if (str_cmp(cmd, "eject") == 0) {
236 vcmd = vcmd_eject;
237 if (argc <= i) {
238 printf("Parameter missing.\n");
239 goto syntax_error;
240 }
241 volspec = argv[i++];
242 } else {
243 printf("Invalid sub-command '%s'.\n", cmd);
244 goto syntax_error;
245 }
246 }
247
248 if (argc > i) {
249 printf("Unexpected argument '%s'.\n", argv[i]);
250 goto syntax_error;
251 }
252
253 switch (vcmd) {
254 case vcmd_eject:
255 rc = vol_cmd_eject(volspec);
256 break;
257 case vcmd_help:
258 print_syntax();
259 rc = EOK;
260 break;
261 case vcmd_list:
262 rc = vol_cmd_list();
263 break;
264 }
265
266 if (rc != EOK)
267 return 1;
268
269 return 0;
270
271syntax_error:
272 printf("Use %s -h to get help.\n", NAME);
273 return 1;
274}
275
276/** @}
277 */
Note: See TracBrowser for help on using the repository browser.