summaryrefslogtreecommitdiffstats
path: root/firmware/common/rbpaths.c
blob: c82e0f2dce7e35fad84ee462759b97ed65618a58 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 "rbpaths.h"
#include "file.h" /* MAX_PATH */
#include "gcc_extensions.h"
#include "string-extra.h"
#include "filefuncs.h"

#undef open
#undef creat
#undef remove
#undef rename
#undef opendir
#undef mkdir
#undef rmdir

#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
#include "dir-target.h"
#define opendir opendir_android
#define mkdir   mkdir_android
#define rmdir   rmdir_android
#elif (CONFIG_PLATFORM & PLATFORM_SDL)
#define open    sim_open
#define remove  sim_remove
#define rename  sim_rename
#define opendir sim_opendir
#define mkdir   sim_mkdir
#define rmdir   sim_rmdir
extern int sim_open(const char* name, int o, ...);
extern int sim_remove(const char* name);
extern int sim_rename(const char* old, const char* new);
extern DIR* sim_opendir(const char* name);
extern int sim_mkdir(const char* name);
extern int sim_rmdir(const char* name);
#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)

void paths_init(void)
{
    /* make sure $HOME/.config/rockbox.org exists, it's needed for config.cfg */
#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
    mkdir("/sdcard/rockbox");
#else
    char home_path[MAX_PATH];
    snprintf(home_path, sizeof(home_path), "%s/.config/rockbox.org", getenv("HOME"));
    mkdir(home_path);
#endif
}

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", getenv("HOME"), 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;
}

int app_open(const char *name, int o, ...)
{
    char realpath[MAX_PATH];
    va_list ap;
    int fd;
    
    if (!strncmp(ROCKBOX_DIR, name, ROCKBOX_DIR_LEN))
    {
        int flags = IS_FILE;
        if (o & (O_CREAT|O_RDWR|O_TRUNC|O_WRONLY))
            flags |= NEED_WRITE;
        name = _get_user_file_path(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 = name;
    if (!strncmp(ROCKBOX_DIR, name, ROCKBOX_DIR_LEN))
    {
        fname = _get_user_file_path(name, 0, realpath, sizeof(realpath));
    }
    return remove(fname);
}

int app_rename(const char *old, const char *new)
{
    char realpath[MAX_PATH];
    const char *fname = old;
    if (!strncmp(ROCKBOX_DIR, old, ROCKBOX_DIR_LEN))
    {
        fname = _get_user_file_path(old, 0, realpath, sizeof(realpath));
    }
    return rename(fname, new);
}

DIR *app_opendir(const char *name)
{
    char realpath[MAX_PATH];
    const char *fname = name;
    if (!strncmp(ROCKBOX_DIR, name, ROCKBOX_DIR_LEN))
    {
        fname = _get_user_file_path(name, 0, realpath, sizeof(realpath));
    }
    return opendir(fname);
}

int app_mkdir(const char* name)
{
    char realpath[MAX_PATH];
    const char *fname = name;
    if (!strncmp(ROCKBOX_DIR, name, ROCKBOX_DIR_LEN))
    {
        fname = _get_user_file_path(name, 0, realpath, sizeof(realpath));
    }
    return mkdir(fname);
}

int app_rmdir(const char* name)
{
    char realpath[MAX_PATH];
    const char *fname = name;
    if (!strncmp(ROCKBOX_DIR, name, ROCKBOX_DIR_LEN))
    {
        fname = _get_user_file_path(name, 0, realpath, sizeof(realpath));
    }
    return rmdir(fname);
}