summaryrefslogtreecommitdiffstats
path: root/firmware/common/rbpaths.c
blob: 69543bc3a7158953d5eeafd87e77368fccf75535 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Thomas Martitz
 *
 * 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 <stdio.h> /* snprintf */
#include <stdlib.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#include "rbpaths.h"
#include "crc32.h"
#include "file.h" /* MAX_PATH */
#include "logf.h"
#include "gcc_extensions.h"
#include "string-extra.h"
#include "filefuncs.h"

/* In this file we need the actual OS library functions, not the shadowed
 * wrapper used within Rockbox' application code (except SDL adds
 * another layer) */
#undef open
#undef creat
#undef remove
#undef rename
#undef opendir
#undef closedir
#undef readdir
#undef mkdir
#undef rmdir
#undef dirent
#undef DIR
#undef readlink

#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
static const char rbhome[] = "/sdcard";
#elif (CONFIG_PLATFORM & (PLATFORM_SDL|PLATFORM_MAEMO|PLATFORM_PANDORA)) && !defined(__PCTOOL__)
const char *rbhome;
#else
/* YPR0, YPR1 */
static const char rbhome[] = HOME_DIR;
#endif

#if !(defined(SAMSUNG_YPR0) || defined(SAMSUNG_YPR1)) && !defined(__PCTOOL__)
/* Special dirs are user-accessible (and user-writable) dirs which take priority
 * over the ones where Rockbox is installed to. Classic example would be
 * $HOME/.config/rockbox.org vs /usr/share/rockbox */
#define HAVE_SPECIAL_DIRS
#endif

/* flags for get_user_file_path() */
/* whether you need write access to that file/dir, especially true
 * for runtime generated files (config.cfg) */
#define NEED_WRITE          (1<<0)
/* file or directory? */
#define IS_FILE             (1<<1)

#ifdef HAVE_MULTIDRIVE
static uint32_t rbhome_hash;
/* A special link is created under e.g. HOME_DIR/<microSD1>, e.g. to access
 * external storage in a convinient location, much similar to the mount
 * point on our native targets. Here they are treated as symlink (one which
 * doesn't actually exist in the filesystem and therefore we have to override
 * readlink() */
static const char *handle_special_links(const char* link, unsigned flags,
                                char *buf, const size_t bufsize)
{
    (void) flags;
    char vol_string[VOL_ENUM_POS + 8];
    int len = sprintf(vol_string, VOL_NAMES, 1);

    /* link might be passed with or without HOME_DIR expanded. To handle
     * both perform substring matching (VOL_NAMES is unique enough) */
    const char *begin = strstr(link, vol_string);
    if (begin)
    {
        /* begin now points to the start of vol_string within link,
         * we want to copy the remainder of the paths, prefixed by
         * the actual mount point (the remainder might be "") */
        snprintf(buf, bufsize, MULTIDRIVE_DIR"%s", begin + len);
        return buf;
    }

    return link;
}
#endif

void paths_init(void)
{
#ifdef HAVE_SPECIAL_DIRS
    /* make sure $HOME/.config/rockbox.org exists, it's needed for config.cfg */
#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
    mkdir("/sdcard/rockbox", 0777);
    mkdir("/sdcard/rockbox/rocks.data", 0777);
#else
    char config_dir[MAX_PATH];

    const char *home = getenv("RBROOT");
    if (!home)
    {
        home = getenv("HOME");
    }
    if (!home)
    {
      logf("HOME environment var not set. Can't write config");
      return;
    }

    rbhome = home;
    snprintf(config_dir, sizeof(config_dir), "%s/.config", home);
    mkdir(config_dir, 0777);
    snprintf(config_dir, sizeof(config_dir), "%s/.config/rockbox.org", home);
    mkdir(config_dir, 0777);
    /* Plugin data directory */
    snprintf(config_dir, sizeof(config_dir), "%s/.config/rockbox.org/rocks.data", home);
    mkdir(config_dir, 0777);
#endif
#endif /* HAVE_SPECIAL_DIRS */

#ifdef HAVE_MULTIDRIVE
    rbhome_hash = crc_32((const void *) rbhome, strlen(rbhome), 0xffffffff);
#endif /* HAVE_MULTIDRIVE */
}

#ifdef HAVE_SPECIAL_DIRS
static bool try_path(const char* filename, unsigned flags)
{
    if (flags & IS_FILE)
    {
        if (file_exists(filename))
            return true;
    }
    else
    {
        if (dir_exists(filename))
            return true;
    }
    return false;
}

