summaryrefslogtreecommitdiff
path: root/platform/rtthread/rt_vfs_dfs.c
blob: 6bd2f20ff82e6cac43633adcbaf43919b0931f48 (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
/*
 * Copyright (c) 2026, sakumisu
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include <rtthread.h>
#include <rtdevice.h>

#include <string.h>
#include <errno.h>

#include <dfs_fs.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include "usbd_core.h"
#include "usb_vfs.h"

#ifndef RT_USING_DFS
#error "RT_USING_DFS must be enabled to use adb sync over the RT-Thread file system"
#endif

#ifndef CONFIG_USB_DFS_MOUNT_POINT
#define CONFIG_USB_DFS_MOUNT_POINT "/"
#endif

/* -------------------------------------------------------------------------
 * helpers
 * ---------------------------------------------------------------------- */

/**
 * POSIX errno as a plain positive value.
 *
 * RT-Thread stores whatever code it was handed, and the DFS layer is not
 * consistent about the sign, so it is normalised before being looked at.
 */
static int dfs_errno(void)
{
    int err = errno;

    return (err < 0) ? -err : err;
}

/** errno -> VFS_ERR_*, so the host gets a readable reason. */
static int dfs_errno_to_vfs(int err)
{
    switch (err) {
        case ENOENT:
            return VFS_ERR_NOENT;
        case EEXIST:
            return VFS_ERR_EXIST;
        case EINVAL:
            return VFS_ERR_INVAL;
        case ENOTDIR:
            return VFS_ERR_NOTDIR;
        case EISDIR:
            return VFS_ERR_ISDIR;
        case ENOSPC:
            return VFS_ERR_NOSPC;
        case ENOMEM:
            return VFS_ERR_NOMEM;
        case EBUSY:
        case EAGAIN:
            return VFS_ERR_AGAIN;
        case ENODEV:
            return VFS_ERR_NODEV;
        case EROFS:
        case EACCES:
        case EPERM:
            return VFS_ERR_ROFS;
        case EIO:
        default:
            return VFS_ERR_IO;
    }
}

const char *vfs_get_fs_root_path(void)
{
    return CONFIG_USB_DFS_MOUNT_POINT;
}

const char *vfs_get_fs_description(void)
{
    return "CherryUSB VFS DFS";
}

int vfs_mkdir(const char *path)
{
    if (!path || !path[0]) {
        return VFS_ERR_INVAL;
    }

    if (mkdir(path, 0777) < 0) {
        /* adb push of a directory tree re-sends SEND for directories it has
         * already created, so "it is already there" has to count as success */
        int err = dfs_errno();

        if (err != EEXIST) {
            return dfs_errno_to_vfs(err);
        }
    }

    return 0;
}

int vfs_rmdir(const char *path)
{
    if (!path || !path[0]) {
        return VFS_ERR_INVAL;
    }

    if (rmdir(path) < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return 0;
}

VFS_DIR *vfs_opendir(const char *name)
{
    DIR *dir;

    if (!name || !name[0]) {
        return NULL;
    }

    dir = opendir(name);
    if (!dir) {
        return NULL;
    }

    return (VFS_DIR *)dir;
}

int vfs_closedir(VFS_DIR *dir)
{
    if (!dir) {
        return VFS_ERR_INVAL;
    }

    if (closedir((DIR *)dir) < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return 0;
}

struct vfs_dirent *vfs_readdir(VFS_DIR *dir)
{
    static struct vfs_dirent dirent;
    struct dirent *entry;

    if (!dir) {
        return NULL;
    }

    entry = readdir((DIR *)dir);
    if (!entry) {
        return NULL; /* end of directory, or a hard error */
    }

    memset(&dirent, 0, sizeof(dirent));
    strncpy(dirent.d_name, entry->d_name, sizeof(dirent.d_name) - 1);
    dirent.d_name[sizeof(dirent.d_name) - 1] = '\0';
    dirent.d_namlen = (uint8_t)strlen(dirent.d_name);
    /* d_type is just the type field of a mode shifted down, see usb_vfs.h.
     * RT-Thread hands back DT_*, so the two cases that matter are translated
     * and anything else (including DT_UNKNOWN) falls back to a regular file. */
    dirent.d_type = (entry->d_type == DT_DIR) ? (uint8_t)(VFS_S_IFDIR >> 12)
                                              : (uint8_t)(VFS_S_IFREG >> 12);

    return &dirent;
}

int vfs_stat(const char *path, struct vfs_stat *buf)
{
    struct stat st;

    if (!path || !path[0] || !buf) {
        return VFS_ERR_INVAL;
    }

    if (stat(path, &st) < 0) {
        /* Stay quiet on purpose: adb stats a path precisely to find out whether
         * it exists, so "not found" is a normal answer and not an error. The
         * service reports it to the host as "mode 0". */
        return dfs_errno_to_vfs(dfs_errno());
    }

    memset(buf, 0, sizeof(*buf));
    if (S_ISDIR(st.st_mode)) {
        buf->st_mode = VFS_S_IFDIR | VFS_S_IRWXU | VFS_S_IRWXG | VFS_S_IRWXO;
    } else {
        buf->st_mode = VFS_S_IFREG | VFS_S_IRUSR | VFS_S_IRGRP | VFS_S_IROTH |
                       VFS_S_IWUSR | VFS_S_IWGRP | VFS_S_IWOTH;
    }

    buf->st_dev = 0;
    buf->st_nlink = 1;
    buf->st_size = st.st_size;
    buf->st_mtime = st.st_mtime;
    /* st_blksize/st_blocks are left at zero: RT-Thread's struct stat does not
     * carry them consistently across DFS versions, and the adb STAT record
     * only ever reports mode, size and mtime. */
    return 0;
}

int vfs_statfs(const char *path, struct vfs_statfs *buf)
{
    struct statfs st;

    if (!path || !path[0] || !buf) {
        return VFS_ERR_INVAL;
    }

    if (dfs_statfs(path, &st) < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    buf->f_bsize = st.f_bsize;
    buf->f_blocks = st.f_blocks;
    buf->f_bfree = st.f_bfree;
    return 0;
}

int vfs_open(const char *path, uint8_t mode)
{
    int flags;
    int fd;

    if (!path || !path[0]) {
        return VFS_ERR_INVAL;
    }

    if (mode == VFS_O_RDONLY) {
        flags = O_RDONLY; /* existing file only */
    } else if (mode == VFS_O_WRONLY) {
        flags = O_WRONLY | O_CREAT | O_TRUNC; /* create / truncate */
    } else if (mode == VFS_O_RDWR) {
        flags = O_RDWR | O_CREAT; /* create, no truncate */
    } else {
        return VFS_ERR_INVAL;
    }

    fd = open(path, flags, 0666);
    if (fd < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return fd;
}

int vfs_close(int fd)
{
    if (fd < 0) {
        return VFS_ERR_INVAL;
    }

    if (close(fd) < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return 0;
}

int vfs_read(int fd, void *buf, size_t len)
{
    int ret;

    if (fd < 0) {
        return VFS_ERR_INVAL;
    }

    ret = (int)read(fd, buf, len);
    if (ret < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return ret;
}

int vfs_write(int fd, const void *buf, size_t len)
{
    int ret;

    if (fd < 0) {
        return VFS_ERR_INVAL;
    }

    ret = (int)write(fd, buf, len);
    if (ret < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return ret;
}

int vfs_unlink(const char *path)
{
    if (!path || !path[0]) {
        return VFS_ERR_INVAL;
    }

    if (unlink(path) < 0) {
        return dfs_errno_to_vfs(dfs_errno());
    }

    return 0;
}