dfs_file.c 24 KB

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