dfs_posix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  11. #include <dfs.h>
  12. #include <dfs_posix.h>
  13. #include "dfs_private.h"
  14. /**
  15. * @addtogroup FsPosixApi
  16. */
  17. /*@{*/
  18. /**
  19. * this function is a POSIX compliant version, which will open a file and
  20. * return a file descriptor according specified flags.
  21. *
  22. * @param file the path name of file.
  23. * @param flags the file open flags.
  24. *
  25. * @return the non-negative integer on successful open, others for failed.
  26. */
  27. int open(const char *file, int flags, ...)
  28. {
  29. int fd, result;
  30. struct dfs_fd *d;
  31. /* allocate a fd */
  32. fd = fd_new();
  33. if (fd < 0)
  34. {
  35. rt_set_errno(-ENOMEM);
  36. return -1;
  37. }
  38. d = fd_get(fd);
  39. result = dfs_file_open(d, file, flags);
  40. if (result < 0)
  41. {
  42. /* release the ref-count of fd */
  43. fd_put(d);
  44. fd_put(d);
  45. rt_set_errno(result);
  46. return -1;
  47. }
  48. /* release the ref-count of fd */
  49. fd_put(d);
  50. return fd;
  51. }
  52. RTM_EXPORT(open);
  53. /**
  54. * this function is a POSIX compliant version, which will close the open
  55. * file descriptor.
  56. *
  57. * @param fd the file descriptor.
  58. *
  59. * @return 0 on successful, -1 on failed.
  60. */
  61. int close(int fd)
  62. {
  63. int result;
  64. struct dfs_fd *d;
  65. d = fd_get(fd);
  66. if (d == NULL)
  67. {
  68. rt_set_errno(-EBADF);
  69. return -1;
  70. }
  71. result = dfs_file_close(d);
  72. fd_put(d);
  73. if (result < 0)
  74. {
  75. rt_set_errno(result);
  76. return -1;
  77. }
  78. fd_put(d);
  79. return 0;
  80. }
  81. RTM_EXPORT(close);
  82. /**
  83. * this function is a POSIX compliant version, which will read specified data
  84. * buffer length for an open file descriptor.
  85. *
  86. * @param fd the file descriptor.
  87. * @param buf the buffer to save the read data.
  88. * @param len the maximal length of data buffer
  89. *
  90. * @return the actual read data buffer length. If the returned value is 0, it
  91. * may be reach the end of file, please check errno.
  92. */
  93. #if defined(RT_USING_NEWLIB) && defined(_EXFUN)
  94. _READ_WRITE_RETURN_TYPE _EXFUN(read, (int fd, void *buf, size_t len))
  95. #else
  96. int read(int fd, void *buf, size_t len)
  97. #endif
  98. {
  99. int result;
  100. struct dfs_fd *d;
  101. /* get the fd */
  102. d = fd_get(fd);
  103. if (d == NULL)
  104. {
  105. rt_set_errno(-EBADF);
  106. return -1;
  107. }
  108. result = dfs_file_read(d, buf, len);
  109. if (result < 0)
  110. {
  111. fd_put(d);
  112. rt_set_errno(result);
  113. return -1;
  114. }
  115. /* release the ref-count of fd */
  116. fd_put(d);
  117. return result;
  118. }
  119. RTM_EXPORT(read);
  120. /**
  121. * this function is a POSIX compliant version, which will write specified data
  122. * buffer length for an open file descriptor.
  123. *
  124. * @param fd the file descriptor
  125. * @param buf the data buffer to be written.
  126. * @param len the data buffer length.
  127. *
  128. * @return the actual written data buffer length.
  129. */
  130. #if defined(RT_USING_NEWLIB) && defined(_EXFUN)
  131. _READ_WRITE_RETURN_TYPE _EXFUN(write, (int fd, const void *buf, size_t len))
  132. #else
  133. int write(int fd, const void *buf, size_t len)
  134. #endif
  135. {
  136. int result;
  137. struct dfs_fd *d;
  138. /* get the fd */
  139. d = fd_get(fd);
  140. if (d == NULL)
  141. {
  142. rt_set_errno(-EBADF);
  143. return -1;
  144. }
  145. result = dfs_file_write(d, buf, len);
  146. if (result < 0)
  147. {
  148. fd_put(d);
  149. rt_set_errno(result);
  150. return -1;
  151. }
  152. /* release the ref-count of fd */
  153. fd_put(d);
  154. return result;
  155. }
  156. RTM_EXPORT(write);
  157. /**
  158. * this function is a POSIX compliant version, which will seek the offset for
  159. * an open file descriptor.
  160. *
  161. * @param fd the file descriptor.
  162. * @param offset the offset to be seeked.
  163. * @param whence the directory of seek.
  164. *
  165. * @return the current read/write position in the file, or -1 on failed.
  166. */
  167. off_t lseek(int fd, off_t offset, int whence)
  168. {
  169. int result;
  170. struct dfs_fd *d;
  171. d = fd_get(fd);
  172. if (d == NULL)
  173. {
  174. rt_set_errno(-EBADF);
  175. return -1;
  176. }
  177. switch (whence)
  178. {
  179. case SEEK_SET:
  180. break;
  181. case SEEK_CUR:
  182. offset += d->pos;
  183. break;
  184. case SEEK_END:
  185. offset += d->size;
  186. break;
  187. default:
  188. fd_put(d);
  189. rt_set_errno(-EINVAL);
  190. return -1;
  191. }
  192. if (offset < 0)
  193. {
  194. fd_put(d);
  195. rt_set_errno(-EINVAL);
  196. return -1;
  197. }
  198. result = dfs_file_lseek(d, offset);
  199. if (result < 0)
  200. {
  201. fd_put(d);
  202. rt_set_errno(result);
  203. return -1;
  204. }
  205. /* release the ref-count of fd */
  206. fd_put(d);
  207. return offset;
  208. }
  209. RTM_EXPORT(lseek);
  210. /**
  211. * this function is a POSIX compliant version, which will rename old file name
  212. * to new file name.
  213. *
  214. * @param old the old file name.
  215. * @param new the new file name.
  216. *
  217. * @return 0 on successful, -1 on failed.
  218. *
  219. * note: the old and new file name must be belong to a same file system.
  220. */
  221. int rename(const char *old_file, const char *new_file)
  222. {
  223. int result;
  224. result = dfs_file_rename(old_file, new_file);
  225. if (result < 0)
  226. {
  227. rt_set_errno(result);
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. RTM_EXPORT(rename);
  233. /**
  234. * this function is a POSIX compliant version, which will unlink (remove) a
  235. * specified path file from file system.
  236. *
  237. * @param pathname the specified path name to be unlinked.
  238. *
  239. * @return 0 on successful, -1 on failed.
  240. */
  241. int unlink(const char *pathname)
  242. {
  243. int result;
  244. result = dfs_file_unlink(pathname);
  245. if (result < 0)
  246. {
  247. rt_set_errno(result);
  248. return -1;
  249. }
  250. return 0;
  251. }
  252. RTM_EXPORT(unlink);
  253. #ifndef _WIN32 /* we can not implement these functions */
  254. /**
  255. * this function is a POSIX compliant version, which will get file information.
  256. *
  257. * @param file the file name
  258. * @param buf the data buffer to save stat description.
  259. *
  260. * @return 0 on successful, -1 on failed.
  261. */
  262. int stat(const char *file, struct stat *buf)
  263. {
  264. int result;
  265. result = dfs_file_stat(file, buf);
  266. if (result < 0)
  267. {
  268. rt_set_errno(result);
  269. return -1;
  270. }
  271. return result;
  272. }
  273. RTM_EXPORT(stat);
  274. /**
  275. * this function is a POSIX compliant version, which will get file status.
  276. *
  277. * @param fildes the file description
  278. * @param buf the data buffer to save stat description.
  279. *
  280. * @return 0 on successful, -1 on failed.
  281. */
  282. int fstat(int fildes, struct stat *buf)
  283. {
  284. struct dfs_fd *d;
  285. /* get the fd */
  286. d = fd_get(fildes);
  287. if (d == NULL)
  288. {
  289. rt_set_errno(-EBADF);
  290. return -1;
  291. }
  292. /* it's the root directory */
  293. buf->st_dev = 0;
  294. buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  295. S_IWUSR | S_IWGRP | S_IWOTH;
  296. if (d->type == FT_DIRECTORY)
  297. {
  298. buf->st_mode &= ~S_IFREG;
  299. buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  300. }
  301. buf->st_size = d->size;
  302. buf->st_mtime = 0;
  303. fd_put(d);
  304. return RT_EOK;
  305. }
  306. RTM_EXPORT(fstat);
  307. #endif
  308. /**
  309. * this function is a POSIX compliant version, which shall request that all data
  310. * for the open file descriptor named by fildes is to be transferred to the storage
  311. * device associated with the file described by fildes.
  312. *
  313. * @param fildes the file description
  314. *
  315. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  316. * set to indicate the error.
  317. */
  318. int fsync(int fildes)
  319. {
  320. int ret;
  321. struct dfs_fd *d;
  322. /* get the fd */
  323. d = fd_get(fildes);
  324. if (d == NULL)
  325. {
  326. rt_set_errno(-EBADF);
  327. return -1;
  328. }
  329. ret = dfs_file_flush(d);
  330. fd_put(d);
  331. return ret;
  332. }
  333. RTM_EXPORT(fsync);
  334. /**
  335. * this function is a POSIX compliant version, which shall perform a variety of
  336. * control functions on devices.
  337. *
  338. * @param fildes the file description
  339. * @param cmd the specified command
  340. * @param data represents the additional information that is needed by this
  341. * specific device to perform the requested function.
  342. *
  343. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  344. * set to indicate the error.
  345. */
  346. int fcntl(int fildes, int cmd, ...)
  347. {
  348. int ret = -1;
  349. struct dfs_fd *d;
  350. /* get the fd */
  351. d = fd_get(fildes);
  352. if (d)
  353. {
  354. void *arg;
  355. va_list ap;
  356. va_start(ap, cmd);
  357. arg = va_arg(ap, void *);
  358. va_end(ap);
  359. ret = dfs_file_ioctl(d, cmd, arg);
  360. fd_put(d);
  361. }
  362. else ret = -EBADF;
  363. if (ret < 0)
  364. {
  365. rt_set_errno(ret);
  366. ret = -1;
  367. }
  368. return ret;
  369. }
  370. RTM_EXPORT(fcntl);
  371. /**
  372. * this function is a POSIX compliant version, which shall perform a variety of
  373. * control functions on devices.
  374. *
  375. * @param fildes the file description
  376. * @param cmd the specified command
  377. * @param data represents the additional information that is needed by this
  378. * specific device to perform the requested function.
  379. *
  380. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  381. * set to indicate the error.
  382. */
  383. int ioctl(int fildes, int cmd, ...)
  384. {
  385. void *arg;
  386. va_list ap;
  387. va_start(ap, cmd);
  388. arg = va_arg(ap, void *);
  389. va_end(ap);
  390. /* we use fcntl for this API. */
  391. return fcntl(fildes, cmd, arg);
  392. }
  393. RTM_EXPORT(ioctl);
  394. /**
  395. * this function is a POSIX compliant version, which will return the
  396. * information about a mounted file system.
  397. *
  398. * @param path the path which mounted file system.
  399. * @param buf the buffer to save the returned information.
  400. *
  401. * @return 0 on successful, others on failed.
  402. */
  403. int statfs(const char *path, struct statfs *buf)
  404. {
  405. int result;
  406. result = dfs_statfs(path, buf);
  407. if (result < 0)
  408. {
  409. rt_set_errno(result);
  410. return -1;
  411. }
  412. return result;
  413. }
  414. RTM_EXPORT(statfs);
  415. /**
  416. * this function is a POSIX compliant version, which will make a directory
  417. *
  418. * @param path the directory path to be made.
  419. * @param mode
  420. *
  421. * @return 0 on successful, others on failed.
  422. */
  423. int mkdir(const char *path, mode_t mode)
  424. {
  425. int fd;
  426. struct dfs_fd *d;
  427. int result;
  428. fd = fd_new();
  429. if (fd == -1)
  430. {
  431. rt_set_errno(-ENOMEM);
  432. return -1;
  433. }
  434. d = fd_get(fd);
  435. result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
  436. if (result < 0)
  437. {
  438. fd_put(d);
  439. fd_put(d);
  440. rt_set_errno(result);
  441. return -1;
  442. }
  443. dfs_file_close(d);
  444. fd_put(d);
  445. fd_put(d);
  446. return 0;
  447. }
  448. RTM_EXPORT(mkdir);
  449. #ifdef RT_USING_FINSH
  450. #include <finsh.h>
  451. FINSH_FUNCTION_EXPORT(mkdir, create a directory);
  452. #endif
  453. /**
  454. * this function is a POSIX compliant version, which will remove a directory.
  455. *
  456. * @param pathname the path name to be removed.
  457. *
  458. * @return 0 on successful, others on failed.
  459. */
  460. int rmdir(const char *pathname)
  461. {
  462. int result;
  463. result = dfs_file_unlink(pathname);
  464. if (result < 0)
  465. {
  466. rt_set_errno(result);
  467. return -1;
  468. }
  469. return 0;
  470. }
  471. RTM_EXPORT(rmdir);
  472. /**
  473. * this function is a POSIX compliant version, which will open a directory.
  474. *
  475. * @param name the path name to be open.
  476. *
  477. * @return the DIR pointer of directory, NULL on open directory failed.
  478. */
  479. DIR *opendir(const char *name)
  480. {
  481. struct dfs_fd *d;
  482. int fd, result;
  483. DIR *t;
  484. t = NULL;
  485. /* allocate a fd */
  486. fd = fd_new();
  487. if (fd == -1)
  488. {
  489. rt_set_errno(-ENOMEM);
  490. return NULL;
  491. }
  492. d = fd_get(fd);
  493. result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
  494. if (result >= 0)
  495. {
  496. /* open successfully */
  497. t = (DIR *) rt_malloc(sizeof(DIR));
  498. if (t == NULL)
  499. {
  500. dfs_file_close(d);
  501. fd_put(d);
  502. }
  503. else
  504. {
  505. memset(t, 0, sizeof(DIR));
  506. t->fd = fd;
  507. }
  508. fd_put(d);
  509. return t;
  510. }
  511. /* open failed */
  512. fd_put(d);
  513. fd_put(d);
  514. rt_set_errno(result);
  515. return NULL;
  516. }
  517. RTM_EXPORT(opendir);
  518. /**
  519. * this function is a POSIX compliant version, which will return a pointer
  520. * to a dirent structure representing the next directory entry in the
  521. * directory stream.
  522. *
  523. * @param d the directory stream pointer.
  524. *
  525. * @return the next directory entry, NULL on the end of directory or failed.
  526. */
  527. struct dirent *readdir(DIR *d)
  528. {
  529. int result;
  530. struct dfs_fd *fd;
  531. fd = fd_get(d->fd);
  532. if (fd == NULL)
  533. {
  534. rt_set_errno(-EBADF);
  535. return NULL;
  536. }
  537. if (d->num)
  538. {
  539. struct dirent *dirent_ptr;
  540. dirent_ptr = (struct dirent *)&d->buf[d->cur];
  541. d->cur += dirent_ptr->d_reclen;
  542. }
  543. if (!d->num || d->cur >= d->num)
  544. {
  545. /* get a new entry */
  546. result = dfs_file_getdents(fd,
  547. (struct dirent *)d->buf,
  548. sizeof(d->buf) - 1);
  549. if (result <= 0)
  550. {
  551. fd_put(fd);
  552. rt_set_errno(result);
  553. return NULL;
  554. }
  555. d->num = result;
  556. d->cur = 0; /* current entry index */
  557. }
  558. fd_put(fd);
  559. return (struct dirent *)(d->buf + d->cur);
  560. }
  561. RTM_EXPORT(readdir);
  562. /**
  563. * this function is a POSIX compliant version, which will return current
  564. * location in directory stream.
  565. *
  566. * @param d the directory stream pointer.
  567. *
  568. * @return the current location in directory stream.
  569. */
  570. long telldir(DIR *d)
  571. {
  572. struct dfs_fd *fd;
  573. long result;
  574. fd = fd_get(d->fd);
  575. if (fd == NULL)
  576. {
  577. rt_set_errno(-EBADF);
  578. return 0;
  579. }
  580. result = fd->pos - d->num + d->cur;
  581. fd_put(fd);
  582. return result;
  583. }
  584. RTM_EXPORT(telldir);
  585. /**
  586. * this function is a POSIX compliant version, which will set position of
  587. * next directory structure in the directory stream.
  588. *
  589. * @param d the directory stream.
  590. * @param offset the offset in directory stream.
  591. */
  592. void seekdir(DIR *d, off_t offset)
  593. {
  594. struct dfs_fd *fd;
  595. fd = fd_get(d->fd);
  596. if (fd == NULL)
  597. {
  598. rt_set_errno(-EBADF);
  599. return ;
  600. }
  601. /* seek to the offset position of directory */
  602. if (dfs_file_lseek(fd, offset) >= 0)
  603. d->num = d->cur = 0;
  604. fd_put(fd);
  605. }
  606. RTM_EXPORT(seekdir);
  607. /**
  608. * this function is a POSIX compliant version, which will reset directory
  609. * stream.
  610. *
  611. * @param d the directory stream.
  612. */
  613. void rewinddir(DIR *d)
  614. {
  615. struct dfs_fd *fd;
  616. fd = fd_get(d->fd);
  617. if (fd == NULL)
  618. {
  619. rt_set_errno(-EBADF);
  620. return ;
  621. }
  622. /* seek to the beginning of directory */
  623. if (dfs_file_lseek(fd, 0) >= 0)
  624. d->num = d->cur = 0;
  625. fd_put(fd);
  626. }
  627. RTM_EXPORT(rewinddir);
  628. /**
  629. * this function is a POSIX compliant version, which will close a directory
  630. * stream.
  631. *
  632. * @param d the directory stream.
  633. *
  634. * @return 0 on successful, -1 on failed.
  635. */
  636. int closedir(DIR *d)
  637. {
  638. int result;
  639. struct dfs_fd *fd;
  640. fd = fd_get(d->fd);
  641. if (fd == NULL)
  642. {
  643. rt_set_errno(-EBADF);
  644. return -1;
  645. }
  646. result = dfs_file_close(fd);
  647. fd_put(fd);
  648. fd_put(fd);
  649. rt_free(d);
  650. if (result < 0)
  651. {
  652. rt_set_errno(result);
  653. return -1;
  654. }
  655. else
  656. return 0;
  657. }
  658. RTM_EXPORT(closedir);
  659. #ifdef DFS_USING_WORKDIR
  660. /**
  661. * this function is a POSIX compliant version, which will change working
  662. * directory.
  663. *
  664. * @param path the path name to be changed to.
  665. *
  666. * @return 0 on successful, -1 on failed.
  667. */
  668. int chdir(const char *path)
  669. {
  670. char *fullpath;
  671. DIR *d;
  672. if (path == NULL)
  673. {
  674. dfs_lock();
  675. rt_kprintf("%s\n", working_directory);
  676. dfs_unlock();
  677. return 0;
  678. }
  679. if (strlen(path) > DFS_PATH_MAX)
  680. {
  681. rt_set_errno(-ENOTDIR);
  682. return -1;
  683. }
  684. fullpath = dfs_normalize_path(NULL, path);
  685. if (fullpath == NULL)
  686. {
  687. rt_set_errno(-ENOTDIR);
  688. return -1; /* build path failed */
  689. }
  690. dfs_lock();
  691. d = opendir(fullpath);
  692. if (d == NULL)
  693. {
  694. rt_free(fullpath);
  695. /* this is a not exist directory */
  696. dfs_unlock();
  697. return -1;
  698. }
  699. /* close directory stream */
  700. closedir(d);
  701. /* copy full path to working directory */
  702. strncpy(working_directory, fullpath, DFS_PATH_MAX);
  703. /* release normalize directory path name */
  704. rt_free(fullpath);
  705. dfs_unlock();
  706. return 0;
  707. }
  708. RTM_EXPORT(chdir);
  709. #ifdef RT_USING_FINSH
  710. FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
  711. #endif
  712. #endif
  713. /**
  714. * this function is a POSIX compliant version, which shall check the file named
  715. * by the pathname pointed to by the path argument for accessibility according
  716. * to the bit pattern contained in amode.
  717. *
  718. * @param path the specified file/dir path.
  719. * @param amode the value is either the bitwise-inclusive OR of the access
  720. * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).
  721. */
  722. int access(const char *path, int amode)
  723. {
  724. struct stat sb;
  725. if (stat(path, &sb) < 0)
  726. return -1; /* already sets errno */
  727. /* ignore R_OK,W_OK,X_OK condition */
  728. return 0;
  729. }
  730. /**
  731. * this function is a POSIX compliant version, which will return current
  732. * working directory.
  733. *
  734. * @param buf the returned current directory.
  735. * @param size the buffer size.
  736. *
  737. * @return the returned current directory.
  738. */
  739. char *getcwd(char *buf, size_t size)
  740. {
  741. #ifdef DFS_USING_WORKDIR
  742. dfs_lock();
  743. strncpy(buf, working_directory, size);
  744. dfs_unlock();
  745. #else
  746. rt_kprintf(NO_WORKING_DIR);
  747. #endif
  748. return buf;
  749. }
  750. RTM_EXPORT(getcwd);
  751. /* @} */