dfs_posix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. #ifdef RT_USING_NEWLIB
  107. _READ_WRITE_RETURN_TYPE _EXFUN(read, (int fd, void *buf, size_t len))
  108. #else
  109. int read(int fd, void *buf, size_t len)
  110. #endif
  111. {
  112. int result;
  113. struct dfs_fd *d;
  114. /* get the fd */
  115. d = fd_get(fd);
  116. if (d == RT_NULL)
  117. {
  118. rt_set_errno(-DFS_STATUS_EBADF);
  119. return -1;
  120. }
  121. result = dfs_file_read(d, buf, len);
  122. if (result < 0)
  123. {
  124. fd_put(d);
  125. rt_set_errno(result);
  126. return -1;
  127. }
  128. /* release the ref-count of fd */
  129. fd_put(d);
  130. return result;
  131. }
  132. RTM_EXPORT(read);
  133. /**
  134. * this function is a POSIX compliant version, which will write specified data
  135. * buffer length for an open file descriptor.
  136. *
  137. * @param fd the file descriptor
  138. * @param buf the data buffer to be written.
  139. * @param len the data buffer length.
  140. *
  141. * @return the actual written data buffer length.
  142. */
  143. #ifdef RT_USING_NEWLIB
  144. _READ_WRITE_RETURN_TYPE _EXFUN(write, (int fd, const void *buf, size_t len))
  145. #else
  146. int write(int fd, const void *buf, size_t len)
  147. #endif
  148. {
  149. int result;
  150. struct dfs_fd *d;
  151. /* get the fd */
  152. d = fd_get(fd);
  153. if (d == RT_NULL)
  154. {
  155. rt_set_errno(-DFS_STATUS_EBADF);
  156. return -1;
  157. }
  158. result = dfs_file_write(d, buf, len);
  159. if (result < 0)
  160. {
  161. fd_put(d);
  162. rt_set_errno(result);
  163. return -1;
  164. }
  165. /* release the ref-count of fd */
  166. fd_put(d);
  167. return result;
  168. }
  169. RTM_EXPORT(write);
  170. /**
  171. * this function is a POSIX compliant version, which will seek the offset for
  172. * an open file descriptor.
  173. *
  174. * @param fd the file descriptor.
  175. * @param offset the offset to be seeked.
  176. * @param whence the directory of seek.
  177. *
  178. * @return the current read/write position in the file, or -1 on failed.
  179. */
  180. off_t lseek(int fd, off_t offset, int whence)
  181. {
  182. int result;
  183. struct dfs_fd *d;
  184. d = fd_get(fd);
  185. if (d == RT_NULL)
  186. {
  187. rt_set_errno(-DFS_STATUS_EBADF);
  188. return -1;
  189. }
  190. switch (whence)
  191. {
  192. case DFS_SEEK_SET:
  193. break;
  194. case DFS_SEEK_CUR:
  195. offset += d->pos;
  196. break;
  197. case DFS_SEEK_END:
  198. offset += d->size;
  199. break;
  200. default:
  201. fd_put(d);
  202. rt_set_errno(-DFS_STATUS_EINVAL);
  203. return -1;
  204. }
  205. if (offset < 0)
  206. {
  207. fd_put(d);
  208. rt_set_errno(-DFS_STATUS_EINVAL);
  209. return -1;
  210. }
  211. result = dfs_file_lseek(d, offset);
  212. if (result < 0)
  213. {
  214. fd_put(d);
  215. rt_set_errno(result);
  216. return -1;
  217. }
  218. /* release the ref-count of fd */
  219. fd_put(d);
  220. return offset;
  221. }
  222. RTM_EXPORT(lseek);
  223. /**
  224. * this function is a POSIX compliant version, which will rename old file name
  225. * to new file name.
  226. *
  227. * @param old the old file name.
  228. * @param new the new file name.
  229. *
  230. * @return 0 on successful, -1 on failed.
  231. *
  232. * note: the old and new file name must be belong to a same file system.
  233. */
  234. int rename(const char *old, const char *new)
  235. {
  236. int result;
  237. result = dfs_file_rename(old, new);
  238. if (result < 0)
  239. {
  240. rt_set_errno(result);
  241. return -1;
  242. }
  243. return 0;
  244. }
  245. RTM_EXPORT(rename);
  246. /**
  247. * this function is a POSIX compliant version, which will unlink (remove) a
  248. * specified path file from file system.
  249. *
  250. * @param pathname the specified path name to be unlinked.
  251. *
  252. * @return 0 on successful, -1 on failed.
  253. */
  254. int unlink(const char *pathname)
  255. {
  256. int result;
  257. result = dfs_file_unlink(pathname);
  258. if (result < 0)
  259. {
  260. rt_set_errno(result);
  261. return -1;
  262. }
  263. return 0;
  264. }
  265. RTM_EXPORT(unlink);
  266. #ifndef _WIN32 /* we can not implement these functions */
  267. /**
  268. * this function is a POSIX compliant version, which will get file information.
  269. *
  270. * @param file the file name
  271. * @param buf the data buffer to save stat description.
  272. *
  273. * @return 0 on successful, -1 on failed.
  274. */
  275. int stat(const char *file, struct stat *buf)
  276. {
  277. int result;
  278. result = dfs_file_stat(file, buf);
  279. if (result < 0)
  280. {
  281. rt_set_errno(result);
  282. return -1;
  283. }
  284. return result;
  285. }
  286. RTM_EXPORT(stat);
  287. /**
  288. * this function is a POSIX compliant version, which will get file status.
  289. *
  290. * @param fildes the file description
  291. * @param buf the data buffer to save stat description.
  292. *
  293. * @return 0 on successful, -1 on failed.
  294. */
  295. int fstat(int fildes, struct stat *buf)
  296. {
  297. struct dfs_fd *d;
  298. /* get the fd */
  299. d = fd_get(fildes);
  300. if (d == RT_NULL)
  301. {
  302. rt_set_errno(-DFS_STATUS_EBADF);
  303. return -1;
  304. }
  305. /* it's the root directory */
  306. buf->st_dev = 0;
  307. buf->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  308. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  309. if (d->type == FT_DIRECTORY)
  310. {
  311. buf->st_mode &= ~DFS_S_IFREG;
  312. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  313. }
  314. buf->st_size = d->size;
  315. buf->st_mtime = 0;
  316. fd_put(d);
  317. return DFS_STATUS_OK;
  318. }
  319. RTM_EXPORT(fstat);
  320. #endif
  321. /**
  322. * this function is a POSIX compliant version, which shall request that all data
  323. * for the open file descriptor named by fildes is to be transferred to the storage
  324. * device associated with the file described by fildes.
  325. *
  326. * @param fildes the file description
  327. *
  328. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  329. * set to indicate the error.
  330. */
  331. int fsync(int fildes)
  332. {
  333. int ret;
  334. struct dfs_fd *d;
  335. /* get the fd */
  336. d = fd_get(fildes);
  337. if (d == RT_NULL)
  338. {
  339. rt_set_errno(-DFS_STATUS_EBADF);
  340. return -1;
  341. }
  342. ret = dfs_file_flush(d);
  343. fd_put(d);
  344. return ret;
  345. }
  346. RTM_EXPORT(fsync);
  347. /**
  348. * this function is a POSIX compliant version, which shall perform a variety of
  349. * control functions on devices.
  350. *
  351. * @param fildes the file description
  352. * @param cmd the specified command
  353. * @param data represents the additional information that is needed by this
  354. * specific device to perform the requested function.
  355. *
  356. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  357. * set to indicate the error.
  358. */
  359. int ioctl(int fildes, unsigned long cmd, void *data)
  360. {
  361. int ret;
  362. struct dfs_fd *d;
  363. /* get the fd */
  364. d = fd_get(fildes);
  365. if (d == RT_NULL)
  366. {
  367. rt_set_errno(-DFS_STATUS_EBADF);
  368. return -1;
  369. }
  370. ret = dfs_file_ioctl(d, cmd, data);
  371. if (ret != DFS_STATUS_OK)
  372. {
  373. rt_set_errno(ret);
  374. ret = -1;
  375. }
  376. fd_put(d);
  377. return ret;
  378. }
  379. RTM_EXPORT(ioctl);
  380. /**
  381. * this function is a POSIX compliant version, which will return the
  382. * information about a mounted file system.
  383. *
  384. * @param path the path which mounted file system.
  385. * @param buf the buffer to save the returned information.
  386. *
  387. * @return 0 on successful, others on failed.
  388. */
  389. int statfs(const char *path, struct statfs *buf)
  390. {
  391. int result;
  392. result = dfs_statfs(path, buf);
  393. if (result < 0)
  394. {
  395. rt_set_errno(result);
  396. return -1;
  397. }
  398. return result;
  399. }
  400. RTM_EXPORT(statfs);
  401. /**
  402. * this function is a POSIX compliant version, which will make a directory
  403. *
  404. * @param path the directory path to be made.
  405. * @param mode
  406. *
  407. * @return 0 on successful, others on failed.
  408. */
  409. int mkdir(const char *path, mode_t mode)
  410. {
  411. int fd;
  412. struct dfs_fd *d;
  413. int result;
  414. fd = fd_new();
  415. if (fd == -1)
  416. {
  417. rt_set_errno(-DFS_STATUS_ENOMEM);
  418. return -1;
  419. }
  420. d = fd_get(fd);
  421. result = dfs_file_open(d, path, DFS_O_DIRECTORY | DFS_O_CREAT);
  422. if (result < 0)
  423. {
  424. fd_put(d);
  425. fd_put(d);
  426. rt_set_errno(result);
  427. return -1;
  428. }
  429. dfs_file_close(d);
  430. fd_put(d);
  431. fd_put(d);
  432. return 0;
  433. }
  434. RTM_EXPORT(mkdir);
  435. #ifdef RT_USING_FINSH
  436. #include <finsh.h>
  437. FINSH_FUNCTION_EXPORT(mkdir, create a directory);
  438. #endif
  439. /**
  440. * this function is a POSIX compliant version, which will remove a directory.
  441. *
  442. * @param pathname the path name to be removed.
  443. *
  444. * @return 0 on successful, others on failed.
  445. */
  446. int rmdir(const char *pathname)
  447. {
  448. int result;
  449. result = dfs_file_unlink(pathname);
  450. if (result < 0)
  451. {
  452. rt_set_errno(result);
  453. return -1;
  454. }
  455. return 0;
  456. }
  457. RTM_EXPORT(rmdir);
  458. /**
  459. * this function is a POSIX compliant version, which will open a directory.
  460. *
  461. * @param name the path name to be open.
  462. *
  463. * @return the DIR pointer of directory, NULL on open directory failed.
  464. */
  465. DIR *opendir(const char *name)
  466. {
  467. struct dfs_fd *d;
  468. int fd, result;
  469. DIR *t;
  470. t = RT_NULL;
  471. /* allocate a fd */
  472. fd = fd_new();
  473. if (fd == -1)
  474. {
  475. rt_set_errno(-DFS_STATUS_ENOMEM);
  476. return RT_NULL;
  477. }
  478. d = fd_get(fd);
  479. result = dfs_file_open(d, name, DFS_O_RDONLY | DFS_O_DIRECTORY);
  480. if (result >= 0)
  481. {
  482. /* open successfully */
  483. t = (DIR *) rt_malloc(sizeof(DIR));
  484. if (t == RT_NULL)
  485. {
  486. dfs_file_close(d);
  487. fd_put(d);
  488. }
  489. else
  490. {
  491. rt_memset(t, 0, sizeof(DIR));
  492. t->fd = fd;
  493. }
  494. fd_put(d);
  495. return t;
  496. }
  497. /* open failed */
  498. fd_put(d);
  499. fd_put(d);
  500. rt_set_errno(result);
  501. return RT_NULL;
  502. }
  503. RTM_EXPORT(opendir);
  504. /**
  505. * this function is a POSIX compliant version, which will return a pointer
  506. * to a dirent structure representing the next directory entry in the
  507. * directory stream.
  508. *
  509. * @param d the directory stream pointer.
  510. *
  511. * @return the next directory entry, NULL on the end of directory or failed.
  512. */
  513. struct dirent *readdir(DIR *d)
  514. {
  515. int result;
  516. struct dfs_fd *fd;
  517. fd = fd_get(d->fd);
  518. if (fd == RT_NULL)
  519. {
  520. rt_set_errno(-DFS_STATUS_EBADF);
  521. return RT_NULL;
  522. }
  523. if (d->num)
  524. {
  525. struct dirent* dirent_ptr;
  526. dirent_ptr = (struct dirent*)&d->buf[d->cur];
  527. d->cur += dirent_ptr->d_reclen;
  528. }
  529. if (!d->num || d->cur >= d->num)
  530. {
  531. /* get a new entry */
  532. result = dfs_file_getdents(fd,
  533. (struct dirent*)d->buf,
  534. sizeof(d->buf) - 1);
  535. if (result <= 0)
  536. {
  537. fd_put(fd);
  538. rt_set_errno(result);
  539. return RT_NULL;
  540. }
  541. d->num = result;
  542. d->cur = 0; /* current entry index */
  543. }
  544. fd_put(fd);
  545. return (struct dirent *)(d->buf+d->cur);
  546. }
  547. RTM_EXPORT(readdir);
  548. /**
  549. * this function is a POSIX compliant version, which will return current
  550. * location in directory stream.
  551. *
  552. * @param d the directory stream pointer.
  553. *
  554. * @return the current location in directory stream.
  555. */
  556. long telldir(DIR *d)
  557. {
  558. struct dfs_fd *fd;
  559. long result;
  560. fd = fd_get(d->fd);
  561. if (fd == RT_NULL)
  562. {
  563. rt_set_errno(-DFS_STATUS_EBADF);
  564. return 0;
  565. }
  566. result = fd->pos - d->num + d->cur;
  567. fd_put(fd);
  568. return result;
  569. }
  570. RTM_EXPORT(telldir);
  571. /**
  572. * this function is a POSIX compliant version, which will set position of
  573. * next directory structure in the directory stream.
  574. *
  575. * @param d the directory stream.
  576. * @param offset the offset in directory stream.
  577. */
  578. void seekdir(DIR *d, off_t offset)
  579. {
  580. struct dfs_fd *fd;
  581. fd = fd_get(d->fd);
  582. if (fd == RT_NULL)
  583. {
  584. rt_set_errno(-DFS_STATUS_EBADF);
  585. return ;
  586. }
  587. /* seek to the offset position of directory */
  588. if (dfs_file_lseek(fd, offset) >= 0)
  589. d->num = d->cur = 0;
  590. fd_put(fd);
  591. }
  592. RTM_EXPORT(seekdir);
  593. /**
  594. * this function is a POSIX compliant version, which will reset directory
  595. * stream.
  596. *
  597. * @param d the directory stream.
  598. */
  599. void rewinddir(DIR *d)
  600. {
  601. struct dfs_fd *fd;
  602. fd = fd_get(d->fd);
  603. if (fd == RT_NULL)
  604. {
  605. rt_set_errno(-DFS_STATUS_EBADF);
  606. return ;
  607. }
  608. /* seek to the beginning of directory */
  609. if (dfs_file_lseek(fd, 0) >= 0)
  610. d->num = d->cur = 0;
  611. fd_put(fd);
  612. }
  613. RTM_EXPORT(rewinddir);
  614. /**
  615. * this function is a POSIX compliant version, which will close a directory
  616. * stream.
  617. *
  618. * @param d the directory stream.
  619. *
  620. * @return 0 on successful, -1 on failed.
  621. */
  622. int closedir(DIR *d)
  623. {
  624. int result;
  625. struct dfs_fd *fd;
  626. fd = fd_get(d->fd);
  627. if (fd == RT_NULL)
  628. {
  629. rt_set_errno(-DFS_STATUS_EBADF);
  630. return -1;
  631. }
  632. result = dfs_file_close(fd);
  633. fd_put(fd);
  634. fd_put(fd);
  635. rt_free(d);
  636. if (result < 0)
  637. {
  638. rt_set_errno(result);
  639. return -1;
  640. }
  641. else
  642. return 0;
  643. }
  644. RTM_EXPORT(closedir);
  645. #ifdef DFS_USING_WORKDIR
  646. /**
  647. * this function is a POSIX compliant version, which will change working
  648. * directory.
  649. *
  650. * @param path the path name to be changed to.
  651. *
  652. * @return 0 on successful, -1 on failed.
  653. */
  654. int chdir(const char *path)
  655. {
  656. char *fullpath;
  657. DIR *d;
  658. if (path == RT_NULL)
  659. {
  660. dfs_lock();
  661. rt_kprintf("%s\n", working_directory);
  662. dfs_unlock();
  663. return 0;
  664. }
  665. if (rt_strlen(path) > DFS_PATH_MAX)
  666. {
  667. rt_set_errno(-DFS_STATUS_ENOTDIR);
  668. return -1;
  669. }
  670. fullpath = dfs_normalize_path(NULL, path);
  671. if (fullpath == RT_NULL)
  672. {
  673. rt_set_errno(-DFS_STATUS_ENOTDIR);
  674. return -1; /* build path failed */
  675. }
  676. dfs_lock();
  677. d = opendir(fullpath);
  678. if (d == RT_NULL)
  679. {
  680. rt_free(fullpath);
  681. /* this is a not exist directory */
  682. dfs_unlock();
  683. return -1;
  684. }
  685. /* close directory stream */
  686. closedir(d);
  687. /* copy full path to working directory */
  688. strncpy(working_directory, fullpath, DFS_PATH_MAX);
  689. /* release normalize directory path name */
  690. rt_free(fullpath);
  691. dfs_unlock();
  692. return 0;
  693. }
  694. RTM_EXPORT(chdir);
  695. #ifdef RT_USING_FINSH
  696. FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
  697. #endif
  698. #endif
  699. /**
  700. * this function is a POSIX compliant version, which will return current
  701. * working directory.
  702. *
  703. * @param buf the returned current directory.
  704. * @param size the buffer size.
  705. *
  706. * @return the returned current directory.
  707. */
  708. char *getcwd(char *buf, size_t size)
  709. {
  710. #ifdef DFS_USING_WORKDIR
  711. rt_enter_critical();
  712. rt_strncpy(buf, working_directory, size);
  713. rt_exit_critical();
  714. #else
  715. rt_kprintf(NO_WORKING_DIR);
  716. #endif
  717. return buf;
  718. }
  719. RTM_EXPORT(getcwd);
  720. /* @} */