dfs_posix.c 13 KB

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