summaryrefslogtreecommitdiffstats
path: root/firmware/common/rb_namespace.c
blob: ff5ad0f6db416c12afe248ba98c6bfeba05518d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2017 by Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "config.h"
#include <errno.h>
#include "fileobj_mgr.h"
#include "rb_namespace.h"
#include "file_internal.h"

/* Define LOGF_ENABLE to enable logf output in this file */
//#define LOGF_ENABLE
#include "logf.h"

#if !defined(HAVE_MULTIVOLUME) && defined(LOGF_ENABLE)
    int volume = 0;
#endif


#define ROOT_CONTENTS_INDEX  (NUM_VOLUMES)
#define NUM_ROOT_ITEMS   (NUM_VOLUMES+1)

static uint8_t root_entry_flags[NUM_VOLUMES+1];
static struct file_base_binding *root_bindp;

static inline unsigned int get_root_item_state(int item)
{
    return root_entry_flags[item];
}

static inline void set_root_item_state(int item, unsigned int state)
{
    root_entry_flags[item] = state;
}

static void get_mount_point_entry(IF_MV(int volume,) struct DIRENT *entry)
{
#ifdef HAVE_MULTIVOLUME
    get_volume_name(volume, entry->d_name);
#else /* */
    strcpy(entry->d_name, PATH_ROOTSTR);
#endif /* HAVE_MULTIVOLUME */
#if defined(_FILESYSTEM_NATIVE_H_)
    entry->info.attr    = ATTR_MOUNT_POINT;
    entry->info.size    = 0;
    entry->info.wrtdate = 0;
    entry->info.wrttime = 0;
#endif /* is dirinfo_native */
    logf("%s: vol:%d, %s", __func__, volume, entry->d_name);
}

/* unmount the directory that enumerates into the root namespace */
static void unmount_item(int item)
{
    unsigned int state = get_root_item_state(item);
    logf("%s: state: %u", __func__, state);
    if (!state)
        return;

    if (state & NSITEM_CONTENTS)
    {
        fileobj_unmount(root_bindp);
        root_bindp = NULL;
    }

    set_root_item_state(item, 0);
}

/* mount the directory that enumerates into the root namespace */
int root_mount_path(const char *path, unsigned int flags)
{
#ifdef HAVE_MULTIVOLUME
    int volume = path_strip_volume(path, NULL, false);
    if (volume == ROOT_VOLUME)
        return -EINVAL;

    if (!CHECK_VOL(volume))
        return -ENOENT;
#else
    if (!path_is_absolute(path))
    {
        logf("Path not absolute %s", path);
        return -ENOENT;
    }
#endif /* HAVE_MULTIVOLUME */

    bool contents = flags & NSITEM_CONTENTS;
    int item = contents ? ROOT_CONTENTS_INDEX : IF_MV_VOL(volume);
    unsigned int state = get_root_item_state(item);

    logf("%s: item:%d, st:%u, %s", __func__, item, state, path);

    if (state)
        return -EBUSY;

    if (contents)
    {
        /* cache information about the target */
        struct filestr_base stream;
        struct path_component_info compinfo;

        int e = errno;
        int rc = open_stream_internal(path, FF_DIR | FF_PROBE | FF_INFO |
                                      FF_DEVPATH, &stream, &compinfo);
        if (rc <= 0)
        {
            rc = rc ? -errno : -ENOENT;
            errno = e;
            return rc;
        }

        if (!fileobj_mount(&compinfo.info, FO_DIRECTORY, &root_bindp))
            return -EBUSY;
    }

    state = NSITEM_MOUNTED | (flags & (NSITEM_HIDDEN|NSITEM_CONTENTS));
    set_root_item_state(item, state);

    return 0;
}

/* inform root that an entire volume is being unmounted */
void root_unmount_volume(IF_MV_NONVOID(int volume))
{
    logf("%s: vol: %d", __func__, volume);
    FOR_EACH_VOLUME(volume, item)
    {
    #ifdef HAVE_MULTIVOLUME
        uint32_t state = get_root_item_state(item);
        if (state && (volume < 0 || item == volume))
    #endif /* HAVE_MULTIVOLUME */
            unmount_item(item);
    }

    /* if the volume unmounted contains the root directory contents then
       the contents must also be unmounted */
#ifdef HAVE_MULTIVOLUME
    uint32_t state = get_root_item_state(ROOT_CONTENTS_INDEX);
    if (state && (volume < 0 || BASEINFO_VOL(&root_bindp->info) == volume))
#endif
        unmount_item(ROOT_CONTENTS_INDEX);
}

