[438f355] | 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>
|
---|
[1d6dd2a] | 42 | #include <str.h>
|
---|
[438f355] | 43 |
|
---|
| 44 | static void process_mp(const char *path, struct stat *stat, list_t *mtab_list)
|
---|
| 45 | {
|
---|
| 46 | mtab_ent_t *ent;
|
---|
| 47 |
|
---|
| 48 | ent = (mtab_ent_t *) malloc(sizeof(mtab_ent_t));
|
---|
| 49 | if (!ent)
|
---|
| 50 | return;
|
---|
| 51 |
|
---|
| 52 | link_initialize(&ent->link);
|
---|
| 53 | str_cpy(ent->mp, sizeof(ent->mp), path);
|
---|
| 54 | ent->service_id = stat->service_id;
|
---|
| 55 |
|
---|
| 56 | struct statfs stfs;
|
---|
| 57 | if (vfs_statfs_path(path, &stfs) == EOK)
|
---|
| 58 | str_cpy(ent->fs_name, sizeof(ent->fs_name), stfs.fs_name);
|
---|
| 59 | else
|
---|
| 60 | str_cpy(ent->fs_name, sizeof(ent->fs_name), "?");
|
---|
| 61 |
|
---|
| 62 | list_append(&ent->link, mtab_list);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[b7fd2a0] | 65 | static errno_t vfs_get_mtab_visit(const char *path, list_t *mtab_list,
|
---|
[438f355] | 66 | fs_handle_t fs_handle, service_id_t service_id)
|
---|
| 67 | {
|
---|
| 68 | DIR *dir;
|
---|
| 69 | struct dirent *dirent;
|
---|
| 70 |
|
---|
| 71 | dir = opendir(path);
|
---|
| 72 | if (!dir)
|
---|
| 73 | return ENOENT;
|
---|
| 74 |
|
---|
| 75 | while ((dirent = readdir(dir)) != NULL) {
|
---|
| 76 | char *child;
|
---|
| 77 | struct stat st;
|
---|
[b7fd2a0] | 78 | errno_t rc;
|
---|
[d5c1051] | 79 | int ret;
|
---|
[438f355] | 80 |
|
---|
[d5c1051] | 81 | ret = asprintf(&child, "%s/%s", path, dirent->d_name);
|
---|
| 82 | if (ret < 0) {
|
---|
[438f355] | 83 | closedir(dir);
|
---|
[d5c1051] | 84 | return ENOMEM;
|
---|
[438f355] | 85 | }
|
---|
| 86 |
|
---|
| 87 | char *pa = vfs_absolutize(child, NULL);
|
---|
| 88 | if (!pa) {
|
---|
| 89 | free(child);
|
---|
| 90 | closedir(dir);
|
---|
| 91 | return ENOMEM;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | free(child);
|
---|
| 95 | child = pa;
|
---|
| 96 |
|
---|
| 97 | rc = vfs_stat_path(child, &st);
|
---|
[a53ed3a] | 98 | if (rc != EOK) {
|
---|
[438f355] | 99 | free(child);
|
---|
| 100 | closedir(dir);
|
---|
| 101 | return rc;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | if (st.fs_handle != fs_handle || st.service_id != service_id) {
|
---|
| 105 | /*
|
---|
| 106 | * We have discovered a mountpoint.
|
---|
| 107 | */
|
---|
| 108 | process_mp(child, &st, mtab_list);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (st.is_directory) {
|
---|
| 112 | (void) vfs_get_mtab_visit(child, mtab_list,
|
---|
| 113 | st.fs_handle, st.service_id);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | free(child);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | closedir(dir);
|
---|
| 120 | return EOK;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[b7fd2a0] | 123 | errno_t vfs_get_mtab_list(list_t *mtab_list)
|
---|
[438f355] | 124 | {
|
---|
| 125 | struct stat st;
|
---|
| 126 |
|
---|
[b7fd2a0] | 127 | errno_t rc = vfs_stat_path("/", &st);
|
---|
[a53ed3a] | 128 | if (rc != EOK)
|
---|
[438f355] | 129 | return rc;
|
---|
| 130 |
|
---|
| 131 | process_mp("/", &st, mtab_list);
|
---|
| 132 |
|
---|
| 133 | return vfs_get_mtab_visit("/", mtab_list, st.fs_handle, st.service_id);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /** @}
|
---|
| 137 | */
|
---|