dfs_posix.c 13 KB

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