dfs_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. * 2005-02-22 Bernard The first version.
  9. * 2011-12-08 Bernard Merges rename patch from iamcacy.
  10. * 2015-05-27 Bernard Fix the fd clear issue.
  11. * 2019-01-24 Bernard Remove file repeatedly open check.
  12. */
  13. #include <dfs.h>
  14. #include <dfs_file.h>
  15. #include <dfs_private.h>
  16. #include <unistd.h>
  17. #define DFS_FNODE_HASH_NR 128
  18. struct dfs_fnode_mgr
  19. {
  20. struct rt_mutex lock;
  21. rt_list_t head[DFS_FNODE_HASH_NR];
  22. };
  23. static struct dfs_fnode_mgr dfs_fm;
  24. void dfs_fm_lock(void)
  25. {
  26. rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
  27. }
  28. void dfs_fm_unlock(void)
  29. {
  30. rt_mutex_release(&dfs_fm.lock);
  31. }
  32. void dfs_fnode_mgr_init(void)
  33. {
  34. int i = 0;
  35. rt_mutex_init(&dfs_fm.lock, "dfs_mgr", RT_IPC_FLAG_PRIO);
  36. for (i = 0; i < DFS_FNODE_HASH_NR; i++)
  37. {
  38. rt_list_init(&dfs_fm.head[i]);
  39. }
  40. }
  41. /* BKDR Hash Function */
  42. static unsigned int bkdr_hash(const char *str)
  43. {
  44. unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
  45. unsigned int hash = 0;
  46. while (*str)
  47. {
  48. hash = hash * seed + (*str++);
  49. }
  50. return (hash % DFS_FNODE_HASH_NR);
  51. }
  52. static struct dfs_fnode *dfs_fnode_find(const char *path, rt_list_t **hash_head)
  53. {
  54. struct dfs_fnode *vnode = NULL;
  55. int hash = bkdr_hash(path);
  56. rt_list_t *hh;
  57. hh = dfs_fm.head[hash].next;
  58. if (hash_head)
  59. {
  60. *hash_head = &dfs_fm.head[hash];
  61. }
  62. while (hh != &dfs_fm.head[hash])
  63. {
  64. vnode = rt_container_of(hh, struct dfs_fnode, list);
  65. if (rt_strcmp(path, vnode->fullpath) == 0)
  66. {
  67. /* found */
  68. return vnode;
  69. }
  70. hh = hh->next;
  71. }
  72. return NULL;
  73. }
  74. /**
  75. * @addtogroup FileApi
  76. */
  77. /*@{*/
  78. /**
  79. * This function will return whether this file has been opend.
  80. *
  81. * @param pathname the file path name.
  82. *
  83. * @return 0 on file has been open successfully, -1 on open failed.
  84. */
  85. int dfs_file_is_open(const char *pathname)
  86. {
  87. char *fullpath = NULL;
  88. struct dfs_fnode *vnode = NULL;
  89. int ret = 0;
  90. fullpath = dfs_normalize_path(NULL, pathname);
  91. dfs_fm_lock();
  92. vnode = dfs_fnode_find(fullpath, NULL);
  93. if (vnode)
  94. {
  95. ret = 1;
  96. }
  97. dfs_fm_unlock();
  98. rt_free(fullpath);
  99. return ret;
  100. }
  101. /**
  102. * this function will open a file which specified by path with specified flags.
  103. *
  104. * @param fd the file descriptor pointer to return the corresponding result.
  105. * @param path the specified file path.
  106. * @param flags the flags for open operator.
  107. *
  108. * @return 0 on successful, -1 on failed.
  109. */
  110. int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
  111. {
  112. struct dfs_filesystem *fs;
  113. char *fullpath;
  114. int result;
  115. struct dfs_fnode *vnode = NULL;
  116. rt_list_t *hash_head;
  117. /* parameter check */
  118. if (fd == NULL)
  119. return -EINVAL;
  120. /* make sure we have an absolute path */
  121. fullpath = dfs_normalize_path(NULL, path);
  122. if (fullpath == NULL)
  123. {
  124. return -ENOMEM;
  125. }
  126. LOG_D("open file:%s", fullpath);
  127. dfs_fm_lock();
  128. /* vnode find */
  129. vnode = dfs_fnode_find(fullpath, &hash_head);
  130. if (vnode)
  131. {
  132. vnode->ref_count++;
  133. fd->pos = 0;
  134. fd->vnode = vnode;
  135. dfs_fm_unlock();
  136. rt_free(fullpath); /* release path */
  137. }
  138. else
  139. {
  140. /* find filesystem */
  141. fs = dfs_filesystem_lookup(fullpath);
  142. if (fs == NULL)
  143. {
  144. dfs_fm_unlock();
  145. rt_free(fullpath); /* release path */
  146. return -ENOENT;
  147. }
  148. vnode = rt_calloc(1, sizeof(struct dfs_fnode));
  149. if (!vnode)
  150. {
  151. dfs_fm_unlock();
  152. rt_free(fullpath); /* release path */
  153. return -ENOMEM;
  154. }
  155. vnode->ref_count = 1;
  156. LOG_D("open in filesystem:%s", fs->ops->name);
  157. vnode->fs = fs; /* set file system */
  158. vnode->fops = fs->ops->fops; /* set file ops */
  159. /* initialize the fd item */
  160. vnode->type = FT_REGULAR;
  161. vnode->flags = 0;
  162. if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
  163. {
  164. if (dfs_subdir(fs->path, fullpath) == NULL)
  165. vnode->path = rt_strdup("/");
  166. else
  167. vnode->path = rt_strdup(dfs_subdir(fs->path, fullpath));
  168. LOG_D("Actual file path: %s", vnode->path);
  169. }
  170. else
  171. {
  172. vnode->path = fullpath;
  173. }
  174. vnode->fullpath = fullpath;
  175. /* specific file system open routine */
  176. if (vnode->fops->open == NULL)
  177. {
  178. dfs_fm_unlock();
  179. /* clear fd */
  180. if (vnode->path != vnode->fullpath)
  181. {
  182. rt_free(vnode->fullpath);
  183. }
  184. rt_free(vnode->path);
  185. rt_free(vnode);
  186. return -ENOSYS;
  187. }
  188. fd->pos = 0;
  189. fd->vnode = vnode;
  190. /* insert vnode to hash */
  191. rt_list_insert_after(hash_head, &vnode->list);
  192. }
  193. fd->flags = flags;
  194. if ((result = vnode->fops->open(fd)) < 0)
  195. {
  196. vnode->ref_count--;
  197. if (vnode->ref_count == 0)
  198. {
  199. /* remove from hash */
  200. rt_list_remove(&vnode->list);
  201. /* clear fd */
  202. if (vnode->path != vnode->fullpath)
  203. {
  204. rt_free(vnode->fullpath);
  205. }
  206. rt_free(vnode->path);
  207. fd->vnode = NULL;
  208. rt_free(vnode);
  209. }
  210. dfs_fm_unlock();
  211. LOG_D("%s open failed", fullpath);
  212. return result;
  213. }
  214. fd->flags |= DFS_F_OPEN;
  215. if (flags & O_DIRECTORY)
  216. {
  217. fd->vnode->type = FT_DIRECTORY;
  218. fd->flags |= DFS_F_DIRECTORY;
  219. }
  220. dfs_fm_unlock();
  221. LOG_D("open successful");
  222. return 0;
  223. }
  224. /**
  225. * this function will close a file descriptor.
  226. *
  227. * @param fd the file descriptor to be closed.
  228. *
  229. * @return 0 on successful, -1 on failed.
  230. */
  231. int dfs_file_close(struct dfs_fd *fd)
  232. {
  233. struct dfs_fnode *vnode = NULL;
  234. int result = 0;
  235. if (fd == NULL)
  236. {
  237. return -ENXIO;
  238. }
  239. if (fd->ref_count == 1)
  240. {
  241. dfs_fm_lock();
  242. vnode = fd->vnode;
  243. if (vnode->ref_count <= 0)
  244. {
  245. dfs_fm_unlock();
  246. return -ENXIO;
  247. }
  248. if (vnode->fops->close != NULL)
  249. {
  250. result = vnode->fops->close(fd);
  251. }
  252. /* close fd error, return */
  253. if (result < 0)
  254. {
  255. dfs_fm_unlock();
  256. return result;
  257. }
  258. if (vnode->ref_count == 1)
  259. {
  260. /* remove from hash */
  261. rt_list_remove(&vnode->list);
  262. fd->vnode = NULL;
  263. if (vnode->path != vnode->fullpath)
  264. {
  265. rt_free(vnode->fullpath);
  266. }
  267. rt_free(vnode->path);
  268. rt_free(vnode);
  269. }
  270. dfs_fm_unlock();
  271. }
  272. return result;
  273. }
  274. /**
  275. * this function will perform a io control on a file descriptor.
  276. *
  277. * @param fd the file descriptor.
  278. * @param cmd the command to send to file descriptor.
  279. * @param args the argument to send to file descriptor.
  280. *
  281. * @return 0 on successful, -1 on failed.
  282. */
  283. int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
  284. {
  285. if (fd == NULL)
  286. {
  287. return -EINVAL;
  288. }
  289. /* regular file system fd */
  290. if (fd->vnode->type == FT_REGULAR || fd->vnode->type == FT_DEVICE)
  291. {
  292. switch (cmd)
  293. {
  294. case F_GETFL:
  295. return fd->flags; /* return flags */
  296. case F_SETFL:
  297. {
  298. int flags = (int)(rt_base_t)args;
  299. int mask = O_NONBLOCK | O_APPEND;
  300. flags &= mask;
  301. fd->flags &= ~mask;
  302. fd->flags |= flags;
  303. }
  304. return 0;
  305. }
  306. }
  307. if (fd->vnode->fops->ioctl != NULL)
  308. {
  309. return fd->vnode->fops->ioctl(fd, cmd, args);
  310. }
  311. return -ENOSYS;
  312. }
  313. /**
  314. * this function will read specified length data from a file descriptor to a
  315. * buffer.
  316. *
  317. * @param fd the file descriptor.
  318. * @param buf the buffer to save the read data.
  319. * @param len the length of data buffer to be read.
  320. *
  321. * @return the actual read data bytes or 0 on end of file or failed.
  322. */
  323. int dfs_file_read(struct dfs_fd *fd, void *buf, size_t len)
  324. {
  325. int result = 0;
  326. if (fd == NULL)
  327. {
  328. return -EINVAL;
  329. }
  330. if (fd->vnode->fops->read == NULL)
  331. {
  332. return -ENOSYS;
  333. }
  334. if ((result = fd->vnode->fops->read(fd, buf, len)) < 0)
  335. {
  336. fd->flags |= DFS_F_EOF;
  337. }
  338. return result;
  339. }
  340. /**
  341. * this function will fetch directory entries from a directory descriptor.
  342. *
  343. * @param fd the directory descriptor.
  344. * @param dirp the dirent buffer to save result.
  345. * @param nbytes the available room in the buffer.
  346. *
  347. * @return the read dirent, others on failed.
  348. */
  349. int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, size_t nbytes)
  350. {
  351. /* parameter check */
  352. if (fd == NULL)
  353. {
  354. return -EINVAL;
  355. }
  356. if (fd->vnode->type != FT_DIRECTORY)
  357. {
  358. return -EINVAL;
  359. }
  360. if (fd->vnode->fops->getdents != NULL)
  361. {
  362. return fd->vnode->fops->getdents(fd, dirp, nbytes);
  363. }
  364. return -ENOSYS;
  365. }
  366. /**
  367. * this function will unlink (remove) a specified path file from file system.
  368. *
  369. * @param path the specified path file to be unlinked.
  370. *
  371. * @return 0 on successful, -1 on failed.
  372. */
  373. int dfs_file_unlink(const char *path)
  374. {
  375. int result;
  376. char *fullpath;
  377. struct dfs_filesystem *fs;
  378. /* Make sure we have an absolute path */
  379. fullpath = dfs_normalize_path(NULL, path);
  380. if (fullpath == NULL)
  381. {
  382. return -EINVAL;
  383. }
  384. /* Check whether file is already open */
  385. if (dfs_file_is_open(fullpath))
  386. {
  387. result = -EBUSY;
  388. goto __exit;
  389. }
  390. /* get filesystem */
  391. if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
  392. {
  393. result = -ENOENT;
  394. goto __exit;
  395. }
  396. if (fs->ops->unlink != NULL)
  397. {
  398. if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
  399. {
  400. if (dfs_subdir(fs->path, fullpath) == NULL)
  401. result = fs->ops->unlink(fs, "/");
  402. else
  403. result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
  404. }
  405. else
  406. result = fs->ops->unlink(fs, fullpath);
  407. }
  408. else result = -ENOSYS;
  409. __exit:
  410. rt_free(fullpath);
  411. return result;
  412. }
  413. /**
  414. * this function will write some specified length data to file system.
  415. *
  416. * @param fd the file descriptor.
  417. * @param buf the data buffer to be written.
  418. * @param len the data buffer length
  419. *
  420. * @return the actual written data length.
  421. */
  422. int dfs_file_write(struct dfs_fd *fd, const void *buf, size_t len)
  423. {
  424. if (fd == NULL)
  425. {
  426. return -EINVAL;
  427. }
  428. if (fd->vnode->fops->write == NULL)
  429. {
  430. return -ENOSYS;
  431. }
  432. return fd->vnode->fops->write(fd, buf, len);
  433. }
  434. /**
  435. * this function will flush buffer on a file descriptor.
  436. *
  437. * @param fd the file descriptor.
  438. *
  439. * @return 0 on successful, -1 on failed.
  440. */
  441. int dfs_file_flush(struct dfs_fd *fd)
  442. {
  443. if (fd == NULL)
  444. return -EINVAL;
  445. if (fd->vnode->fops->flush == NULL)
  446. return -ENOSYS;
  447. return fd->vnode->fops->flush(fd);
  448. }
  449. /**
  450. * this function will seek the offset for specified file descriptor.
  451. *
  452. * @param fd the file descriptor.
  453. * @param offset the offset to be sought.
  454. *
  455. * @return the current position after seek.
  456. */
  457. int dfs_file_lseek(struct dfs_fd *fd, off_t offset)
  458. {
  459. int result;
  460. if (fd == NULL)
  461. return -EINVAL;
  462. if (fd->vnode->fops->lseek == NULL)
  463. return -ENOSYS;
  464. result = fd->vnode->fops->lseek(fd, offset);
  465. /* update current position */
  466. if (result >= 0)
  467. fd->pos = result;
  468. return result;
  469. }
  470. /**
  471. * this function will get file information.
  472. *
  473. * @param path the file path.
  474. * @param buf the data buffer to save stat description.
  475. *
  476. * @return 0 on successful, -1 on failed.
  477. */
  478. int dfs_file_stat(const char *path, struct stat *buf)
  479. {
  480. int result;
  481. char *fullpath;
  482. struct dfs_filesystem *fs;
  483. fullpath = dfs_normalize_path(NULL, path);
  484. if (fullpath == NULL)
  485. {
  486. return -1;
  487. }
  488. if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
  489. {
  490. LOG_E("can't find mounted filesystem on this path:%s", fullpath);
  491. rt_free(fullpath);
  492. return -ENOENT;
  493. }
  494. if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
  495. (dfs_subdir(fs->path, fullpath) == NULL))
  496. {
  497. /* it's the root directory */
  498. buf->st_dev = 0;
  499. buf->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
  500. S_IWUSR | S_IWGRP | S_IWOTH;
  501. buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  502. buf->st_size = 0;
  503. buf->st_mtime = 0;
  504. /* release full path */
  505. rt_free(fullpath);
  506. return RT_EOK;
  507. }
  508. else
  509. {
  510. if (fs->ops->stat == NULL)
  511. {
  512. rt_free(fullpath);
  513. LOG_E("the filesystem didn't implement this function");
  514. return -ENOSYS;
  515. }
  516. /* get the real file path and get file stat */
  517. if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
  518. result = fs->ops->stat(fs, fullpath, buf);
  519. else
  520. result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
  521. }
  522. rt_free(fullpath);
  523. return result;
  524. }
  525. /**
  526. * this function will rename an old path name to a new path name.
  527. *
  528. * @param oldpath the old path name.
  529. * @param newpath the new path name.
  530. *
  531. * @return 0 on successful, -1 on failed.
  532. */
  533. int dfs_file_rename(const char *oldpath, const char *newpath)
  534. {
  535. int result = RT_EOK;
  536. struct dfs_filesystem *oldfs = NULL, *newfs = NULL;
  537. char *oldfullpath = NULL, *newfullpath = NULL;
  538. newfullpath = NULL;
  539. oldfullpath = NULL;
  540. oldfullpath = dfs_normalize_path(NULL, oldpath);
  541. if (oldfullpath == NULL)
  542. {
  543. result = -ENOENT;
  544. goto __exit;
  545. }
  546. if (dfs_file_is_open((const char *)oldfullpath))
  547. {
  548. result = -EBUSY;
  549. goto __exit;
  550. }
  551. newfullpath = dfs_normalize_path(NULL, newpath);
  552. if (newfullpath == NULL)
  553. {
  554. result = -ENOENT;
  555. goto __exit;
  556. }
  557. oldfs = dfs_filesystem_lookup(oldfullpath);
  558. newfs = dfs_filesystem_lookup(newfullpath);
  559. if (oldfs == newfs)
  560. {
  561. if (oldfs->ops->rename == NULL)
  562. {
  563. result = -ENOSYS;
  564. }
  565. else
  566. {
  567. if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
  568. result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
  569. else
  570. /* use sub directory to rename in file system */
  571. result = oldfs->ops->rename(oldfs,
  572. dfs_subdir(oldfs->path, oldfullpath),
  573. dfs_subdir(newfs->path, newfullpath));
  574. }
  575. }
  576. else
  577. {
  578. result = -EXDEV;
  579. }
  580. __exit:
  581. if (oldfullpath)
  582. {
  583. rt_free(oldfullpath);
  584. }
  585. if (newfullpath)
  586. {
  587. rt_free(newfullpath);
  588. }
  589. /* not at same file system, return EXDEV */
  590. return result;
  591. }
  592. /**
  593. * this function is will cause the regular file referenced by fd
  594. * to be truncated to a size of precisely length bytes.
  595. *
  596. * @param fd the file descriptor.
  597. * @param length the length to be truncated.
  598. *
  599. * @return the status of truncated.
  600. */
  601. int dfs_file_ftruncate(struct dfs_fd *fd, off_t length)
  602. {
  603. int result;
  604. /* fd is null or not a regular file system fd, or length is invalid */
  605. if (fd == NULL || fd->vnode->type != FT_REGULAR || length < 0)
  606. return -EINVAL;
  607. if (fd->vnode->fops->ioctl == NULL)
  608. return -ENOSYS;
  609. result = fd->vnode->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
  610. /* update current size */
  611. if (result == 0)
  612. fd->vnode->size = length;
  613. return result;
  614. }
  615. int dfs_file_mmap2(struct dfs_fd *fd, struct dfs_mmap2_args *mmap2)
  616. {
  617. int ret = 0;
  618. if (fd && mmap2)
  619. {
  620. if (fd->vnode->type != FT_DEVICE || !fd->vnode->fops->ioctl)
  621. {
  622. rt_set_errno(EINVAL);
  623. }
  624. else if (fd->vnode->type == FT_DEVICE && fd->vnode->fops->ioctl)
  625. {
  626. ret = fd->vnode->fops->ioctl(fd, RT_FIOMMAP2, mmap2);
  627. if (ret != 0)
  628. {
  629. ret = ret > 0? ret : -ret;
  630. rt_set_errno(ret);
  631. }
  632. }
  633. }
  634. return ret;
  635. }
  636. #ifdef RT_USING_FINSH
  637. #include <finsh.h>
  638. void ls(const char *pathname)
  639. {
  640. struct dfs_fd fd;
  641. struct dirent dirent;
  642. struct stat stat;
  643. int length;
  644. char *fullpath, *path;
  645. fullpath = NULL;
  646. if (pathname == NULL)
  647. {
  648. #ifdef DFS_USING_WORKDIR
  649. /* open current working directory */
  650. path = rt_strdup(working_directory);
  651. #else
  652. path = rt_strdup("/");
  653. #endif
  654. if (path == NULL)
  655. return ; /* out of memory */
  656. }
  657. else
  658. {
  659. path = (char *)pathname;
  660. }
  661. fd_init(&fd);
  662. /* list directory */
  663. if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
  664. {
  665. rt_kprintf("Directory %s:\n", path);
  666. do
  667. {
  668. rt_memset(&dirent, 0, sizeof(struct dirent));
  669. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  670. if (length > 0)
  671. {
  672. rt_memset(&stat, 0, sizeof(struct stat));
  673. /* build full path for each file */
  674. fullpath = dfs_normalize_path(path, dirent.d_name);
  675. if (fullpath == NULL)
  676. break;
  677. if (dfs_file_stat(fullpath, &stat) == 0)
  678. {
  679. rt_kprintf("%-20s", dirent.d_name);
  680. if (S_ISDIR(stat.st_mode))
  681. {
  682. rt_kprintf("%-25s\n", "<DIR>");
  683. }
  684. else
  685. {
  686. rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
  687. }
  688. }
  689. else
  690. rt_kprintf("BAD file: %s\n", dirent.d_name);
  691. rt_free(fullpath);
  692. }
  693. }
  694. while (length > 0);
  695. dfs_file_close(&fd);
  696. }
  697. else
  698. {
  699. rt_kprintf("No such directory\n");
  700. }
  701. if (pathname == NULL)
  702. rt_free(path);
  703. }
  704. FINSH_FUNCTION_EXPORT(ls, list directory contents);
  705. void rm(const char *filename)
  706. {
  707. if (dfs_file_unlink(filename) < 0)
  708. {
  709. rt_kprintf("Delete %s failed\n", filename);
  710. }
  711. }
  712. FINSH_FUNCTION_EXPORT(rm, remove files or directories);
  713. void cat(const char *filename)
  714. {
  715. struct dfs_fd fd;
  716. int length = 0;
  717. char buffer[81];
  718. fd_init(&fd);
  719. if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
  720. {
  721. rt_kprintf("Open %s failed\n", filename);
  722. return;
  723. }
  724. do
  725. {
  726. rt_memset(buffer, 0x0, sizeof(buffer));
  727. length = dfs_file_read(&fd, (void *)buffer, sizeof(buffer) - 1);
  728. if (length > 0)
  729. {
  730. buffer[length] = '\0';
  731. rt_device_t out_device = rt_console_get_device();
  732. rt_device_write(out_device, 0, (void *)buffer, sizeof(buffer));
  733. }
  734. } while (length > 0);
  735. rt_kprintf("\n");
  736. dfs_file_close(&fd);
  737. }
  738. FINSH_FUNCTION_EXPORT(cat, print file);
  739. #ifdef DFS_USING_POSIX
  740. #define BUF_SZ 4096
  741. static void copyfile(const char *src, const char *dst)
  742. {
  743. struct dfs_fd fd;
  744. struct dfs_fd src_fd;
  745. rt_uint8_t *block_ptr;
  746. rt_int32_t read_bytes;
  747. block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
  748. if (block_ptr == NULL)
  749. {
  750. rt_kprintf("out of memory\n");
  751. return;
  752. }
  753. fd_init(&src_fd);
  754. if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
  755. {
  756. rt_free(block_ptr);
  757. rt_kprintf("Read %s failed\n", src);
  758. return;
  759. }
  760. fd_init(&fd);
  761. if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
  762. {
  763. rt_free(block_ptr);
  764. dfs_file_close(&src_fd);
  765. rt_kprintf("Write %s failed\n", dst);
  766. return;
  767. }
  768. do
  769. {
  770. read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
  771. if (read_bytes > 0)
  772. {
  773. int length;
  774. length = dfs_file_write(&fd, block_ptr, read_bytes);
  775. if (length != read_bytes)
  776. {
  777. /* write failed. */
  778. rt_kprintf("Write file data failed, errno=%d\n", length);
  779. break;
  780. }
  781. }
  782. }
  783. while (read_bytes > 0);
  784. dfs_file_close(&src_fd);
  785. dfs_file_close(&fd);
  786. rt_free(block_ptr);
  787. }
  788. extern int mkdir(const char *path, mode_t mode);
  789. static void copydir(const char *src, const char *dst)
  790. {
  791. struct dirent dirent;
  792. struct stat stat;
  793. int length;
  794. struct dfs_fd cpfd;
  795. if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
  796. {
  797. rt_kprintf("open %s failed\n", src);
  798. return ;
  799. }
  800. do
  801. {
  802. rt_memset(&dirent, 0, sizeof(struct dirent));
  803. length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
  804. if (length > 0)
  805. {
  806. char *src_entry_full = NULL;
  807. char *dst_entry_full = NULL;
  808. if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
  809. continue;
  810. /* build full path for each file */
  811. if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
  812. {
  813. rt_kprintf("out of memory!\n");
  814. break;
  815. }
  816. if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
  817. {
  818. rt_kprintf("out of memory!\n");
  819. rt_free(src_entry_full);
  820. break;
  821. }
  822. rt_memset(&stat, 0, sizeof(struct stat));
  823. if (dfs_file_stat(src_entry_full, &stat) != 0)
  824. {
  825. rt_kprintf("open file: %s failed\n", dirent.d_name);
  826. continue;
  827. }
  828. if (S_ISDIR(stat.st_mode))
  829. {
  830. mkdir(dst_entry_full, 0);
  831. copydir(src_entry_full, dst_entry_full);
  832. }
  833. else
  834. {
  835. copyfile(src_entry_full, dst_entry_full);
  836. }
  837. rt_free(src_entry_full);
  838. rt_free(dst_entry_full);
  839. }
  840. }
  841. while (length > 0);
  842. dfs_file_close(&cpfd);
  843. }
  844. static const char *_get_path_lastname(const char *path)
  845. {
  846. char *ptr;
  847. if ((ptr = (char *)strrchr(path, '/')) == NULL)
  848. return path;
  849. /* skip the '/' then return */
  850. return ++ptr;
  851. }
  852. void copy(const char *src, const char *dst)
  853. {
  854. #define FLAG_SRC_TYPE 0x03
  855. #define FLAG_SRC_IS_DIR 0x01
  856. #define FLAG_SRC_IS_FILE 0x02
  857. #define FLAG_SRC_NON_EXSIT 0x00
  858. #define FLAG_DST_TYPE 0x0C
  859. #define FLAG_DST_IS_DIR 0x04
  860. #define FLAG_DST_IS_FILE 0x08
  861. #define FLAG_DST_NON_EXSIT 0x00
  862. struct stat stat;
  863. uint32_t flag = 0;
  864. /* check the staus of src and dst */
  865. if (dfs_file_stat(src, &stat) < 0)
  866. {
  867. rt_kprintf("copy failed, bad %s\n", src);
  868. return;
  869. }
  870. if (S_ISDIR(stat.st_mode))
  871. flag |= FLAG_SRC_IS_DIR;
  872. else
  873. flag |= FLAG_SRC_IS_FILE;
  874. if (dfs_file_stat(dst, &stat) < 0)
  875. {
  876. flag |= FLAG_DST_NON_EXSIT;
  877. }
  878. else
  879. {
  880. if (S_ISDIR(stat.st_mode))
  881. flag |= FLAG_DST_IS_DIR;
  882. else
  883. flag |= FLAG_DST_IS_FILE;
  884. }
  885. //2. check status
  886. if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
  887. {
  888. rt_kprintf("cp faild, cp dir to file is not permitted!\n");
  889. return ;
  890. }
  891. //3. do copy
  892. if (flag & FLAG_SRC_IS_FILE)
  893. {
  894. if (flag & FLAG_DST_IS_DIR)
  895. {
  896. char *fdst;
  897. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  898. if (fdst == NULL)
  899. {
  900. rt_kprintf("out of memory\n");
  901. return;
  902. }
  903. copyfile(src, fdst);
  904. rt_free(fdst);
  905. }
  906. else
  907. {
  908. copyfile(src, dst);
  909. }
  910. }
  911. else //flag & FLAG_SRC_IS_DIR
  912. {
  913. if (flag & FLAG_DST_IS_DIR)
  914. {
  915. char *fdst;
  916. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  917. if (fdst == NULL)
  918. {
  919. rt_kprintf("out of memory\n");
  920. return;
  921. }
  922. mkdir(fdst, 0);
  923. copydir(src, fdst);
  924. rt_free(fdst);
  925. }
  926. else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
  927. {
  928. mkdir(dst, 0);
  929. copydir(src, dst);
  930. }
  931. else
  932. {
  933. copydir(src, dst);
  934. }
  935. }
  936. }
  937. FINSH_FUNCTION_EXPORT(copy, copy file or dir)
  938. #endif /* DFS_USING_POSIX */
  939. #endif /* RT_USING_FINSH */
  940. /* @} */