dfs_file.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. dfs_file_close(&fd);
  736. }
  737. FINSH_FUNCTION_EXPORT(cat, print file);
  738. #ifdef DFS_USING_POSIX
  739. #define BUF_SZ 4096
  740. static void copyfile(const char *src, const char *dst)
  741. {
  742. struct dfs_fd fd;
  743. struct dfs_fd src_fd;
  744. rt_uint8_t *block_ptr;
  745. rt_int32_t read_bytes;
  746. block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
  747. if (block_ptr == NULL)
  748. {
  749. rt_kprintf("out of memory\n");
  750. return;
  751. }
  752. fd_init(&src_fd);
  753. if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
  754. {
  755. rt_free(block_ptr);
  756. rt_kprintf("Read %s failed\n", src);
  757. return;
  758. }
  759. fd_init(&fd);
  760. if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
  761. {
  762. rt_free(block_ptr);
  763. dfs_file_close(&src_fd);
  764. rt_kprintf("Write %s failed\n", dst);
  765. return;
  766. }
  767. do
  768. {
  769. read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
  770. if (read_bytes > 0)
  771. {
  772. int length;
  773. length = dfs_file_write(&fd, block_ptr, read_bytes);
  774. if (length != read_bytes)
  775. {
  776. /* write failed. */
  777. rt_kprintf("Write file data failed, errno=%d\n", length);
  778. break;
  779. }
  780. }
  781. }
  782. while (read_bytes > 0);
  783. dfs_file_close(&src_fd);
  784. dfs_file_close(&fd);
  785. rt_free(block_ptr);
  786. }
  787. extern int mkdir(const char *path, mode_t mode);
  788. static void copydir(const char *src, const char *dst)
  789. {
  790. struct dirent dirent;
  791. struct stat stat;
  792. int length;
  793. struct dfs_fd cpfd;
  794. if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
  795. {
  796. rt_kprintf("open %s failed\n", src);
  797. return ;
  798. }
  799. do
  800. {
  801. rt_memset(&dirent, 0, sizeof(struct dirent));
  802. length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
  803. if (length > 0)
  804. {
  805. char *src_entry_full = NULL;
  806. char *dst_entry_full = NULL;
  807. if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
  808. continue;
  809. /* build full path for each file */
  810. if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
  811. {
  812. rt_kprintf("out of memory!\n");
  813. break;
  814. }
  815. if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
  816. {
  817. rt_kprintf("out of memory!\n");
  818. rt_free(src_entry_full);
  819. break;
  820. }
  821. rt_memset(&stat, 0, sizeof(struct stat));
  822. if (dfs_file_stat(src_entry_full, &stat) != 0)
  823. {
  824. rt_kprintf("open file: %s failed\n", dirent.d_name);
  825. continue;
  826. }
  827. if (S_ISDIR(stat.st_mode))
  828. {
  829. mkdir(dst_entry_full, 0);
  830. copydir(src_entry_full, dst_entry_full);
  831. }
  832. else
  833. {
  834. copyfile(src_entry_full, dst_entry_full);
  835. }
  836. rt_free(src_entry_full);
  837. rt_free(dst_entry_full);
  838. }
  839. }
  840. while (length > 0);
  841. dfs_file_close(&cpfd);
  842. }
  843. static const char *_get_path_lastname(const char *path)
  844. {
  845. char *ptr;
  846. if ((ptr = (char *)strrchr(path, '/')) == NULL)
  847. return path;
  848. /* skip the '/' then return */
  849. return ++ptr;
  850. }
  851. void copy(const char *src, const char *dst)
  852. {
  853. #define FLAG_SRC_TYPE 0x03
  854. #define FLAG_SRC_IS_DIR 0x01
  855. #define FLAG_SRC_IS_FILE 0x02
  856. #define FLAG_SRC_NON_EXSIT 0x00
  857. #define FLAG_DST_TYPE 0x0C
  858. #define FLAG_DST_IS_DIR 0x04
  859. #define FLAG_DST_IS_FILE 0x08
  860. #define FLAG_DST_NON_EXSIT 0x00
  861. struct stat stat;
  862. uint32_t flag = 0;
  863. /* check the staus of src and dst */
  864. if (dfs_file_stat(src, &stat) < 0)
  865. {
  866. rt_kprintf("copy failed, bad %s\n", src);
  867. return;
  868. }
  869. if (S_ISDIR(stat.st_mode))
  870. flag |= FLAG_SRC_IS_DIR;
  871. else
  872. flag |= FLAG_SRC_IS_FILE;
  873. if (dfs_file_stat(dst, &stat) < 0)
  874. {
  875. flag |= FLAG_DST_NON_EXSIT;
  876. }
  877. else
  878. {
  879. if (S_ISDIR(stat.st_mode))
  880. flag |= FLAG_DST_IS_DIR;
  881. else
  882. flag |= FLAG_DST_IS_FILE;
  883. }
  884. //2. check status
  885. if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
  886. {
  887. rt_kprintf("cp faild, cp dir to file is not permitted!\n");
  888. return ;
  889. }
  890. //3. do copy
  891. if (flag & FLAG_SRC_IS_FILE)
  892. {
  893. if (flag & FLAG_DST_IS_DIR)
  894. {
  895. char *fdst;
  896. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  897. if (fdst == NULL)
  898. {
  899. rt_kprintf("out of memory\n");
  900. return;
  901. }
  902. copyfile(src, fdst);
  903. rt_free(fdst);
  904. }
  905. else
  906. {
  907. copyfile(src, dst);
  908. }
  909. }
  910. else //flag & FLAG_SRC_IS_DIR
  911. {
  912. if (flag & FLAG_DST_IS_DIR)
  913. {
  914. char *fdst;
  915. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  916. if (fdst == NULL)
  917. {
  918. rt_kprintf("out of memory\n");
  919. return;
  920. }
  921. mkdir(fdst, 0);
  922. copydir(src, fdst);
  923. rt_free(fdst);
  924. }
  925. else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
  926. {
  927. mkdir(dst, 0);
  928. copydir(src, dst);
  929. }
  930. else
  931. {
  932. copydir(src, dst);
  933. }
  934. }
  935. }
  936. FINSH_FUNCTION_EXPORT(copy, copy file or dir)
  937. #endif /* DFS_USING_POSIX */
  938. #endif /* RT_USING_FINSH */
  939. /* @} */