summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/filesystem-unix.c
blob: 3a14480e11c3648120fc8152c6ec943302afd9e2 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 by 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.
 *
 ****************************************************************************/
#define RB_FILESYSTEM_OS
#include <sys/statfs.h> /* lowest common denominator */
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <utime.h>
#include "config.h"
#include "system.h"
#include "file.h"
#include "dir.h"
#include "mv.h"
#include "debug.h"
#include "pathfuncs.h"
#include "string-extra.h"

int os_volume_path(IF_MV(int volume, ) char *buffer, size_t bufsize);

#define SAME_FILE_INFO(sb1p, sb2p) \
    ((sb1p)->st_dev == (sb2p)->st_dev && (sb1p)->st_ino == (sb2p)->st_ino)

int os_modtime(const char *path, time_t modtime)
{
    struct utimbuf times =
    {
        .actime = modtime,
        .modtime = modtime,
    };

    return utime(path, &times);
}

off_t os_filesize(int osfd)
{
    struct stat sb;

    if (!os_fstat(osfd, &sb))
        return sb.st_size;
    else
        return -1;
}

int os_fsamefile(int osfd1, int osfd2)
{
    struct stat sb1, sb2;

    if (os_fstat(osfd1, &sb1))
        return -1;

    if (os_fstat(osfd2, &sb2))
        return -1;

    return SAME_FILE_INFO(&sb1, &sb2);
}

int os_relate(const char *ospath1, const char *ospath2)
{
    DEBUGF("\"%s\" : \"%s\"\n", ospath1, ospath2);

    if (!ospath2 || !*ospath2)
    {
        errno = ospath2 ? ENOENT : EFAULT;
        return -1;
    }

    /* First file must stay open for duration so that its stats don't change */
    int fd1 = os_open(ospath1, O_RDONLY | O_CLOEXEC);
    if (fd1 < 0)
        return -2;

    struct stat sb1;
    if (os_fstat(fd1, &sb1))
    {
        os_close(fd1);
        return -3;
    }

    char path2buf[strlen(ospath2) + 1];
    *path2buf = 0;

    ssize_t len = 0;
    const char *p = ospath2;
    const char *sepmo = path_is_absolute(ospath2) ? PA_SEP_HARD : PA_SEP_SOFT;

    int rc = RELATE_DIFFERENT;

    while (1)
    {
        if (sepmo != PA_SEP_HARD &&
            !(len = parse_path_component(&ospath2, &p)))
        {
            break;
        }

        char compname[len + 1];
        strmemcpy(compname, p, len);

        path_append(path2buf, sepmo, compname, sizeof (path2buf));
        sepmo = PA_SEP_SOFT;

        int errnum = errno; /* save and restore if not actually failing */
        struct stat sb2;

        if (!os_stat(path2buf, &sb2))
        {
            if (SAME_FILE_INFO(&sb1, &sb2))
            {
                rc = RELATE_SAME;
            }
            else if (rc == RELATE_SAME)
            {
                if (name_is_dot_dot(compname))
                    rc = RELATE_DIFFERENT;
                else if (!name_is_dot(compname))
                    rc = RELATE_PREFIX;
            }
        }
        else if (errno == ENOENT && !*GOBBLE_PATH_SEPCH(ospath2) &&
                 !name_is_dot_dot(compname))
        {
            if (rc == RELATE_SAME)
                rc = RELATE_PREFIX;

            errno = errnum;
            break;
        }
        else
        {
            rc = -4;
            break;
        }
    }

    if (os_close(fd1) && rc >= 0)
        rc = -5;

    return rc;
}

bool os_file_exists(const char *ospath)
{
    int sim_fd = os_open(ospath, O_RDONLY | O_CLOEXEC, 0);
    if (sim_fd < 0)
        return false;

    int errnum = errno;
    os_close(sim_fd);
    errno = errnum;

    return true;
}

int os_opendirfd(const char *osdirname)
{
    return os_open(osdirname, O_RDONLY | O_CLOEXEC);
}

int os_opendir_and_fd(const char *osdirname, DIR **osdirpp, int *osfdp)
{
    /* another possible way is to use open() then fdopendir() */
    *osdirpp = NULL;
    *osfdp = -1;

    DIR *dirp = os_opendir(osdirname);
    if (!dirp)
        return -1;

    int rc = 0;
    int errnum = errno;

    int fd = os_dirfd(dirp);
    if (fd < 0)
    {
        fd = os_opendirfd(osdirname);
        rc = 1;
    }

    if (fd < 0)
    {
        os_closedir(dirp);
        return -2;
    }

    errno = errnum;

    *osdirpp = dirp;
    *osfdp = fd;

    return rc;
}

/* do we really need this in the app? (in the sim, yes) */
void volume_size(IF_MV(int volume,) unsigned long *sizep, unsigned long *freep)
{
    unsigned long size = 0, free = 0;
    char volpath[MAX_PATH];
    struct statfs fs;

    if (os_volume_path(IF_MV(volume,) volpath, sizeof (volpath)) >= 0
        && !statfs(volpath, &fs))
    {
        DEBUGF("statvfs: frsize=%d blocks=%ld bfree=%ld\n",
               (int)fs.f_frsize, (long)fs.f_blocks, (long)fs.f_bfree);
        if (sizep)
            size = (fs.f_blocks / 2) * (fs.f_frsize / 512);

        if (freep)
            free = (fs.f_bfree / 2) * (fs.f_frsize / 512);
    }

    if (sizep)
        *sizep = size;

    if (freep)
        *freep = free;
}