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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8610c2c was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • 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 int 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 int rc;
78
79 rc = asprintf(&child, "%s/%s", path, dirent->d_name);
80 if (rc < 0) {
81 closedir(dir);
82 return rc;
83 }
84
85 char *pa = vfs_absolutize(child, NULL);
86 if (!pa) {
87 free(child);
88 closedir(dir);
89 return ENOMEM;
90 }
91
92 free(child);
93 child = pa;
94
95 rc = vfs_stat_path(child, &st);
96 if (rc != 0) {
97 free(child);
98 closedir(dir);
99 return rc;
100 }
101
102 if (st.fs_handle != fs_handle || st.service_id != service_id) {
103 /*
104 * We have discovered a mountpoint.
105 */
106 process_mp(child, &st, mtab_list);
107 }
108
109 if (st.is_directory) {
110 (void) vfs_get_mtab_visit(child, mtab_list,
111 st.fs_handle, st.service_id);
112 }
113
114 free(child);
115 }
116
117 closedir(dir);
118 return EOK;
119}
120
121int vfs_get_mtab_list(list_t *mtab_list)
122{
123 struct stat st;
124
125 int rc = vfs_stat_path("/", &st);
126 if (rc != 0)
127 return rc;
128
129 process_mp("/", &st, mtab_list);
130
131 return vfs_get_mtab_visit("/", mtab_list, st.fs_handle, st.service_id);
132}
133
134/** @}
135 */
Note: See TracBrowser for help on using the repository browser.