~aleteoryx/muditaos

ref: 46aa1ee4e7899f535e48616fd2f303dfc1f2ee0f muditaos/module-vfs/src/newlib/vfs_io_syscalls.cpp -rw-r--r-- 10.8 KiB
46aa1ee4 — Lucjan Bryndza [EGD-4505] Fix problem with fread (#1092) 5 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include <vfs.hpp>
#include <errno.h>
#include <dirent.h>
#include <log/log.hpp>
#include <fcntl.h>
#include <cstring>
#include <unistd.h>
#include "vfs_internal_dirent.hpp"
#include "HandleManager.hpp"
#include <newlib/vfs_io_syscalls.hpp>
#include <deprecated/vfs.hpp>

// NOTE: It will be removed in later stage
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

/** NOTE: This is generic wrapper for newlib syscalls
 * using current VFS implementation. This implementation
 * will be removed in next release when disk manager
 * and new VFS filesystem will be ready
 */

namespace vfsn::internal
{
    namespace
    {
        stdlib::HandleManager<FF_FILE> gFileHandles;
    }

    namespace
    {
        auto open_to_fopen_flags(int flags)
        {
            flags &= ~0x00010000; // Ignore binary
            if ((flags & O_RDONLY) || !flags) {
                return "r";
            }
            else if (flags & (O_WRONLY | O_CREAT | O_TRUNC)) {
                return "w";
            }
            else if (flags & (O_WRONLY | O_CREAT | O_APPEND)) {
                return "a";
            }
            else if (flags & O_RDWR) {
                return "r+";
            }
            else if (flags & (O_RDWR | O_CREAT | O_TRUNC)) {
                return "w+";
            }
            else if (flags & (O_RDWR | O_CREAT | O_APPEND)) {
                return "a+";
            }
            else {
                LOG_ERROR("Unsupported flag %08x", flags);
                return "";
            }
        }

    } // namespace

} // namespace vfsn::internal

namespace vfsn::internal::syscalls
{
    int open(int &_errno_, const char *file, int flags, int mode)
    {
        auto fflags = open_to_fopen_flags(flags);
        if (*fflags == '\0') {
            _errno_ = EINVAL;
            return -1;
        }
        auto fil = ff_fopen(vfs.relativeToRoot(file).c_str(), fflags);
        if (!fil) {
            _errno_ = stdioGET_ERRNO();
            return -1;
        }
        int hwnd = gFileHandles.setHandle(fil);
        if (hwnd < 0) {
            ff_fclose(fil);
            _errno_ = EMFILE;
            LOG_ERROR("Unable to find handle %i", hwnd);
            return -1;
        }
        else {
            _errno_ = 0;
            return hwnd;
        }
    }

    long close(int &_errno_, int fd)
    {
        auto fil = gFileHandles.clearHandle(fd);
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        auto ret = ff_fclose(fil);
        _errno_  = stdioGET_ERRNO();
        return ret;
    }

    long write(int &_errno_, int fd, const void *buf, size_t cnt)
    {
        if (fd == STDERR_FILENO || fd == STDOUT_FILENO) {
            log_WriteToDevice((uint8_t *)buf, cnt);
            return cnt;
        }
        auto fil = gFileHandles[fd];
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        auto ret = ff_fwrite(buf, cnt, 1, fil);
        _errno_  = stdioGET_ERRNO();
        return ret * cnt;
    }

    long read(int &_errno_, int fd, void *buf, size_t cnt)
    {
        auto fil = gFileHandles[fd];
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        auto ret = ff_fread(buf, 1, cnt, fil);
        _errno_  = stdioGET_ERRNO();
        return ret * cnt;
    }

    off_t lseek(int &_errno_, int fd, off_t pos, int dir)
    {
        auto fil = gFileHandles[fd];
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        auto ret = ff_fseek(fil, pos, dir);
        if (ret) {
            _errno_ = stdioGET_ERRNO();
            return static_cast<off_t>(-1);
        }
        ret = ff_ftell(fil);
        if (ret < 0) {
            _errno_ = stdioGET_ERRNO();
            return static_cast<off_t>(-1);
        }
        return ret;
    }
    int fstat(int &_errno_, int fd, struct stat *pstat)
    {
        auto fil = gFileHandles[fd];
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        std::memset(pstat, 0, sizeof(*pstat));
        pstat->st_ino  = fil->ulObjectCluster;
        pstat->st_size = fil->ulFileSize;
        pstat->st_dev  = 0;
        pstat->st_mode = S_IFREG | 0666;
        _errno_        = 0;
        return 0;
    }
    int link(int &_errno_, const char *existing, const char *newLink)
    {
        _errno_ = ENOSYS;
        LOG_ERROR("Syscall %s not supported", __PRETTY_FUNCTION__);
        return -1;
    }
    int unlink(int &_errno_, const char *name)
    {
        const auto rel_name = vfs.relativeToRoot(name);
        auto ret            = ff_remove(rel_name.c_str());
        if (ret && stdioGET_ERRNO() == EISDIR)
            ret = ff_deltree(rel_name.c_str(), nullptr, nullptr);
        _errno_ = stdioGET_ERRNO();
        return ret;
    }

    int fcntl(int &_errno_, int fd, int cmd, int arg)
    {
        _errno_ = ENOSYS;
        LOG_ERROR("Syscall %s not supported", __PRETTY_FUNCTION__);
        return -1;
    }
    int stat(int &_errno_, const char *file, struct stat *pstat)
    {
        FF_Stat_t stat_ff;
        auto ret = ff_stat(vfs.relativeToRoot(file).c_str(), &stat_ff);
        if (!ret) {
            std::memset(pstat, 0, sizeof(*pstat));
            pstat->st_ino  = stat_ff.st_ino;
            pstat->st_size = stat_ff.st_size;
            pstat->st_dev  = stat_ff.st_dev;
            // Linux mode compat
            if (stat_ff.st_mode == FF_IFREG)
                pstat->st_mode = S_IFREG | 0666;
            if (stat_ff.st_mode == FF_IFDIR)
                pstat->st_mode = S_IFDIR | 0777;
        }
        _errno_ = stdioGET_ERRNO();
        // NOTE: ff_stdio uses wrong errno NOADDRESS
        if (_errno_ == EFAULT)
            _errno_ = ENOENT;
        return ret;
    }

    int chdir(int &_errno_, const char *path)
    {
        auto ret = ff_chdir(vfs.relativeToRoot(path).c_str());
        _errno_  = stdioGET_ERRNO();
        return ret;
    }
    char *getcwd(int &_errno_, char *buf, size_t size)
    {
        auto cwd = ff_getcwd(buf, size);
        _errno_  = stdioGET_ERRNO();
        return cwd;
    }
    int rename(int &_errno_, const char *oldName, const char *newName)
    {
        auto ret = ff_rename(vfs.relativeToRoot(oldName).c_str(), vfs.relativeToRoot(newName).c_str(), true);
        _errno_  = stdioGET_ERRNO();
        return ret;
    }

    int mkdir(int &_errno_, const char *path, uint32_t mode)
    {
        auto ret = ff_mkdir(vfs.relativeToRoot(path).c_str());
        _errno_  = stdioGET_ERRNO();
        return ret;
    }

    DIR *opendir(int &_errno_, const char *dirname)
    {
        auto dir      = new DIR;
        dir->dir_data = dirent::diropen(_errno_, vfs.relativeToRoot(dirname).c_str());
        if (!dir->dir_data) {
            delete dir;
            return nullptr;
        }
        dir->position            = 0;
        dir->file_data.d_ino     = -1;
        dir->file_data.d_name[0] = '\0';
        return dir;
    }

    int closedir(int &_errno_, DIR *dirp)
    {
        if (!dirp) {
            _errno_ = EBADF;
            return -1;
        }
        auto res = dirent::dirclose(_errno_, dirp->dir_data);
        delete dirp;
        return res;
    }

    struct dirent *readdir(int &_errno_, DIR *dirp)
    {
        if (!dirp) {
            _errno_ = EBADF;
            return nullptr;
        }
        auto olderrno{_errno_};
        auto res = dirent::dirnext(_errno_, dirp->dir_data);
        auto fff = reinterpret_cast<FF_FindData_t *>(dirp->dir_data->dir_state);
        if (res < 0) {
            if (_errno_ == pdFREERTOS_ERRNO_ENMFILE) {
                _errno_ = olderrno;
            }
            return nullptr;
        }
        dirp->position += 1;
        if (strnlen(fff->pcFileName, NAME_MAX) >= sizeof(dirp->file_data.d_name)) {
            _errno_ = EOVERFLOW;
            return nullptr;
        }
        dirp->file_data.d_ino  = fff->xDirectoryEntry.ulObjectCluster;
        dirp->file_data.d_type = (fff->ucAttributes & FF_FAT_ATTR_DIR) ? DT_DIR : DT_REG;
        std::strncpy(dirp->file_data.d_name, fff->pcFileName, sizeof(dirp->file_data.d_name));
        return &dirp->file_data;
    }

    int readdir_r(int &_errno_, DIR *dirp, struct dirent *entry, struct dirent **result)
    {
        if (!dirp) {
            return EBADF;
        }
        auto olderrno{_errno_};
        auto res = dirent::dirnext(_errno_, dirp->dir_data);
        auto fff = reinterpret_cast<FF_FindData_t *>(dirp->dir_data->dir_state);
        if (res < 0) {
            res = _errno_;
            if (_errno_ == pdFREERTOS_ERRNO_ENMFILE) {
                res = 0;
            }
            _errno_ = olderrno;
            return res;
        }
        dirp->position += 1;
        if (strnlen(fff->pcFileName, NAME_MAX) >= sizeof(entry->d_name)) {
            return EOVERFLOW;
        }
        entry->d_ino  = fff->xDirectoryEntry.ulObjectCluster;
        entry->d_type = (fff->ucAttributes & FF_FAT_ATTR_DIR) ? DT_DIR : DT_REG;
        std::strncpy(entry->d_name, fff->pcFileName, sizeof(entry->d_name));
        *result = entry;
        return 0;
    }

    void rewinddir(int &_errno_, DIR *dirp)
    {
        if (!dirp) {
            return;
        }
        dirent::dirreset(_errno_, dirp->dir_data);
        dirp->position = 0;
    }

    void seekdir(int &_errno_, DIR *dirp, long int loc)
    {
        if (!dirp || loc < 0) {
            return;
        }
        if (dirp->position > loc) {
            dirent::dirreset(_errno_, dirp->dir_data);
            dirp->position = 0;
        }
        while ((dirp->position < loc) && (dirent::dirnext(_errno_, dirp->dir_data) >= 0)) {
            dirp->position += 1;
        }
    }
    long int telldir(int &_errno_, DIR *dirp)
    {
        if (!dirp) {
            return -1;
        }
        return dirp->position;
    }

    int chmod(int &_errno_, const char *path, mode_t mode)
    {
        _errno_ = ENOSYS;
        LOG_ERROR("Syscall %s not supported", __PRETTY_FUNCTION__);
        return -1;
    }
    int fchmod(int &_errno_, int fd, mode_t mode)
    {
        _errno_ = ENOSYS;
        LOG_ERROR("Syscall %s not supported", __PRETTY_FUNCTION__);
        return -1;
    }
    int fsync(int &_errno_, int fd)
    {
        auto fil = gFileHandles[fd];
        if (!fil) {
            LOG_ERROR("Unable to find handle %i", fd);
            _errno_ = EBADF;
            return -1;
        }
        auto ret = ff_fflush(fil);
        _errno_  = stdioGET_ERRNO();
        return ret;
    }
} // namespace vfsn::internal::syscalls

#pragma GCC diagnostic pop