RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
dfs_posix.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2024 RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2009-05-27 Yi.qiu The first version
9 * 2018-02-07 Bernard Change the 3rd parameter of open/fcntl/ioctl to '...'
10 * 2021-08-26 linzhenxing add setcwd and modify getcwd\chdir
11 */
12
13#include <dfs.h>
14#include <unistd.h>
15
16#include <dfs_dentry.h>
17#include <dfs_mnt.h>
18#include "dfs_private.h"
19
20#ifdef RT_USING_SMART
21#include <lwp.h>
22#endif
23
28
48int open(const char *file, int flags, ...)
49{
50 int fd, result;
51 struct dfs_file *df = RT_NULL;
52 mode_t mode = 0;
53
54 if (file == NULL)
55 {
56 rt_set_errno(-EBADF);
57 return -1;
58 }
59
60 if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE)
61 {
62 va_list ap;
63 va_start(ap, flags);
64 mode = va_arg(ap, mode_t);
65 va_end(ap);
66 }
67
68 fd = fd_new();
69 if (fd >= 0)
70 {
71 df = fd_get(fd);
72 }
73 else
74 {
75 rt_set_errno(-RT_ERROR);
76 return RT_NULL;
77 }
78
79 result = dfs_file_open(df, file, flags, mode);
80 if (result < 0)
81 {
82 fd_release(fd);
83 rt_set_errno(result);
84 return -1;
85 }
86
87 return fd;
88}
90
91#ifndef AT_FDCWD
92#define AT_FDCWD (-100)
93#endif
94
110int openat(int dirfd, const char *path, int flag, ...)
111{
112 struct dfs_file *d;
113 char *fullpath;
114 int fd;
115
116 if (!path)
117 {
118 rt_set_errno(-EBADF);
119 return -1;
120 }
121
122 fullpath = (char*)path;
123
124 if (path[0] != '/')
125 {
126 if (dirfd != AT_FDCWD)
127 {
128 d = fd_get(dirfd);
129 if (!d || !d->vnode)
130 {
131 rt_set_errno(-EBADF);
132 return -1;
133 }
134
135 fullpath = dfs_dentry_full_path(d->dentry);
136 if (!fullpath)
137 {
138 rt_set_errno(-ENOMEM);
139 return -1;
140 }
141 }
142 }
143
144 fd = open(fullpath, flag, 0);
145
146 if (fullpath != path)
147 {
148 rt_free(fullpath);
149 }
150
151 return fd;
152}
153
154int utimensat(int __fd, const char *__path, const struct timespec __times[2], int __flags)
155{
156 int ret;
157 struct stat buffer;
158 struct dfs_file *d;
159 char *fullpath;
160 struct dfs_attr attr;
161 time_t current_time;
162 char *link_fn = (char *)rt_malloc(DFS_PATH_MAX);
163 int err;
164
165 if (__path == NULL)
166 {
167 return -EFAULT;
168 }
169
170 if (__path[0] == '/' || __fd == AT_FDCWD)
171 {
172 if (stat(__path, &buffer) < 0)
173 {
174 return -ENOENT;
175 }
176 else
177 {
178 fullpath = (char*)__path;
179 }
180 }
181 else
182 {
183 if (__fd != AT_FDCWD)
184 {
185 d = fd_get(__fd);
186 if (!d || !d->vnode)
187 {
188 return -EBADF;
189 }
190
191 fullpath = dfs_dentry_full_path(d->dentry);
192 if (!fullpath)
193 {
194 rt_set_errno(-ENOMEM);
195 return -1;
196 }
197 }
198 }
199
200 /*update time*/
202 time(&current_time);
203 if (UTIME_NOW == __times[0].tv_nsec)
204 {
205 attr.ia_atime.tv_sec = current_time;
206 }
207 else if (UTIME_OMIT != __times[0].tv_nsec)
208 {
209 attr.ia_atime.tv_sec = __times[0].tv_sec;
210 }
211 else
212 {
214 }
215
216 if (UTIME_NOW == __times[1].tv_nsec)
217 {
218 attr.ia_mtime.tv_sec = current_time;
219 }
220 else if (UTIME_OMIT == __times[1].tv_nsec)
221 {
222 attr.ia_mtime.tv_sec = __times[1].tv_sec;
223 }
224 else
225 {
227 }
228
229 if (dfs_file_lstat(fullpath, &buffer) == 0)
230 {
231 if (S_ISLNK(buffer.st_mode) && (__flags != AT_SYMLINK_NOFOLLOW))
232 {
233 if (link_fn)
234 {
235 err = dfs_file_readlink(fullpath, link_fn, DFS_PATH_MAX);
236 if (err < 0)
237 {
238 rt_free(link_fn);
239 return -ENOENT;
240 }
241 else
242 {
243 fullpath = link_fn;
244 if (dfs_file_stat(fullpath, &buffer) != 0)
245 {
246 rt_free(link_fn);
247 return -ENOENT;
248 }
249 }
250 }
251
252 }
253 }
254 attr.st_mode = buffer.st_mode;
255 ret = dfs_file_setattr(fullpath, &attr);
256 rt_free(link_fn);
257
258 return ret;
259}
260
270int creat(const char *path, mode_t mode)
271{
272 return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
273}
275
284int close(int fd)
285{
286 int result;
287 struct dfs_file *file;
288
289 file = fd_get(fd);
290 if (file == NULL)
291 {
292 rt_set_errno(-EBADF);
293
294 return -1;
295 }
296
297 result = dfs_file_close(file);
298 if (result < 0)
299 {
300 rt_set_errno(result);
301
302 return -1;
303 }
304
305 fd_release(fd);
306
307 return 0;
308}
310
322#if defined(RT_USING_NEWLIB) && defined(_EXFUN)
323_READ_WRITE_RETURN_TYPE _EXFUN(read, (int fd, void *buf, size_t len))
324#else
325ssize_t read(int fd, void *buf, size_t len)
326#endif
327{
328 ssize_t result;
329 struct dfs_file *file;
330
331 if (buf == NULL)
332 {
333 rt_set_errno(-EBADF);
334 return -1;
335 }
336
337 file = fd_get(fd);
338 if (file == NULL)
339 {
340 rt_set_errno(-EBADF);
341
342 return -1;
343 }
344
345 result = dfs_file_read(file, buf, len);
346 if (result < 0)
347 {
348 rt_set_errno(result);
349
350 return -1;
351 }
352
353 return result;
354}
356
367#if defined(RT_USING_NEWLIB) && defined(_EXFUN)
368_READ_WRITE_RETURN_TYPE _EXFUN(write, (int fd, const void *buf, size_t len))
369#else
370ssize_t write(int fd, const void *buf, size_t len)
371#endif
372{
373 ssize_t result;
374 struct dfs_file *file;
375
376 if (buf == NULL)
377 {
378 rt_set_errno(-EBADF);
379 return -1;
380 }
381
382 file = fd_get(fd);
383 if (file == NULL)
384 {
385 rt_set_errno(-EBADF);
386
387 return -1;
388 }
389
390 result = dfs_file_write(file, buf, len);
391 if (result < 0)
392 {
393 rt_set_errno(result);
394
395 return -1;
396 }
397
398 return result;
399}
401
420off_t lseek(int fd, off_t offset, int whence)
421{
422 off_t result;
423 struct dfs_file *file;
424
425 file = fd_get(fd);
426 if (file == NULL)
427 {
428 rt_set_errno(-EBADF);
429
430 return -1;
431 }
432
433 result = dfs_file_lseek(file, offset, whence);
434 if (result < 0)
435 {
436 rt_set_errno(-EPERM);
437
438 return -1;
439 }
440
441 return result;
442}
444
456int rename(const char *old_file, const char *new_file)
457{
458 int result;
459
460 if (old_file == NULL || new_file == NULL)
461 {
462 rt_set_errno(-EBADF);
463 return -1;
464 }
465
466 result = dfs_file_rename(old_file, new_file);
467 if (result < 0)
468 {
469 rt_set_errno(result);
470
471 return -1;
472 }
473
474 return 0;
475}
477
486int unlink(const char *pathname)
487{
488 int result;
489 struct stat stat;
490
491 if (pathname == NULL)
492 {
493 rt_set_errno(-EBADF);
494 return -1;
495 }
496
497 result = dfs_file_lstat(pathname, &stat);
498 if (result == 0 && S_ISDIR(stat.st_mode))
499 {
500 rt_set_errno(-RT_ERROR);
501
502 return -1;
503 }
504
505 result = dfs_file_unlink(pathname);
506 if (result < 0)
507 {
508 rt_set_errno(result);
509
510 return -1;
511 }
512
513 return 0;
514}
516
517#ifndef _WIN32 /* we can not implement these functions */
526int stat(const char *file, struct stat *buf)
527{
528 int result;
529
530 if (file == NULL || buf == NULL)
531 {
532 rt_set_errno(EBADF);
533 return -1;
534 }
535
536 result = dfs_file_stat(file, buf);
537 if (result < 0)
538 {
539 rt_set_errno(-result);
540 }
541
542 return result;
543}
545
554int fstat(int fildes, struct stat *buf)
555{
556 int ret = -1;
557 struct dfs_file *file;
558
559 if (buf == NULL)
560 {
561 rt_set_errno(-EBADF);
562 return -1;
563 }
564
565 /* get the fd */
566 file = fd_get(fildes);
567 if (file == NULL)
568 {
569 rt_set_errno(-EBADF);
570
571 return -1;
572 }
573
574 if (dfs_is_mounted(file->dentry->mnt) == 0)
575 {
576 ret = file->dentry->mnt->fs_ops->stat(file->dentry, buf);
577 }
578
579 return ret;
580}
582#endif
583
594int fsync(int fildes)
595{
596 int ret;
597 struct dfs_file *file;
598
599 file = fd_get(fildes);
600 if (file == NULL)
601 {
602 rt_set_errno(-EBADF);
603
604 return -1;
605 }
606
607 ret = dfs_file_fsync(file);
608
609 return ret;
610}
612
631int fcntl(int fildes, int cmd, ...)
632{
633 int ret = -1;
634 struct dfs_file *file;
635
636 file = fd_get(fildes);
637 if (file)
638 {
639 void *arg;
640 va_list ap;
641
642 va_start(ap, cmd);
643 arg = va_arg(ap, void *);
644 va_end(ap);
645
646 ret = dfs_file_ioctl(file, cmd, arg);
647 if (ret < 0)
648 {
649 ret = dfs_file_fcntl(fildes, cmd, (unsigned long)arg);
650 }
651 }
652 else
653 {
654 ret = -EBADF;
655 }
656
657 if (ret < 0)
658 {
659 rt_set_errno(ret);
660 ret = -1;
661 }
662
663 return ret;
664}
666
679int ioctl(int fildes, int cmd, ...)
680{
681 void *arg;
682 va_list ap;
683
684 va_start(ap, cmd);
685 arg = va_arg(ap, void *);
686 va_end(ap);
687
688 /* we use fcntl for this API. */
689 return fcntl(fildes, cmd, arg);
690}
692
703int ftruncate(int fd, off_t length)
704{
705 int result;
706 struct dfs_file *file;
707
708 file = fd_get(fd);
709 if (file == NULL)
710 {
711 rt_set_errno(-EBADF);
712
713 return -1;
714 }
715
716 if (length < 0)
717 {
718 rt_set_errno(-EINVAL);
719
720 return -1;
721 }
722
723 result = dfs_file_ftruncate(file, length);
724 if (result < 0)
725 {
726 rt_set_errno(result);
727
728 return -1;
729 }
730
731 return 0;
732}
734
744int statfs(const char *path, struct statfs *buf)
745{
746 int result;
747
748 if (path == NULL || buf == NULL)
749 {
750 rt_set_errno(-EBADF);
751 return -1;
752 }
753
754 result = dfs_statfs(path, buf);
755 if (result < 0)
756 {
757 rt_set_errno(result);
758
759 return -1;
760 }
761
762 return result;
763}
765
775int fstatfs(int fildes, struct statfs *buf)
776{
777 int ret = -1;
778 struct dfs_file *file;
779
780 if (buf == NULL)
781 {
782 rt_set_errno(-EBADF);
783 return -1;
784 }
785
786 /* get the fd */
787 file = fd_get(fildes);
788 if (file == NULL)
789 {
790 rt_set_errno(-EBADF);
791
792 return -1;
793 }
794
795 if (dfs_is_mounted(file->dentry->mnt) == 0)
796 {
797 ret = file->dentry->mnt->fs_ops->statfs(file->dentry->mnt, buf);
798 }
799
800 return ret;
801}
803
812int mkdir(const char *path, mode_t mode)
813{
814 int result;
815 struct stat stat;
816 struct dfs_file file;
817
818 if (path == NULL)
819 {
820 rt_set_errno(-EBADF);
821 return -1;
822 }
823
824 if (path && dfs_file_lstat(path, &stat) == 0)
825 {
826 rt_set_errno(-EEXIST);
827 return -1;
828 }
829
830 dfs_file_init(&file);
831
832 result = dfs_file_open(&file, path, O_DIRECTORY | O_CREAT, mode);
833 if (result >= 0)
834 {
835 dfs_file_close(&file);
836 result = 0;
837 }
838 else
839 {
840 rt_set_errno(result);
841 result = -1;
842 }
843
844 dfs_file_deinit(&file);
845
846 return result;
847}
849
850#ifdef RT_USING_FINSH
851#include <finsh.h>
852FINSH_FUNCTION_EXPORT(mkdir, create a directory);
853#endif
854
862int rmdir(const char *pathname)
863{
864 int result;
865 DIR *dir = RT_NULL;
866 struct stat stat;
867
868 if (!pathname)
869 {
870 rt_set_errno(-EPERM);
871 return -1;
872 }
873
874 dir = opendir(pathname);
875 if (dir)
876 {
877 struct dirent *dirent;
878
879 while (1)
880 {
881 dirent = readdir(dir);
882 if (dirent == RT_NULL)
883 break;
884 if (rt_strcmp(".", dirent->d_name) != 0 &&
885 rt_strcmp("..", dirent->d_name) != 0)
886 {
887 break;
888 }
889 }
890
891 closedir(dir);
892
893 if (dirent)
894 {
895 rt_set_errno(-EPERM);
896 return -1;
897 }
898 }
899
900 if (dfs_file_lstat(pathname, &stat) == 0)
901 {
902 if (S_ISLNK(stat.st_mode))
903 {
904 rt_set_errno(-EPERM);
905 return -1;
906 }
907 }
908
909 result = dfs_file_unlink(pathname);
910 if (result < 0)
911 {
912 rt_set_errno(result);
913
914 return -1;
915 }
916
917 return 0;
918}
920
928DIR *opendir(const char *name)
929{
930 DIR *t = RT_NULL;
931 int fd, result;
932 struct dfs_file *file = RT_NULL;
933
934 if (!name || dfs_file_isdir(name) != 0)
935 {
936 rt_set_errno(-RT_ERROR);
937 return RT_NULL;
938 }
939
940 fd = fd_new();
941 if (fd >= 0)
942 {
943 file = fd_get(fd);
944 }
945 else
946 {
947 rt_set_errno(-RT_ERROR);
948 return RT_NULL;
949 }
950
951 result = dfs_file_open(file, name, O_RDONLY | O_DIRECTORY, 0);
952 if (result >= 0)
953 {
954 /* open successfully */
955 t = (DIR *) rt_malloc(sizeof(DIR));
956 if (t == NULL)
957 {
958 dfs_file_close(file);
959 fd_release(fd);
960 }
961 else
962 {
963 rt_memset(t, 0, sizeof(DIR));
964
965 t->fd = fd;
966 }
967
968 return t;
969 }
970
971 fd_release(fd);
972 rt_set_errno(result);
973
974 return NULL;
975}
977
987struct dirent *readdir(DIR *d)
988{
989 int result;
990 struct dirent *dirent = NULL;
991
992 if (d == NULL)
993 {
994 rt_set_errno(-EBADF);
995 return NULL;
996 }
997
998 do
999 {
1000 if (d->num)
1001 {
1002 struct dirent *dirent_ptr;
1003 dirent_ptr = (struct dirent *)&d->buf[d->cur];
1004 d->cur += dirent_ptr->d_reclen;
1005 }
1006
1007 if (!d->num || d->cur >= d->num)
1008 {
1009 /* get a new entry */
1010 result = dfs_file_getdents(fd_get(d->fd),
1011 (struct dirent *)d->buf,
1012 sizeof(d->buf) - 1);
1013 if (result <= 0)
1014 {
1015 rt_set_errno(result);
1016
1017 return NULL;
1018 }
1019
1020 d->num = result;
1021 d->cur = 0; /* current entry index */
1022 }
1023
1024 dirent = (struct dirent *)(d->buf + d->cur);
1025 if (rt_strcmp(".", dirent->d_name) != 0 &&
1026 rt_strcmp("..", dirent->d_name) != 0)
1027 {
1028 break;
1029 }
1030 } while (dirent);
1031
1032 return dirent;
1033}
1035
1044long telldir(DIR *d)
1045{
1046 struct dfs_file *file;
1047 long result;
1048
1049 if (d == NULL)
1050 {
1051 rt_set_errno(-EBADF);
1052 return -1;
1053 }
1054
1055 file = fd_get(d->fd);
1056 if (file == NULL)
1057 {
1058 rt_set_errno(-EBADF);
1059
1060 return 0;
1061 }
1062
1063 result = file->fpos - d->num + d->cur;
1064
1065 return result;
1066}
1068
1076void seekdir(DIR *d, long offset)
1077{
1078 struct dfs_file *file;
1079
1080 if (d == NULL)
1081 {
1082 rt_set_errno(-EBADF);
1083 return;
1084 }
1085
1086 file = fd_get(d->fd);
1087 if (file == NULL)
1088 {
1089 rt_set_errno(-EBADF);
1090
1091 return;
1092 }
1093
1094 if (d && d->fd > 0)
1095 {
1096 if (file->fpos > offset)
1097 {
1098 /* seek to the offset position of directory */
1099 if (dfs_file_lseek(fd_get(d->fd), 0, SEEK_SET) >= 0)
1100 d->num = d->cur = 0;
1101 }
1102
1103 while(file->fpos < offset)
1104 {
1105 if (!readdir(d))
1106 {
1107 break;
1108 }
1109 }
1110 }
1111}
1113
1120void rewinddir(DIR *d)
1121{
1122 if (d && d->fd > 0)
1123 {
1124 /* seek to the beginning of directory */
1125 if (dfs_file_lseek(fd_get(d->fd), 0, SEEK_SET) >= 0)
1126 d->num = d->cur = 0;
1127 }
1128}
1130
1139int closedir(DIR *d)
1140{
1141 int result;
1142 struct dfs_file *file;
1143
1144 if (d == NULL)
1145 {
1146 rt_set_errno(-EBADF);
1147 return -1;
1148 }
1149
1150 file = fd_get(d->fd);
1151 if (file == NULL)
1152 {
1153 rt_set_errno(-EBADF);
1154 return -1;
1155 }
1156
1157 result = dfs_file_close(file);
1158 if (result < 0)
1159 {
1160 rt_set_errno(result);
1161
1162 return -1;
1163 }
1164 else
1165 {
1166 fd_release(d->fd);
1167 rt_free(d);
1168 }
1169
1170 return 0;
1171}
1173
1174#ifdef DFS_USING_WORKDIR
1183int chdir(const char *path)
1184{
1185 char *fullpath;
1186 DIR *d;
1187
1188 if (path == NULL)
1189 {
1190 dfs_lock();
1191#ifdef DFS_USING_WORKDIR
1193#endif
1194 dfs_unlock();
1195
1196 rt_set_errno(-ENOTDIR);
1197 return -1;
1198 }
1199
1200 if (strlen(path) > DFS_PATH_MAX)
1201 {
1202 rt_set_errno(-ENOTDIR);
1203
1204 return -1;
1205 }
1206
1207 fullpath = dfs_normalize_path(NULL, path);
1208 if (fullpath == NULL)
1209 {
1210 rt_set_errno(-ENOTDIR);
1211
1212 return -1; /* build path failed */
1213 }
1214
1215 dfs_lock();
1216 d = opendir(fullpath);
1217 if (d == NULL)
1218 {
1219 rt_free(fullpath);
1220 /* this is a not exist directory */
1221 dfs_unlock();
1222
1223 rt_set_errno(-ENOTDIR);
1224 return -1;
1225 }
1226
1227 /* close directory stream */
1228 closedir(d);
1229#ifdef RT_USING_SMART
1230 /* copy full path to working directory */
1231 lwp_setcwd(fullpath);
1232#else
1233 rt_strncpy(working_directory, fullpath, DFS_PATH_MAX);
1234#endif
1235 /* release normalize directory path name */
1236 rt_free(fullpath);
1237
1238 dfs_unlock();
1239
1240 return 0;
1241}
1242RTM_EXPORT(chdir);
1243
1244#ifdef RT_USING_FINSH
1245FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
1246#endif
1247#endif
1248
1258int access(const char *path, int amode)
1259{
1260 struct stat st;
1261
1262 if (path == NULL)
1263 {
1264 rt_set_errno(-EINVAL);
1265 return -1;
1266 }
1267
1268 if (stat(path, &st) < 0)
1269 {
1270 rt_set_errno(-ENOENT);
1271 return -1;
1272 }
1273
1274 if (amode == F_OK)
1275 {
1276 return 0;
1277 }
1278
1279 if ((amode & R_OK) && !(st.st_mode & S_IRUSR))
1280 {
1281 rt_set_errno(-EACCES);
1282 return -1;
1283 }
1284
1285 if ((amode & W_OK) && !(st.st_mode & S_IWUSR))
1286 {
1287 rt_set_errno(-EACCES);
1288 return -1;
1289 }
1290
1291 if ((amode & X_OK) && !(st.st_mode & S_IXUSR))
1292 {
1293 rt_set_errno(-EACCES);
1294 return -1;
1295 }
1296
1297 return 0;
1298}
1299
1306void setcwd(char *buf)
1307{
1308 if (buf == NULL)
1309 {
1310 rt_set_errno(-EBADF);
1311 return;
1312 }
1313
1314#ifdef DFS_USING_WORKDIR
1315 dfs_lock();
1316#ifdef RT_USING_SMART
1317 lwp_setcwd(buf);
1318#else
1319 rt_strncpy(working_directory, buf, DFS_PATH_MAX);
1320#endif
1321 dfs_unlock();
1322#else
1324#endif
1325
1326 return ;
1327}
1329
1339char *getcwd(char *buf, size_t size)
1340{
1341 if (buf == NULL)
1342 {
1343 rt_set_errno(-EBADF);
1344 return NULL;
1345 }
1346
1347#ifdef DFS_USING_WORKDIR
1348 char *dir_buf = RT_NULL;
1349
1350 dfs_lock();
1351
1352#ifdef RT_USING_SMART
1353 dir_buf = lwp_getcwd();
1354#else
1355 dir_buf = &working_directory[0];
1356#endif
1357
1358 /* copy to buf parameter */
1359 if (buf)
1360 {
1361 rt_strncpy(buf, dir_buf, size);
1362 }
1363
1364 dfs_unlock();
1365#else
1367#endif
1368
1369 return buf;
1370}
1372
1385ssize_t pread(int fd, void *buf, size_t len, off_t offset)
1386{
1387 ssize_t result;
1388 off_t fpos;
1389 struct dfs_file *file;
1390
1391 if (buf == NULL)
1392 {
1393 rt_set_errno(-EBADF);
1394 return -1;
1395 }
1396
1397 file = fd_get(fd);
1398 if (file == NULL)
1399 {
1400 rt_set_errno(-EBADF);
1401
1402 return -1;
1403 }
1404
1405 /* fpos lock */
1406 fpos = dfs_file_get_fpos(file);
1407 result = dfs_file_pread(file, buf, len, offset);
1408 /* fpos unlock */
1409 dfs_file_set_fpos(file, fpos);
1410 if (result < 0)
1411 {
1412 rt_set_errno(result);
1413
1414 return -1;
1415 }
1416
1417 return result;
1418}
1420
1432ssize_t pwrite(int fd, const void *buf, size_t len, off_t offset)
1433{
1434 ssize_t result;
1435 off_t fpos;
1436 struct dfs_file *file;
1437
1438 if (buf == NULL)
1439 {
1440 rt_set_errno(-EBADF);
1441 return -1;
1442 }
1443
1444 file = fd_get(fd);
1445 if (file == NULL)
1446 {
1447 rt_set_errno(-EBADF);
1448
1449 return -1;
1450 }
1451 /* fpos lock */
1452 fpos = dfs_file_get_fpos(file);
1453 result = dfs_file_pwrite(file, buf, len, offset);
1454 /* fpos unlock */
1455 dfs_file_set_fpos(file, fpos);
1456 if (result < 0)
1457 {
1458 rt_set_errno(result);
1459
1460 return -1;
1461 }
1462
1463 return result;
1464}
1466
rt_err_t dfs_lock(void)
定义 dfs.c:120
void dfs_unlock(void)
定义 dfs.c:137
#define ATTR_MTIME_SET
定义 dfs.h:37
#define ATTR_ATIME_SET
定义 dfs.h:33
#define UTIME_OMIT
定义 dfs.h:57
#define UTIME_NOW
定义 dfs.h:53
#define AT_SYMLINK_NOFOLLOW
定义 dfs.h:49
#define DFS_PATH_MAX
定义 dfs.h:72
char * dfs_dentry_full_path(struct dfs_dentry *dentry)
int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes)
int dfs_file_lstat(const char *path, struct stat *buf)
off_t dfs_file_get_fpos(struct dfs_file *file)
int dfs_file_unlink(const char *path)
int dfs_file_ftruncate(struct dfs_file *file, off_t length)
void dfs_file_init(struct dfs_file *file)
ssize_t dfs_file_pwrite(struct dfs_file *file, const void *buf, size_t len, off_t offset)
off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece)
int dfs_file_setattr(const char *path, struct dfs_attr *attr)
int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args)
ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len)
ssize_t dfs_file_pread(struct dfs_file *file, void *buf, size_t len, off_t offset)
int dfs_file_fsync(struct dfs_file *file)
int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
int dfs_file_isdir(const char *path)
int dfs_file_close(struct dfs_file *file)
int dfs_file_readlink(const char *path, char *buf, int bufsize)
int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mode)
void dfs_file_set_fpos(struct dfs_file *file, off_t fpos)
void dfs_file_deinit(struct dfs_file *file)
int dfs_file_stat(const char *path, struct stat *buf)
int dfs_file_rename(const char *old_file, const char *new_file)
ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len)
int dfs_statfs(const char *path, struct statfs *buffer)
int dfs_is_mounted(struct dfs_mnt *mnt)
char working_directory[]
#define NO_WORKING_DIR
int fd_new(void)
定义 dfs.c:345
char * dfs_normalize_path(const char *directory, const char *filename)
定义 dfs.c:779
struct dfs_file * fd_get(int fd)
定义 dfs.c:367
void fd_release(int fd)
定义 dfs.c:359
ssize_t write(int fd, const void *buf, size_t len)
int closedir(DIR *d)
int openat(int dirfd, const char *path, int flag,...)
Opens a file relative to a directory file descriptor.
void rewinddir(DIR *d)
int ftruncate(int fd, off_t length)
ssize_t read(int fd, void *buf, size_t len)
int rename(const char *old_file, const char *new_file)
#define AT_FDCWD
struct dirent * readdir(DIR *d)
DIR * opendir(const char *name)
int mkdir(const char *path, mode_t mode)
void setcwd(char *buf)
int rmdir(const char *pathname)
void seekdir(DIR *d, long offset)
int fstatfs(int fildes, struct statfs *buf)
int fsync(int fildes)
int ioctl(int fildes, int cmd,...)
off_t lseek(int fd, off_t offset, int whence)
int open(const char *file, int flags,...)
int utimensat(int __fd, const char *__path, const struct timespec __times[2], int __flags)
int access(const char *path, int amode)
long telldir(DIR *d)
int close(int fd)
int fstat(int fildes, struct stat *buf)
int stat(const char *file, struct stat *buf)
char * getcwd(char *buf, size_t size)
int fcntl(int fildes, int cmd,...)
ssize_t pwrite(int fd, const void *buf, size_t len, off_t offset)
int statfs(const char *path, struct statfs *buf)
ssize_t pread(int fd, void *buf, size_t len, off_t offset)
int unlink(const char *pathname)
int creat(const char *path, mode_t mode)
#define rt_kprintf(...)
rt_weak void rt_free(void *ptr)
This function will release the previously allocated memory block by rt_malloc. The released memory bl...
rt_weak void * rt_malloc(rt_size_t size)
Allocate a block of memory with a minimum of 'size' bytes.
#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
#define FINSH_FUNCTION_EXPORT(name, desc)
#define RTM_EXPORT(symbol)
定义 rtm.h:33
#define RT_NULL
mode_t st_mode
struct timespec ia_mtime
struct timespec ia_atime
unsigned int ia_valid
struct dfs_mnt * mnt
struct dfs_vnode * vnode
struct dfs_dentry * dentry
uint32_t flags
uint16_t mode
int(* statfs)(struct dfs_mnt *mnt, struct statfs *buf)
int(* stat)(struct dfs_dentry *dentry, struct stat *buf)
const struct dfs_filesystem_ops * fs_ops