/* parse the root part of a path */
int ns_parse_root(const char *path, const char **pathp, uint16_t *lenp)
{
    logf("%s: path: %s", __func__, path);
    int volume = ROOT_VOLUME;

#ifdef HAVE_MULTIVOLUME
    /* this seamlessly integrates secondary filesystems into the
       root namespace (e.g. "/<0>/../../<1>/../foo/." :<=> "/foo") */
    const char *p;
    volume = path_strip_volume(path, &p, false);
    if (volume != ROOT_VOLUME && !CHECK_VOL(volume))
    {
        logf("vol: %d is not root", volume);
        return -ENOENT;
    }
#endif /* HAVE_MULTIVOLUME */

    /* set name to start at last leading separator; name of root will
     * be returned as "/", volume specifiers as "/<fooN>" */
    *pathp = GOBBLE_PATH_SEPCH(path) - 1;
    *lenp  = IF_MV( volume < NUM_VOLUMES ? p - *pathp : ) 1;
#ifdef LOGF_ENABLE
    if (volume == INT_MAX)
        logf("vol: ROOT(%d) %s", volume, *pathp);
    else
        logf("vol: %d %s", volume, *pathp);
#endif
#ifdef HAVE_MULTIVOLUME
    if (*lenp > MAX_COMPNAME+1)
    {
        logf("%s: path too long %s", __func__, path);
        return -ENAMETOOLONG;
    }
#endif
#ifdef LOGF_ENABLE
    if (volume == INT_MAX)
        logf("%s: vol: ROOT(%d) path: %s", __func__, volume, path);
    else
        logf("%s: vol: %d path: %s", __func__, volume, path);
#endif
    return volume;
}

/* open one of the items in the root */
int ns_open_root(IF_MV(int volume,) unsigned int *callflagsp,
                 struct file_base_info *infop, uint16_t *attrp)
{
    unsigned int callflags = *callflagsp;
    bool devpath = !!(callflags & FF_DEVPATH);
#ifdef HAVE_MULTIVOLUME
    bool sysroot = volume == ROOT_VOLUME;
    if (devpath && sysroot)
        return -ENOENT;         /* devpath needs volume spec */
#else
    bool sysroot = !devpath;    /* always sysroot unless devpath */
#endif

    int item = sysroot ? ROOT_CONTENTS_INDEX : IF_MV_VOL(volume);
    unsigned int state = get_root_item_state(item);
    logf("%s: Vol:%d St:%d", __func__, item, state);
    if (sysroot)
    {
        *attrp = ATTR_SYSTEM_ROOT;

        if (state)
            *infop = root_bindp->info;
        else
        {
            logf("%s: SysRoot Vol:%d St:%d NOT mounted", __func__, item, state);
            *callflagsp = callflags | FF_NOFS;  /* contents not mounted */
        }
    }
    else
    {
        *attrp = ATTR_MOUNT_POINT;

        if (!devpath && !state)
            return -ENOENT; /* regular open requires having been mounted */
#if CONFIG_PLATFORM & PLATFORM_NATIVE
        if (fat_open_rootdir(IF_MV(volume,) &infop->fatfile) < 0)
        {
            logf("%s: DevPath Vol:%d St:%d NOT mounted", __func__, item, state);
            return -ENOENT; /* not mounted */
        }
#endif
        get_rootinfo_internal(infop);
    }

    return 0;
}

/* read root directory entries */
int root_readdir_dirent(struct filestr_base *stream,
                        struct ns_scan_info *scanp, struct DIRENT *entry)
{
    int rc = 0;

    int item = scanp->item;
    logf("%s: item: %d", __func__, item);

    /* skip any not-mounted or hidden items */
    unsigned int state;
    while (1)
    {
        if (item >= NUM_ROOT_ITEMS)
            goto file_eod;

        state = get_root_item_state(item);
        if ((state & (NSITEM_MOUNTED|NSITEM_HIDDEN)) == NSITEM_MOUNTED)
        {
            logf("Found mounted item: %d %s", item, entry->d_name);
            break;
        }

        item++;
    }

    if (item == ROOT_CONTENTS_INDEX)
    {
        rc = readdir_dirent(stream, &scanp->scan, entry);
        if (rc < 0)
            FILE_ERROR(ERRNO, rc * 10 - 1);

        if (rc == 0)
        {
            logf("Found root item: %d %s", item, entry->d_name);
            item++;
        }
    }
    else
    {
        get_mount_point_entry(IF_MV(item,) entry);
        item++;
        rc = 1;
        logf("Found mp item:%d %s", item,  entry->d_name);
    }

    scanp->item = item;

file_eod:
#ifdef HAVE_DIRCACHE
    if (rc == 0)
        empty_dirent(entry);
#endif
file_error:
    logf("%s: status: %d", __func__, rc);
    return rc;
}

/* opens a stream to enumerate items in a namespace container */
int ns_open_stream(const char *path, unsigned int callflags,
                   struct filestr_base *stream, struct ns_scan_info *scanp)
{
    logf("%s: path: %s", __func__, path);
    /* stream still needs synchronization even if we don't have a stream */
    static struct mutex no_contents_mtx SHAREDBSS_ATTR;

    int rc = open_stream_internal(path, callflags, stream, NULL);
    if (rc < 0)
        FILE_ERROR(ERRNO, rc * 10 - 1);

    scanp->item = rc > 1 ? 0 : -1;

    if (stream->flags & FDO_BUSY)
    {
        /* root contents are mounted */
        fat_rewind(&stream->fatstr);
    }
    else
    {
        /* root contents not mounted */
        mutex_init(&no_contents_mtx);
        stream->mtx = &no_contents_mtx;
    }

    ns_dirscan_rewind(scanp);

    rc = 0;
file_error:
    return rc;
}