dfs_posix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-05-27 Yi.qiu The first version
  9. * 2018-02-07 Bernard Change the 3rd parameter of open/fcntl/ioctl to '...'
  10. * 2022-01-19 Meco Man add creat()
  11. */
  12. #include <dfs_file.h>
  13. #include <dfs_private.h>
  14. #include <sys/errno.h>
  15. #ifdef RT_USING_SMART
  16. #include <lwp.h>
  17. #endif
  18. /**
  19. * @addtogroup FsPosixApi
  20. */
  21. /*@{*/
  22. /**
  23. * this function is a POSIX compliant version, which will open a file and
  24. * return a file descriptor according specified flags.
  25. *
  26. * @param file the path name of file.
  27. * @param flags the file open flags.
  28. *
  29. * @return the non-negative integer on successful open, others for failed.
  30. */
  31. int open(const char *file, int flags, ...)
  32. {
  33. int fd, result;
  34. struct dfs_fd *d;
  35. /* allocate a fd */
  36. fd = fd_new();
  37. if (fd < 0)
  38. {
  39. rt_set_errno(-ENOMEM);
  40. return -1;
  41. }
  42. d = fd_get(fd);
  43. result = dfs_file_open(d, file, flags);
  44. if (result < 0)
  45. {
  46. /* release the ref-count of fd */
  47. fd_release(fd);
  48. rt_set_errno(result);
  49. return -1;
  50. }
  51. return fd;
  52. }
  53. RTM_EXPORT(open);
  54. /**
  55. * this function is a POSIX compliant version,
  56. * which will create a new file or rewrite an existing one
  57. *
  58. * @param path the path name of file.
  59. * @param mode the file permission bits to be used in creating the file (not used, can be 0)
  60. *
  61. * @return the non-negative integer on successful open, others for failed.
  62. */
  63. int creat(const char *path, mode_t mode)
  64. {
  65. return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
  66. }
  67. RTM_EXPORT(creat);
  68. /**
  69. * this function is a POSIX compliant version, which will close the open
  70. * file descriptor.
  71. *
  72. * @param fd the file descriptor.
  73. *
  74. * @return 0 on successful, -1 on failed.
  75. */
  76. int close(int fd)
  77. {
  78. int result;
  79. struct dfs_fd *d;
  80. d = fd_get(fd);
  81. if (d == NULL)
  82. {
  83. rt_set_errno(-EBADF);
  84. return -1;
  85. }
  86. result = dfs_file_close(d);
  87. if (result < 0)
  88. {
  89. rt_set_errno(result);
  90. return -1;
  91. }
  92. fd_release(fd);
  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 _READ_WRITE_RETURN_TYPE
  108. _READ_WRITE_RETURN_TYPE read(int fd, void *buf, size_t len) /* some gcc tool chains will use different data structure */
  109. #else
  110. ssize_t 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. rt_set_errno(result);
  126. return -1;
  127. }
  128. return result;
  129. }
  130. RTM_EXPORT(read);
  131. /**
  132. * this function is a POSIX compliant version, which will write specified data
  133. * buffer length for an open file descriptor.
  134. *
  135. * @param fd the file descriptor
  136. * @param buf the data buffer to be written.
  137. * @param len the data buffer length.
  138. *
  139. * @return the actual written data buffer length.
  140. */
  141. #ifdef _READ_WRITE_RETURN_TYPE
  142. _READ_WRITE_RETURN_TYPE write(int fd, const void *buf, size_t len) /* some gcc tool chains will use different data structure */
  143. #else
  144. ssize_t write(int fd, const void *buf, size_t len)
  145. #endif
  146. {
  147. int result;
  148. struct dfs_fd *d;
  149. /* get the fd */
  150. d = fd_get(fd);
  151. if (d == NULL)
  152. {
  153. rt_set_errno(-EBADF);
  154. return -1;
  155. }
  156. result = dfs_file_write(d, buf, len);
  157. if (result < 0)
  158. {
  159. rt_set_errno(result);
  160. return -1;
  161. }
  162. return result;
  163. }
  164. RTM_EXPORT(write);
  165. /**
  166. * this function is a POSIX compliant version, which will seek the offset for
  167. * an open file descriptor.
  168. *
  169. * @param fd the file descriptor.
  170. * @param offset the offset to be seeked.
  171. * @param whence the directory of seek.
  172. *
  173. * @return the current read/write position in the file, or -1 on failed.
  174. */
  175. off_t lseek(int fd, off_t offset, int whence)
  176. {
  177. int result;
  178. struct dfs_fd *d;
  179. d = fd_get(fd);
  180. if (d == NULL)
  181. {
  182. rt_set_errno(-EBADF);
  183. return -1;
  184. }
  185. switch (whence)
  186. {
  187. case SEEK_SET:
  188. break;
  189. case SEEK_CUR:
  190. offset += d->pos;
  191. break;
  192. case SEEK_END:
  193. offset += d->vnode->size;
  194. break;
  195. default:
  196. rt_set_errno(-EINVAL);
  197. return -1;
  198. }
  199. if (offset < 0)
  200. {
  201. rt_set_errno(-EINVAL);
  202. return -1;
  203. }
  204. result = dfs_file_lseek(d, offset);
  205. if (result < 0)
  206. {
  207. rt_set_errno(result);
  208. return -1;
  209. }
  210. return offset;
  211. }
  212. RTM_EXPORT(lseek);
  213. #ifndef _WIN32
  214. /**
  215. * this function is a POSIX compliant version, which will rename old file name
  216. * to new file name.
  217. *
  218. * @param old the old file name.
  219. * @param new the new file name.
  220. *
  221. * @return 0 on successful, -1 on failed.
  222. *
  223. * note: the old and new file name must be belong to a same file system.
  224. */
  225. int rename(const char *old_file, const char *new_file)
  226. {
  227. int result;
  228. result = dfs_file_rename(old_file, new_file);
  229. if (result < 0)
  230. {
  231. rt_set_errno(result);
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. RTM_EXPORT(rename);
  237. #endif
  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 == NULL)
  292. {
  293. rt_set_errno(-EBADF);
  294. return -1;
  295. }
  296. /* it's the root directory */
  297. buf->st_dev = 0;
  298. buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  299. S_IWUSR | S_IWGRP | S_IWOTH;
  300. if (d->vnode->type == FT_DIRECTORY)
  301. {
  302. buf->st_mode &= ~S_IFREG;
  303. buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  304. }
  305. buf->st_size = d->vnode->size;
  306. buf->st_mtime = 0;
  307. return RT_EOK;
  308. }
  309. RTM_EXPORT(fstat);
  310. /**
  311. * this function is a POSIX compliant version, which shall request that all data
  312. * for the open file descriptor named by fildes is to be transferred to the storage
  313. * device associated with the file described by fildes.
  314. *
  315. * @param fildes the file description
  316. *
  317. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  318. * set to indicate the error.
  319. */
  320. int fsync(int fildes)
  321. {
  322. int ret;
  323. struct dfs_fd *d;
  324. /* get the fd */
  325. d = fd_get(fildes);
  326. if (d == NULL)
  327. {
  328. rt_set_errno(-EBADF);
  329. return -1;
  330. }
  331. ret = dfs_file_flush(d);
  332. return ret;
  333. }
  334. RTM_EXPORT(fsync);
  335. /**
  336. * this function is a POSIX compliant version, which shall perform a variety of
  337. * control functions on devices.
  338. *
  339. * @param fildes the file description
  340. * @param cmd the specified command
  341. * @param data represents the additional information that is needed by this
  342. * specific device to perform the requested function.
  343. *
  344. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  345. * set to indicate the error.
  346. */
  347. int fcntl(int fildes, int cmd, ...)
  348. {
  349. int ret = -1;
  350. struct dfs_fd *d;
  351. /* get the fd */
  352. d = fd_get(fildes);
  353. if (d)
  354. {
  355. void *arg;
  356. va_list ap;
  357. va_start(ap, cmd);
  358. arg = va_arg(ap, void *);
  359. va_end(ap);
  360. ret = dfs_file_ioctl(d, cmd, arg);
  361. }
  362. else ret = -EBADF;
  363. if (ret < 0)
  364. {
  365. rt_set_errno(ret);
  366. ret = -1;
  367. }
  368. return ret;
  369. }
  370. RTM_EXPORT(fcntl);
  371. /**
  372. * this function is a POSIX compliant version, which shall perform a variety of
  373. * control functions on devices.
  374. *
  375. * @param fildes the file description
  376. * @param cmd the specified command
  377. * @param data represents the additional information that is needed by this
  378. * specific device to perform the requested function.
  379. *
  380. * @return 0 on successful completion. Otherwise, -1 shall be returned and errno
  381. * set to indicate the error.
  382. */
  383. int ioctl(int fildes, int cmd, ...)
  384. {
  385. void *arg;
  386. va_list ap;
  387. va_start(ap, cmd);
  388. arg = va_arg(ap, void *);
  389. va_end(ap);
  390. /* we use fcntl for this API. */
  391. return fcntl(fildes, cmd, arg);
  392. }
  393. RTM_EXPORT(ioctl);
  394. /**
  395. *
  396. * this function is a POSIX compliant version, which cause the regular file
  397. * referenced by fd to be truncated to a size of precisely length bytes.
  398. * @param fd the file descriptor.
  399. * @param length the length to be truncated.
  400. *
  401. * @return Upon successful completion, ftruncate() shall return 0;
  402. * otherwise, -1 shall be returned and errno set to indicate the error.
  403. */
  404. int ftruncate(int fd, off_t length)
  405. {
  406. int result;
  407. struct dfs_fd *d;
  408. d = fd_get(fd);
  409. if (d == NULL)
  410. {
  411. rt_set_errno(-EBADF);
  412. return -1;
  413. }
  414. if (length < 0)
  415. {
  416. rt_set_errno(-EINVAL);
  417. return -1;
  418. }
  419. result = dfs_file_ftruncate(d, length);
  420. if (result < 0)
  421. {
  422. rt_set_errno(result);
  423. return -1;
  424. }
  425. return 0;
  426. }
  427. RTM_EXPORT(ftruncate);
  428. /**
  429. * this function is a POSIX compliant version, which will return the
  430. * information about a mounted file system.
  431. *
  432. * @param path the path which mounted file system.
  433. * @param buf the buffer to save the returned information.
  434. *
  435. * @return 0 on successful, others on failed.
  436. */
  437. int statfs(const char *path, struct statfs *buf)
  438. {
  439. int result;
  440. result = dfs_statfs(path, buf);
  441. if (result < 0)
  442. {
  443. rt_set_errno(result);
  444. return -1;
  445. }
  446. return result;
  447. }
  448. RTM_EXPORT(statfs);
  449. /**
  450. * this function is a POSIX compliant version, which will make a directory
  451. *
  452. * @param path the directory path to be made.
  453. * @param mode
  454. *
  455. * @return 0 on successful, others on failed.
  456. */
  457. int mkdir(const char *path, mode_t mode)
  458. {
  459. int fd;
  460. struct dfs_fd *d;
  461. int result;
  462. fd = fd_new();
  463. if (fd == -1)
  464. {
  465. rt_set_errno(-ENOMEM);
  466. return -1;
  467. }
  468. d = fd_get(fd);
  469. result = dfs_file_open(d, path, O_DIRECTORY | O_CREAT);
  470. if (result < 0)
  471. {
  472. fd_release(fd);
  473. rt_set_errno(result);
  474. return -1;
  475. }
  476. dfs_file_close(d);
  477. fd_release(fd);
  478. return 0;
  479. }
  480. RTM_EXPORT(mkdir);
  481. /**
  482. * this function is a POSIX compliant version, which will remove a directory.
  483. *
  484. * @param pathname the path name to be removed.
  485. *
  486. * @return 0 on successful, others on failed.
  487. */
  488. int rmdir(const char *pathname)
  489. {
  490. int result;
  491. result = dfs_file_unlink(pathname);
  492. if (result < 0)
  493. {
  494. rt_set_errno(result);
  495. return -1;
  496. }
  497. return 0;
  498. }
  499. RTM_EXPORT(rmdir);
  500. /**
  501. * this function is a POSIX compliant version, which will open a directory.
  502. *
  503. * @param name the path name to be open.
  504. *
  505. * @return the DIR pointer of directory, NULL on open directory failed.
  506. */
  507. DIR *opendir(const char *name)
  508. {
  509. struct dfs_fd *d;
  510. int fd, result;
  511. DIR *t;
  512. t = NULL;
  513. /* allocate a fd */
  514. fd = fd_new();
  515. if (fd == -1)
  516. {
  517. rt_set_errno(-ENOMEM);
  518. return NULL;
  519. }
  520. d = fd_get(fd);
  521. result = dfs_file_open(d, name, O_RDONLY | O_DIRECTORY);
  522. if (result >= 0)
  523. {
  524. /* open successfully */
  525. t = (DIR *) rt_malloc(sizeof(DIR));
  526. if (t == NULL)
  527. {
  528. dfs_file_close(d);
  529. fd_release(fd);
  530. }
  531. else
  532. {
  533. rt_memset(t, 0, sizeof(DIR));
  534. t->fd = fd;
  535. }
  536. return t;
  537. }
  538. /* open failed */
  539. fd_release(fd);
  540. rt_set_errno(result);
  541. return NULL;
  542. }
  543. RTM_EXPORT(opendir);
  544. /**
  545. * this function is a POSIX compliant version, which will return a pointer
  546. * to a dirent structure representing the next directory entry in the
  547. * directory stream.
  548. *
  549. * @param d the directory stream pointer.
  550. *
  551. * @return the next directory entry, NULL on the end of directory or failed.
  552. */
  553. struct dirent *readdir(DIR *d)
  554. {
  555. int result;
  556. struct dfs_fd *fd;
  557. fd = fd_get(d->fd);
  558. if (fd == NULL)
  559. {
  560. rt_set_errno(-EBADF);
  561. return NULL;
  562. }
  563. if (d->num)
  564. {
  565. struct dirent *dirent_ptr;
  566. dirent_ptr = (struct dirent *)&d->buf[d->cur];
  567. d->cur += dirent_ptr->d_reclen;
  568. }
  569. if (!d->num || d->cur >= d->num)
  570. {
  571. /* get a new entry */
  572. result = dfs_file_getdents(fd,
  573. (struct dirent *)d->buf,
  574. sizeof(d->buf) - 1);
  575. if (result <= 0)
  576. {
  577. rt_set_errno(result);
  578. return NULL;
  579. }
  580. d->num = result;
  581. d->cur = 0; /* current entry index */
  582. }
  583. return (struct dirent *)(d->buf + d->cur);
  584. }
  585. RTM_EXPORT(readdir);
  586. /**
  587. * this function is a POSIX compliant version, which will return current
  588. * location in directory stream.
  589. *
  590. * @param d the directory stream pointer.
  591. *
  592. * @return the current location in directory stream.
  593. */
  594. long telldir(DIR *d)
  595. {
  596. struct dfs_fd *fd;
  597. long result;
  598. fd = fd_get(d->fd);
  599. if (fd == NULL)
  600. {
  601. rt_set_errno(-EBADF);
  602. return 0;
  603. }
  604. result = fd->pos - d->num + d->cur;
  605. return result;
  606. }
  607. RTM_EXPORT(telldir);
  608. /**
  609. * this function is a POSIX compliant version, which will set position of
  610. * next directory structure in the directory stream.
  611. *
  612. * @param d the directory stream.
  613. * @param offset the offset in directory stream.
  614. */
  615. void seekdir(DIR *d, long offset)
  616. {
  617. struct dfs_fd *fd;
  618. fd = fd_get(d->fd);
  619. if (fd == NULL)
  620. {
  621. rt_set_errno(-EBADF);
  622. return ;
  623. }
  624. /* seek to the offset position of directory */
  625. if (dfs_file_lseek(fd, offset) >= 0)
  626. d->num = d->cur = 0;
  627. }
  628. RTM_EXPORT(seekdir);
  629. /**
  630. * this function is a POSIX compliant version, which will reset directory
  631. * stream.
  632. *
  633. * @param d the directory stream.
  634. */
  635. void rewinddir(DIR *d)
  636. {
  637. struct dfs_fd *fd;
  638. fd = fd_get(d->fd);
  639. if (fd == NULL)
  640. {
  641. rt_set_errno(-EBADF);
  642. return ;
  643. }
  644. /* seek to the beginning of directory */
  645. if (dfs_file_lseek(fd, 0) >= 0)
  646. d->num = d->cur = 0;
  647. }
  648. RTM_EXPORT(rewinddir);
  649. /**
  650. * this function is a POSIX compliant version, which will close a directory
  651. * stream.
  652. *
  653. * @param d the directory stream.
  654. *
  655. * @return 0 on successful, -1 on failed.
  656. */
  657. int closedir(DIR *d)
  658. {
  659. int result;
  660. struct dfs_fd *fd;
  661. fd = fd_get(d->fd);
  662. if (fd == NULL)
  663. {
  664. rt_set_errno(-EBADF);
  665. return -1;
  666. }
  667. result = dfs_file_close(fd);
  668. fd_release(d->fd);
  669. rt_free(d);
  670. if (result < 0)
  671. {
  672. rt_set_errno(result);
  673. return -1;
  674. }
  675. else
  676. return 0;
  677. }
  678. RTM_EXPORT(closedir);
  679. #ifdef DFS_USING_WORKDIR
  680. /**
  681. * this function is a POSIX compliant version, which will change working
  682. * directory.
  683. *
  684. * @param path the path name to be changed to.
  685. *
  686. * @return 0 on successful, -1 on failed.
  687. */
  688. int chdir(const char *path)
  689. {
  690. char *fullpath;
  691. DIR *d;
  692. if (path == NULL)
  693. {
  694. dfs_lock();
  695. #ifdef DFS_USING_WORKDIR
  696. rt_kprintf("%s\n", working_directory);
  697. #endif
  698. dfs_unlock();
  699. return 0;
  700. }
  701. if (strlen(path) > DFS_PATH_MAX)
  702. {
  703. rt_set_errno(-ENOTDIR);
  704. return -1;
  705. }
  706. fullpath = dfs_normalize_path(NULL, path);
  707. if (fullpath == NULL)
  708. {
  709. rt_set_errno(-ENOTDIR);
  710. return -1; /* build path failed */
  711. }
  712. dfs_lock();
  713. d = opendir(fullpath);
  714. if (d == NULL)
  715. {
  716. rt_free(fullpath);
  717. /* this is a not exist directory */
  718. dfs_unlock();
  719. return -1;
  720. }
  721. /* close directory stream */
  722. closedir(d);
  723. #ifdef RT_USING_SMART
  724. /* copy full path to working directory */
  725. lwp_setcwd(fullpath);
  726. #else
  727. rt_strncpy(working_directory, fullpath, DFS_PATH_MAX);
  728. #endif
  729. /* release normalize directory path name */
  730. rt_free(fullpath);
  731. dfs_unlock();
  732. return 0;
  733. }
  734. RTM_EXPORT(chdir);
  735. #ifdef RT_USING_FINSH
  736. FINSH_FUNCTION_EXPORT_ALIAS(chdir, cd, change current working directory);
  737. #endif
  738. #endif
  739. /**
  740. * this function is a POSIX compliant version, which shall check the file named
  741. * by the pathname pointed to by the path argument for accessibility according
  742. * to the bit pattern contained in amode.
  743. *
  744. * @param path the specified file/dir path.
  745. * @param amode the value is either the bitwise-inclusive OR of the access
  746. * permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).
  747. */
  748. int access(const char *path, int amode)
  749. {
  750. struct stat sb;
  751. if (stat(path, &sb) < 0)
  752. return -1; /* already sets errno */
  753. /* ignore R_OK,W_OK,X_OK condition */
  754. return 0;
  755. }
  756. /**
  757. * this function is a POSIX compliant version, which will set current
  758. * working directory.
  759. *
  760. * @param buf the current directory.
  761. *
  762. * @return null.
  763. */
  764. void setcwd(char *buf)
  765. {
  766. #ifdef DFS_USING_WORKDIR
  767. dfs_lock();
  768. #ifdef RT_USING_SMART
  769. lwp_setcwd(buf);
  770. #else
  771. rt_strncpy(working_directory, buf, DFS_PATH_MAX);
  772. #endif
  773. dfs_unlock();
  774. #else
  775. rt_kprintf(NO_WORKING_DIR);
  776. #endif
  777. return ;
  778. }
  779. RTM_EXPORT(setcwd);
  780. /**
  781. * this function is a POSIX compliant version, which will return current
  782. * working directory.
  783. *
  784. * @param buf the returned current directory.
  785. * @param size the buffer size.
  786. *
  787. * @return the returned current directory.
  788. */
  789. char *getcwd(char *buf, size_t size)
  790. {
  791. #ifdef DFS_USING_WORKDIR
  792. char *dir_buf = RT_NULL;
  793. dfs_lock();
  794. #ifdef RT_USING_SMART
  795. dir_buf = lwp_getcwd();
  796. #else
  797. dir_buf = &working_directory[0];
  798. #endif
  799. /* copy to buf parameter */
  800. if (buf)
  801. {
  802. rt_strncpy(buf, dir_buf, size);
  803. }
  804. dfs_unlock();
  805. #else
  806. rt_kprintf(NO_WORKING_DIR);
  807. #endif
  808. return buf;
  809. }
  810. RTM_EXPORT(getcwd);
  811. /* @} */