source: mainline/uspace/lib/c/generic/vfs/mtab.c@ bd76871

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd76871 was a53ed3a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Change (rc != 0) to (rc != EOK), where appropriate.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
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 libc
30 * @{
31 */
32/** @file
33 */
34
35#include <vfs/vfs.h>
36#include <vfs/vfs_mtab.h>
37#include <macros.h>
38#include <stdlib.h>
39#include <dirent.h>
40#include <errno.h>
41#include <assert.h>
42
43static void process_mp(const char *path, struct stat *stat, list_t *mtab_list)
44{
45 mtab_ent_t *ent;
46
47 ent = (mtab_ent_t *) malloc(sizeof(mtab_ent_t));
48 if (!ent)
49 return;
50
51 link_initialize(&ent->link);
52 str_cpy(ent->mp, sizeof(ent->mp), path);
53 ent->service_id = stat->service_id;
54
55 struct statfs stfs;
56 if (vfs_statfs_path(path, &stfs) == EOK)
57 str_cpy(ent->fs_name, sizeof(ent->fs_name), stfs.fs_name);
58 else
59 str_cpy(ent->fs_name, sizeof(ent->fs_name), "?");
60
61 list_append(&ent->link, mtab_list);
62}
63
64static errno_t vfs_get_mtab_visit(const char *path, list_t *mtab_list,
65 fs_handle_t fs_handle, service_id_t service_id)
66{
67 DIR *dir;
68 struct dirent *dirent;
69
70 dir = opendir(path);
71 if (!dir)
72 return ENOENT;
73
74 while ((dirent = readdir(dir)) != NULL) {
75 char *child;
76 struct stat st;
77 errno_t rc;
78 int ret;
79
80 ret = asprintf(&child, "%s/%s", path, dirent->d_name);
81 if (ret < 0) {
82 closedir(dir);
83 return ENOMEM;
84 }
85
86 char *pa = vfs_absolutize(child, NULL);
87 if (!pa) {
88 free(child);
89 closedir(dir);
90 return ENOMEM;
91 }
92
93 free(child);
94 child = pa;
95
96 rc = vfs_stat_path(child, &st);
97 if (rc != EOK) {
98 free(child);
99 closedir(dir);
100 return rc;
101 }
102
103 if (st.fs_handle != fs_handle || st.service_id != service_id) {
104 /*
105 * We have discovered a mountpoint.
106 */
107 process_mp(child, &st, mtab_list);
108 }
109
110 if (st.is_directory) {
111 (void) vfs_get_mtab_visit(child, mtab_list,
112 st.fs_handle, st.service_id);
113 }
114
115 free(child);
116 }
117
118 closedir(dir);
119 return EOK;
120}
121
122errno_t vfs_get_mtab_list(list_t *mtab_list)
123{
124 struct stat st;
125
126 errno_t rc = vfs_stat_path("/", &st);
127 if (rc != EOK)
128 return rc;
129
130 process_mp("/", &st, mtab_list);
131
132 return vfs_get_mtab_visit("/", mtab_list, st.fs_handle, st.service_id);
133}
134
135/** @}
136 */
Note: See TracBrowser for help on using the repository browser.