dfs_posix.c 17 KB

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