static const char* _get_user_file_path(const char *path,
                                       unsigned flags,
                                       char* buf,
                                       const size_t bufsize)
{
    const char *ret = path;
    const char *pos = path;
    /* replace ROCKBOX_DIR in path with $HOME/.config/rockbox.org */
    pos += ROCKBOX_DIR_LEN;
    if (*pos == '/') pos += 1;

#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
    if (snprintf(buf, bufsize, "/sdcard/rockbox/%s", pos)
#else
    if (snprintf(buf, bufsize, "%s/.config/rockbox.org/%s", rbhome, pos)
#endif
            >= (int)bufsize)
        return NULL;

    /* always return the replacement buffer (pointing to $HOME) if
     * write access is needed */
    if (flags & NEED_WRITE)
        ret = buf;
    else if (try_path(buf, flags))
        ret = buf;
        
    if (ret != buf) /* not found in $HOME, try ROCKBOX_BASE_DIR, !NEED_WRITE only */
    {
        if (snprintf(buf, bufsize, ROCKBOX_SHARE_PATH "/%s", pos) >= (int)bufsize)
            return NULL;

        if (try_path(buf, flags))
            ret = buf;
    }

    return ret;
}

#endif

static const char* handle_special_dirs(const char* dir, unsigned flags,
                                char *buf, const size_t bufsize)
{
    (void) flags; (void) buf; (void) bufsize;
#ifdef HAVE_SPECIAL_DIRS
    if (!strncmp(HOME_DIR, dir, HOME_DIR_LEN))
    {
        const char *p = dir + HOME_DIR_LEN;
        while (*p == '/') p++;
        snprintf(buf, bufsize, "%s/%s", rbhome, p);
        dir = buf;
    }
    else if (!strncmp(ROCKBOX_DIR, dir, ROCKBOX_DIR_LEN))
        dir = _get_user_file_path(dir, flags, buf, bufsize);
#endif
#ifdef HAVE_MULTIDRIVE
    dir = handle_special_links(dir, flags, buf, bufsize);
#endif
    return dir;
}

int app_open(const char *name, int o, ...)
{
    char realpath[MAX_PATH];
    va_list ap;
    int fd;
    int flags = IS_FILE;
    if (o & (O_CREAT|O_RDWR|O_TRUNC|O_WRONLY))
        flags |= NEED_WRITE;
    
    name = handle_special_dirs(name, flags, realpath, sizeof(realpath));

    va_start(ap, o);
    fd = open(name, o, va_arg(ap, unsigned int));
    va_end(ap);
    
    return fd;
}

int app_creat(const char* name, mode_t mode)
{
    return app_open(name, O_CREAT|O_WRONLY|O_TRUNC, mode);
}

int app_remove(const char *name)
{
    char realpath[MAX_PATH];
    const char *fname = handle_special_dirs(name, NEED_WRITE, realpath, sizeof(realpath));

    return remove(fname);
}

int app_rename(const char *old, const char *new)
{
    char realpath_old[MAX_PATH], realpath_new[MAX_PATH];
    const char *final_old, *final_new;

    final_old = handle_special_dirs(old, NEED_WRITE, realpath_old, sizeof(realpath_old));
    final_new = handle_special_dirs(new, NEED_WRITE, realpath_new, sizeof(realpath_new));

    return rename(final_old, final_new);
}

/* need to wrap around DIR* because we need to save the parent's
 * directory path in order to determine dirinfo, required to implement
 * get_dir_info() */
struct __dir {
    DIR *dir;
#ifdef HAVE_MULTIDRIVE
    int volumes_returned;
    /* A crc of rbhome is used to speed op the common case where
     * readdir()/get_dir_info() is called on non-rbhome paths, because
     * each call needs to check against rbhome for the virtual
     * mount point of the external storage */
    uint32_t path_hash;
#endif
    char path[];
};

struct dirinfo dir_get_info(DIR* _parent, struct dirent *dir)
{
    struct __dir *parent = (struct __dir*)_parent;
    struct stat s;
    struct tm *tm = NULL;
    struct dirinfo ret;
    char path[MAX_PATH];

    memset(&ret, 0, sizeof(ret));

#ifdef HAVE_MULTIDRIVE
    char vol_string[VOL_ENUM_POS + 8];
    sprintf(vol_string, VOL_NAMES, 1);
    if (UNLIKELY(rbhome_hash == parent->path_hash) &&
            /* compare path anyway because of possible hash collision */
            !strcmp(vol_string, dir->d_name))
    {
        ret.attribute = ATTR_LINK;
        strcpy(path, MULTIDRIVE_DIR);
    }
    else
#endif
        snprintf(path, sizeof(path), "%s/%s", parent->path, dir->d_name);


    if (!lstat(path, &s))
    {
        int err = 0;
        if (S_ISLNK(s.st_mode))
        {
            ret.attribute |= ATTR_LINK;
            err = stat(path, &s);
        }
        if (!err)
        {
            if (S_ISDIR(s.st_mode))
                ret.attribute |= ATTR_DIRECTORY;

            ret.size = s.st_size;
            if ((tm = localtime(&(s.st_mtime))))
            {
                ret.wrtdate = ((tm->tm_year - 80) << 9) |
                                  ((tm->tm_mon + 1) << 5) |
                                    tm->tm_mday;
                ret.wrttime = (tm->tm_hour << 11) |
                                  (tm->tm_min << 5) |
                                  (tm->tm_sec >> 1);
            }
        }
    }

    return ret;
}
   
DIR* app_opendir(const char *_name)
{
    size_t name_len;
    char realpath[MAX_PATH];
    const char *name = handle_special_dirs(_name, 0, realpath, sizeof(realpath));
    name_len = strlen(name);
    char *buf = malloc(sizeof(struct __dir) + name_len+1);
    if (!buf)
        return NULL;

    struct __dir *this = (struct __dir*)buf;
    /* carefully remove any trailing slash from the input, so that
     * hash/path matching in readdir() works properly */
    while (name[name_len-1] == '/' && name_len > 1)
        name_len -= 1;
    /* strcpy cannot be used because of trailing slashes */
    memcpy(this->path, name, name_len);
    this->path[name_len] = 0;
    this->dir = opendir(this->path);

    if (!this->dir)
    {
        free(buf);
        return NULL;
    }
#ifdef HAVE_MULTIDRIVE
    this->volumes_returned = 0;
    this->path_hash = crc_32((const void *)this->path, name_len, 0xffffffff);
#endif

    return (DIR*)this;
}

int app_closedir(DIR *dir)
{
    struct __dir *this = (struct __dir*)dir;
    int ret = closedir(this->dir);
    free(this);
    return ret;
}


struct dirent* app_readdir(DIR* dir)
{
    struct __dir *d = (struct __dir*)dir;
#ifdef HAVE_MULTIDRIVE
    /* this is not MT-safe but OK according to man readdir */
    static struct dirent voldir;
    if (UNLIKELY(rbhome_hash == d->path_hash)
            && d->volumes_returned < (NUM_VOLUMES-1)
            && volume_present(d->volumes_returned+1)
            /* compare path anyway because of possible hash collision */
            && !strcmp(d->path, rbhome))
    {
        d->volumes_returned += 1;
        sprintf(voldir.d_name, VOL_NAMES, d->volumes_returned);
        return &voldir;
    }
#endif
    return readdir(d->dir);
}


int app_mkdir(const char* name)
{
    char realpath[MAX_PATH];
    const char *fname = handle_special_dirs(name, NEED_WRITE, realpath, sizeof(realpath));
    return mkdir(fname, 0777);
}


int app_rmdir(const char* name)
{
    char realpath[MAX_PATH];
    const char *fname = handle_special_dirs(name, NEED_WRITE, realpath, sizeof(realpath));
    return rmdir(fname);
}


/* On MD we create a virtual symlink for the external drive,
 * for this we need to override readlink(). */
ssize_t app_readlink(const char *path, char *buf, size_t bufsiz)
{
    char _buf[MAX_PATH];
    (void) path; (void) buf; (void) bufsiz;
    path = handle_special_dirs(path, 0, _buf, sizeof(_buf));
#ifdef HAVE_MULTIDRIVE
    /* if path == _buf then we can be sure handle_special_dir() did something
     * and path is not an ordinary directory */
    if (path == _buf && !strncmp(path, MULTIDRIVE_DIR, sizeof(MULTIDRIVE_DIR)-1))
    {
        /* copying NUL is not required as per readlink specification */
        ssize_t len = strlen(path);
        memcpy(buf, path, len);
        return len;
    }
#endif
    /* does not append NUL !! */
    return readlink(path, buf, bufsiz);
}