dfs_posix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * File : dfs_posix.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-05-27 Yi.qiu The first version
  23. */
  24. #include <dfs.h>
  25. #include <dfs_posix.h>
  26. /**
  27. * @addtogroup FsPosixApi
  28. */
  29. /*@{*/
  30. /**
  31. * this function is a POSIX compliant version, which will open a file and
  32. * return a file descriptor according specified flags.
  33. *
  34. * @param file the path name of file.
  35. * @param flags the file open flags.
  36. * @param mode ignored parameter
  37. *
  38. * @return the non-negative integer on successful open, others for failed.
  39. */
  40. int open(const char *file, int flags, int mode)
  41. {
  42. int fd, result;
  43. struct dfs_fd *d;
  44. /* allocate a fd */
  45. fd = fd_new();
  46. if (fd < 0)
  47. {
  48. rt_set_errno(-DFS_STATUS_ENOMEM);
  49. return -1;
  50. }
  51. d = fd_get(fd);
  52. result = dfs_file_open(d, file, flags);
  53. if (result < 0)
  54. {
  55. /* release the ref-count of fd */
  56. fd_put(d);
  57. fd_put(d);
  58. rt_set_errno(result);
  59. return -1;
  60. }
  61. /* release the ref-count of fd */
  62. fd_put(d);
  63. return fd;
  64. }
  65. RTM_EXPORT(open);
  66. /**
  67. * this function is a POSIX compliant version, which will close the open
  68. * file descriptor.
  69. *
  70. * @param fd the file descriptor.
  71. *
  72. * @return 0 on successful, -1 on failed.
  73. */
  74. int close(int fd)
  75. {
  76. int result;
  77. struct dfs_fd *d;
  78. d = fd_get(fd);
  79. if (d == RT_NULL)
  80. {
  81. rt_set_errno(-DFS_STATUS_EBADF);
  82. return -1;
  83. }
  84. result = dfs_file_close(d);
  85. fd_put(d);
  86. if (result < 0)
  87. {
  88. rt_set_errno(result);
  89. return -1;
  90. }
  91. fd_put(d);
  92. return 0;
  93. }
  94. RTM_EXPORT(close);
  95. /**
  96. * this function is a POSIX compliant version, which will read specified data
  97. * buffer length for an open file descriptor.
  98. *
  99. * @param fd the file descriptor.
  100. * @param buf the buffer to save the read data.
  101. * @param len the maximal length of data buffer
  102. *
  103. * @return the actual read data buffer length. If the returned value is 0, it
  104. * may be reach the end of file, please check errno.
  105. */
  106. int read(int fd, void *buf, size_t len)
  107. {
  108. int result;
  109. struct dfs_fd *d;
  110. /* get the fd */
  111. d = fd_get(fd);
  112. if (d == RT_NULL)
  113. {
  114. rt_set_errno(-DFS_STATUS_EBADF);
  115. return -1;
  116. }
  117. result = dfs_file_read(d, buf, len);
  118. if (result < 0)
  119. {
  120. fd_put(d);
  121. rt_set_errno(result);
  122. return -1;
  123. }
  124. /* release the ref-count of fd */
  125. fd_put(d);
  126. return result;
  127. }
  128. RTM_EXPORT(read);
  129. /**
  130. * this function is a POSIX compliant version, which will write specified data
  131. * buffer length for an open file descriptor.
  132. *
  133. * @param fd the file descriptor
  134. * @param buf the data buffer to be written.
  135. * @param len the data buffer length.
  136. *
  137. * @return the actual written data buffer length.
  138. */
  139. int write(int fd, const void *buf, size_t len)
  140. {
  141. int result;
  142. struct dfs_fd *d;
  143. /* get the fd */
  144. d = fd_get(fd);
  145. if (d == RT_NULL)
  146. {
  147. rt_set_errno(-DFS_STATUS_EBADF);
  148. return -1;
  149. }
  150. result = dfs_file_write(d, buf, len);
  151. if (result < 0)
  152. {
  153. fd_put(d);
  154. rt_set_errno(result);
  155. return -1;
  156. }
  157. /* release the ref-count of fd */
  158. fd_put(d);
  159. return result;
  160. }
  161. RTM_EXPORT(write);
  162. /**
  163. * this function is a POSIX compliant version, which will seek the offset for
  164. * an open file descriptor.
  165. *
  166. * @param fd the file descriptor.
  167. * @param offset the offset to be seeked.
  168. * @param whence the directory of seek.
  169. *
  170. * @return the current read/write position in the file, or -1 on failed.
  171. */
  172. off_t lseek(int fd, off_t offset, int whence)
  173. {
  174. int result;
  175. struct dfs_fd *d;
  176. d = fd_get(fd);
  177. if (d == RT_NULL)
  178. {
  179. rt_set_errno(-DFS_STATUS_EBADF);
  180. return -1;
  181. }
  182. switch (whence)
  183. {
  184. case DFS_SEEK_SET:
  185. break;
  186. case DFS_SEEK_CUR:
  187. offset += d->pos;
  188. break;
  189. case DFS_SEEK_END:
  190. offset += d->size;
  191. break;
  192. default:
  193. fd_put(d);
  194. rt_set_errno(-DFS_STATUS_EINVAL);
  195. return -1;
  196. }
  197. if (offset < 0)
  198. {
  199. fd_put(d);
  200. rt_set_errno(-DFS_STATUS_EINVAL);
  201. return -1;
  202. }
  203. result = dfs_file_lseek(d, offset);
  204. if (result < 0)
  205. {
  206. fd_put(d);
  207. rt_set_errno(result);
  208. return -1;
  209. }
  210. /* release the ref-count of fd */
  211. fd_put(d);
  212. return offset;
  213. }
  214. RTM_EXPORT(lseek);
  215. /**
  216. * this function is a POSIX compliant version, which will rename old file name
  217. * to new file name.
  218. *
  219. * @param old the old file name.
  220. * @param new the new file name.
  221. *
  222. * @return 0 on successful, -1 on failed.
  223. *
  224. * note: the old and new file name must be belong to a same file system.
  225. */
  226. int rename(const char *old, const char *new)
  227. {
  228. int result;
  229. result = dfs_file_rename(old, new);
  230. if (result < 0)
  231. {
  232. rt_set_errno(result);
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. RTM_EXPORT(rename);
  238. /**
  239. * this function is a POSIX compliant version, which will unlink (remove) a
  240. * specified path file from file system.
  241. *
  242. * @param pathname the specified path name to be unlinked.
  243. *
  244. * @return 0 on successful, -1 on failed.
  245. */
  246. int unlink(const char *pathname)
  247. {
  248. int result;
  249. result = dfs_file_unlink(pathname);
  250. if (result < 0)
  251. {
  252. rt_set_errno(result);
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. RTM_EXPORT(unlink);
  258. /**
  259. * this function is a POSIX compliant version, which will get file information.
  260. *
  261. * @param file the file name
  262. * @param buf the data buffer to save stat description.
  263. *
  264. * @return 0 on successful, -1 on failed.
  265. */
  266. int stat(const char *file, struct stat *buf)
  267. {
  268. int result;
  269. result = dfs_file_stat(file, buf);
  270. if (result < 0)
  271. {
  272. rt_set_errno(result);
  273. return -1;
  274. }
  275. return result;
  276. }
  277. RTM_EXPORT(stat);
  278. /**
  279. * this function is a POSIX compliant version, which will get file status.
  280. *
  281. * @param fildes the file description
  282. * @param buf the data buffer to save stat description.
  283. *
  284. * @return 0 on successful, -1 on failed.
  285. */
  286. int fstat(int fildes, struct stat *buf)
  287. {
  288. struct dfs_fd *d;
  289. /* get the fd */
  290. d = fd_get(fildes);
  291. if (d == RT_NULL)
  292. {
  293. rt_set_errno(-DFS_STATUS_EBADF);
  294. return -1;
  295. }
  296. /* it's the root directory */
  297. buf->st_dev = 0;
  298. buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  299. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  300. if (d->type == FT_DIRECTORY)
  301. {
  302. buf->st_mode &= ~DFS_S_IFREG;
  303. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  304. }
  305. buf->st_size = d->size;
  306. buf->st_mtime = 0;
  307. buf->st_blksize = 512;
  308. fd_put(d);
  309. return DFS_STATUS_OK;
  310. }
  311. RTM_EXPORT(fstat);
  312. /**
  313. * this function is a POSIX compliant version, which shall request that all data
  314. * for the open file descriptor named by fildes is to be transferred to the storage
  315. * device associated with the file described by fildes.
  316. *
  317. * @param fildes the file description
  318. *
  319. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  320. * set to indicate the error.
  321. */
  322. int fsync(int fildes)
  323. {
  324. int ret;
  325. struct dfs_fd *d;
  326. /* get the fd */
  327. d = fd_get(fildes);
  328. if (d == RT_NULL)
  329. {
  330. rt_set_errno(-DFS_STATUS_EBADF);
  331. return -1;
  332. }
  333. ret = dfs_file_flush(d);
  334. fd_put(d);
  335. return ret;
  336. }
  337. RTM_EXPORT(fsync);
  338. /**
  339. * this function is a POSIX compliant version, which will return the
  340. * information about a mounted file system.
  341. *
  342. * @param path the path which mounted file system.
  343. * @param buf the buffer to save the returned information.
  344. *
  345. * @return 0 on successful, others on failed.
  346. */
  347. int statfs(const char *path, struct statfs *buf)
  348. {
  349. int result;
  350. result = dfs_statfs(path, buf);
  351. if (result < 0)
  352. {
  353. rt_set_errno(result);
  354. return -1;
  355. }
  356. return result;
  357. }
  358. RTM_EXPORT(statfs);
  359. /**
  360. * this function is a POSIX compliant version, which will make a directory
  361. *
  362. * @param path the directory path to be made.
  363. * @param mode
  364. *
  365. * @return 0 on successful, others on failed.
  366. */
  367. int mkdir(const char *path, mode_t mode)
  368. {
  369. int fd;
  370. struct dfs_fd *d;
  371. int result;
  372. fd = fd_new();
  373. if (fd == -1)
  374. {
  375. rt_set_errno(-DFS_STATUS_ENOMEM);
  376. return -1;
  377. }
  378. d = fd_get(fd);
  379. result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
  380. if (result < 0)
  381. {
  382. fd_put(d);
  383. fd_put(d);
  384. rt_set_errno(result);
  385. return -1;
  386. }
  387. dfs_file_close(d);
  388. fd_put(d);
  389. fd_put(d);
  390. return 0;
  391. }
  392. RTM_EXPORT(mkdir);
  393. #ifdef RT_USING_FINSH
  394. #include <finsh.h>
  395. FINSH_FUNCTION_EXPORT(mkdir, create a directory);
  396. #endif
  397. /**
  398. * this function is a POSIX compliant version, which will remove a directory.
  399. *
  400. * @param pathname the path name to be removed.
  401. *
  402. * @return 0 on successful, others on failed.
  403. */
  404. int rmdir(const char *pathname)
  405. {
  406. int result;
  407. result = dfs_file_unlink(pathname);
  408. if (result < 0)
  409. {
  410. rt_set_errno(result);
  411. return -1;
  412. }
  413. return 0;
  414. }
  415. RTM_EXPORT(rmdir);
  416. /**
  417. * this function is a POSIX compliant version, which will open a directory.
  418. *
  419. * @param name the path name to be open.
  420. *
  421. * @return the DIR pointer of directory, NULL on open directory failed.
  422. */
  423. DIR *opendir(const char *name)
  424. {
  425. struct dfs_fd *d;
  426. int fd, result;
  427. DIR *t;
  428. t = RT_NULL;
  429. /* allocate a fd */
  430. fd = fd_new();
  431. if (fd == -1)
  432. {
  433. rt_set_errno(-DFS_STATUS_ENOMEM);
  434. return RT_NULL;
  435. }
  436. d = fd_get(fd);
  437. result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
  438. if (result >= 0)
  439. {
  440. /* open successfully */
  441. t = (DIR *) rt_malloc(sizeof(DIR));
  442. if (t == RT_NULL)
  443. {
  444. dfs_file_close(d);
  445. fd_put(d);
  446. }
  447. else
  448. {
  449. rt_memset(t, 0, sizeof(DIR));
  450. t->fd = fd;
  451. }
  452. fd_put(d);
  453. return t;
  454. }
  455. /* open failed */
  456. fd_put(d);
  457. fd_put(d);
  458. rt_set_errno(result);
  459. return RT_NULL;
  460. }
  461. RTM_EXPORT(opendir);
  462. /**
  463. * this function is a POSIX compliant version, which will return a pointer
  464. * to a dirent structure representing the next directory entry in the
  465. * directory stream.
  466. *
  467. * @param d the directory stream pointer.
  468. *
  469. * @return the next directory entry, NULL on the end of directory or failed.
  470. */
  471. struct dirent *readdir(DIR *d)
  472. {
  473. int result;
  474. struct dfs_fd *fd;
  475. fd = fd_get(d->fd);
  476. if (fd == RT_NULL)
  477. {
  478. rt_set_errno(-DFS_STATUS_EBADF);
  479. return RT_NULL;
  480. }
  481. if (d->num)
  482. {
  483. struct dirent* dirent_ptr;
  484. dirent_ptr = (struct dirent*)&d->buf[d->cur];
  485. d->cur += dirent_ptr->d_reclen;
  486. }
  487. if (!d->num || d->cur >= d->num)
  488. {
  489. /* get a new entry */
  490. result = dfs_file_getdents(fd,
  491. (struct dirent*)d->buf,
  492. sizeof(d->buf) - 1);
  493. if (result <= 0)
  494. {
  495. fd_put(fd);
  496. rt_set_errno(result);
  497. return RT_NULL;
  498. }
  499. d->num = result;
  500. d->cur = 0; /* current entry index */
  501. }
  502. fd_put(fd);
  503. return (struct dirent *)(d->buf+d->cur);
  504. }
  505. RTM_EXPORT(readdir);
  506. /**
  507. * this function is a POSIX compliant version, which will return current
  508. * location in directory stream.
  509. *
  510. * @param d the directory stream pointer.
  511. *
  512. * @return the current location in directory stream.
  513. */
  514. long telldir(DIR *d)
  515. {
  516. struct dfs_fd *fd;
  517. long result;
  518. fd = fd_get(d->fd);
  519. if (fd == RT_NULL)
  520. {
  521. rt_set_errno(-DFS_STATUS_EBADF);
  522. return 0;
  523. }
  524. result = fd->pos - d->num + d->cur;
  525. fd_put(fd);
  526. return result;
  527. }
  528. RTM_EXPORT(telldir);
  529. /**
  530. * this function is a POSIX compliant version, which will set position of
  531. * next directory structure in the directory stream.
  532. *
  533. * @param d the directory stream.
  534. * @param offset the offset in directory stream.
  535. */
  536. void seekdir(DIR *d, off_t offset)
  537. {
  538. struct dfs_fd *fd;
  539. fd = fd_get(d->fd);
  540. if (fd == RT_NULL)
  541. {
  542. rt_set_errno(-DFS_STATUS_EBADF);
  543. return ;
  544. }
  545. /* seek to the offset position of directory */
  546. if (dfs_file_lseek(fd, offset) >= 0)
  547. d->num = d->cur = 0;
  548. fd_put(fd);
  549. }
  550. RTM_EXPORT(seekdir);
  551. /**
  552. * this function is a POSIX compliant version, which will reset directory
  553. * stream.
  554. *
  555. * @param d the directory stream.
  556. */
  557. void rewinddir(DIR *d)
  558. {
  559. struct dfs_fd *fd;
  560. fd = fd_get(d->fd);
  561. if (fd == RT_NULL)
  562. {
  563. rt_set_errno(-DFS_STATUS_EBADF);
  564. return ;
  565. }
  566. /* seek to the beginning of directory */
  567. if (dfs_file_lseek(fd, 0) >= 0)
  568. d->num = d->cur = 0;
  569. fd_put(fd);
  570. }
  571. RTM_EXPORT(rewinddir);
  572. /**
  573. * this function is a POSIX compliant version, which will close a directory
  574. * stream.
  575. *
  576. * @param d the directory stream.
  577. *
  578. * @return 0 on successful, -1 on failed.
  579. */
  580. int closedir(DIR *d)
  581. {
  582. int result;
  583. struct dfs_fd *fd;
  584. fd = fd_get(d->fd);
  585. if (fd == RT_NULL)
  586. {
  587. rt_set_errno(-DFS_STATUS_EBADF);
  588. return -1;
  589. }
  590. result = dfs_file_close(fd);
  591. fd_put(fd);
  592. fd_put(fd);
  593. rt_free(d);
  594. if (result < 0)
  595. {
  596. rt_set_errno(result);
  597. return -1;
  598. }
  599. else
  600. return 0;
  601. }
  602. RTM_EXPORT(closedir);
  603. #ifdef DFS_USING_WORKDIR
  604. /**
  605. * this function is a POSIX compliant version, which will change working
  606. * directory.
  607. *
  608. * @param path the path name to be changed to.
  609. *
  610. * @return 0 on successful, -1 on failed.
  611. */
  612. int chdir(const char *path)
  613. {
  614. char *fullpath;
  615. DIR *d;
  616. if (path == RT_NULL)
  617. {
  618. dfs_lock();
  619. rt_kprintf("%s\n", working_directory);
  620. dfs_unlock();
  621. return 0;
  622. }
  623. if (rt_strlen(path) > DFS_PATH_MAX)
  624. {
  625. rt_set_errno(-DFS_STATUS_ENOTDIR);
  626. return -1;
  627. }
  628. fullpath = dfs_normalize_path(NULL, path);
  629. if (fullpath == RT_NULL)
  630. {
  631. rt_set_errno(-DFS_STATUS_ENOTDIR);
  632. return -1; /* build path failed */
  633. }
  634. dfs_lock();
  635. d = opendir(fullpath);
  636. if (d == RT_NULL)
  637. {
  638. rt_free(fullpath);
  639. /* this is a not exist directory */
  640. dfs_unlock();
  641. return -1;
  642. }
  643. /* close directory stream */
  644. closedir(d);
  645. /* copy full path to working directory */
  646. strncpy(working_directory, fullpath, DFS_PATH_MAX);
  647. /* release normalize directory path name */
  648. rt_free(fullpath);
  649. dfs_unlock();
  650. return 0;
  651. }
  652. RTM_EXPORT(chdir);
  653. #ifdef RT_USING_FINSH
  654. FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
  655. #endif
  656. #endif
  657. /**
  658. * this function is a POSIX compliant version, which will return current
  659. * working directory.
  660. *
  661. * @param buf the returned current directory.
  662. * @param size the buffer size.
  663. *
  664. * @return the returned current directory.
  665. */
  666. char *getcwd(char *buf, size_t size)
  667. {
  668. #ifdef DFS_USING_WORKDIR
  669. rt_enter_critical();
  670. rt_strncpy(buf, working_directory, size);
  671. rt_exit_critical();
  672. #else
  673. rt_kprintf(NO_WORKING_DIR);
  674. #endif
  675. return buf;
  676. }
  677. RTM_EXPORT(getcwd);
  678. /* @} */