dfs_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * File : dfs_file.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2005-02-22 Bernard The first version.
  13. * 2011-12-08 Bernard Merges rename patch from iamcacy.
  14. */
  15. #include <dfs.h>
  16. #include <dfs_file.h>
  17. /**
  18. * @addtogroup FileApi
  19. */
  20. /*@{*/
  21. /**
  22. * this function will open a file which specified by path with specified flags.
  23. *
  24. * @param fd the file descriptor pointer to return the corresponding result.
  25. * @param path the specified file path.
  26. * @param flags the flags for open operator.
  27. *
  28. * @return 0 on successful, -1 on failed.
  29. */
  30. int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
  31. {
  32. struct dfs_filesystem *fs;
  33. char *fullpath;
  34. int result;
  35. /* parameter check */
  36. if (fd == RT_NULL)
  37. return -DFS_STATUS_EINVAL;
  38. /* make sure we have an absolute path */
  39. fullpath = dfs_normalize_path(RT_NULL, path);
  40. if (fullpath == RT_NULL)
  41. {
  42. return -1;
  43. }
  44. dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));
  45. /* find filesystem */
  46. fs = dfs_filesystem_lookup(fullpath);
  47. if (fs == RT_NULL)
  48. {
  49. rt_free(fullpath); /* release path */
  50. return -DFS_STATUS_ENOENT;
  51. }
  52. dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
  53. fd->fs = fs;
  54. /* initialize the fd item */
  55. fd->type = FT_REGULAR;
  56. fd->flags = flags;
  57. fd->size = 0;
  58. fd->pos = 0;
  59. if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
  60. {
  61. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  62. fd->path = rt_strdup("/");
  63. else
  64. fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
  65. rt_free(fullpath);
  66. dfs_log(DFS_DEBUG_INFO, ("Actual file path: %s\n", fd->path));
  67. }
  68. else
  69. {
  70. fd->path = fullpath;
  71. }
  72. /* specific file system open routine */
  73. if (fs->ops->open == RT_NULL)
  74. {
  75. /* clear fd */
  76. rt_free(fd->path);
  77. rt_memset(fd, 0, sizeof(*fd));
  78. return -DFS_STATUS_ENOSYS;
  79. }
  80. if ((result = fs->ops->open(fd)) < 0)
  81. {
  82. /* clear fd */
  83. rt_free(fd->path);
  84. rt_memset(fd, 0, sizeof(*fd));
  85. dfs_log(DFS_DEBUG_INFO, ("open failed"));
  86. return result;
  87. }
  88. fd->flags |= DFS_F_OPEN;
  89. if (flags & DFS_O_DIRECTORY)
  90. {
  91. fd->type = FT_DIRECTORY;
  92. fd->flags |= DFS_F_DIRECTORY;
  93. }
  94. dfs_log(DFS_DEBUG_INFO, ("open successful"));
  95. return 0;
  96. }
  97. /**
  98. * this function will close a file descriptor.
  99. *
  100. * @param fd the file descriptor to be closed.
  101. *
  102. * @return 0 on successful, -1 on failed.
  103. */
  104. int dfs_file_close(struct dfs_fd *fd)
  105. {
  106. int result = 0;
  107. if (fd != RT_NULL && fd->fs->ops->close != RT_NULL)
  108. result = fd->fs->ops->close(fd);
  109. /* close fd error, return */
  110. if (result < 0)
  111. return result;
  112. rt_free(fd->path);
  113. rt_memset(fd, 0, sizeof(struct dfs_fd));
  114. return result;
  115. }
  116. /**
  117. * this function will perform a io control on a file descriptor.
  118. *
  119. * @param fd the file descriptor.
  120. * @param cmd the command to send to file descriptor.
  121. * @param args the argument to send to file descriptor.
  122. *
  123. * @return 0 on successful, -1 on failed.
  124. */
  125. int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
  126. {
  127. struct dfs_filesystem *fs;
  128. if (fd == RT_NULL || fd->type != FT_REGULAR)
  129. return -DFS_STATUS_EINVAL;
  130. fs = fd->fs;
  131. if (fs->ops->ioctl != RT_NULL)
  132. return fs->ops->ioctl(fd, cmd, args);
  133. return -DFS_STATUS_ENOSYS;
  134. }
  135. /**
  136. * this function will read specified length data from a file descriptor to a
  137. * buffer.
  138. *
  139. * @param fd the file descriptor.
  140. * @param buf the buffer to save the read data.
  141. * @param len the length of data buffer to be read.
  142. *
  143. * @return the actual read data bytes or 0 on end of file or failed.
  144. */
  145. int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
  146. {
  147. struct dfs_filesystem *fs;
  148. int result = 0;
  149. if (fd == RT_NULL)
  150. return -DFS_STATUS_EINVAL;
  151. fs = (struct dfs_filesystem *)fd->fs;
  152. if (fs->ops->read == RT_NULL)
  153. return -DFS_STATUS_ENOSYS;
  154. if ((result = fs->ops->read(fd, buf, len)) < 0)
  155. fd->flags |= DFS_F_EOF;
  156. return result;
  157. }
  158. /**
  159. * this function will fetch directory entries from a directory descriptor.
  160. *
  161. * @param fd the directory descriptor.
  162. * @param dirp the dirent buffer to save result.
  163. * @param nbytes the available room in the buffer.
  164. *
  165. * @return the read dirent, others on failed.
  166. */
  167. int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes)
  168. {
  169. struct dfs_filesystem *fs;
  170. /* parameter check */
  171. if (fd == RT_NULL || fd->type != FT_DIRECTORY)
  172. return -DFS_STATUS_EINVAL;
  173. fs = (struct dfs_filesystem *)fd->fs;
  174. if (fs->ops->getdents != RT_NULL)
  175. return fs->ops->getdents(fd, dirp, nbytes);
  176. return -DFS_STATUS_ENOSYS;
  177. }
  178. /**
  179. * this function will unlink (remove) a specified path file from file system.
  180. *
  181. * @param path the specified path file to be unlinked.
  182. *
  183. * @return 0 on successful, -1 on failed.
  184. */
  185. int dfs_file_unlink(const char *path)
  186. {
  187. int result;
  188. char *fullpath;
  189. struct dfs_filesystem *fs;
  190. result = DFS_STATUS_OK;
  191. /* Make sure we have an absolute path */
  192. fullpath = dfs_normalize_path(RT_NULL, path);
  193. if (fullpath == RT_NULL)
  194. {
  195. return -DFS_STATUS_EINVAL;
  196. }
  197. /* get filesystem */
  198. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  199. {
  200. result = -DFS_STATUS_ENOENT;
  201. goto __exit;
  202. }
  203. /* Check whether file is already open */
  204. if (fd_is_open(fullpath) == 0)
  205. {
  206. result = -DFS_STATUS_EBUSY;
  207. goto __exit;
  208. }
  209. if (fs->ops->unlink != RT_NULL)
  210. {
  211. if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
  212. {
  213. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  214. result = fs->ops->unlink(fs, "/");
  215. else
  216. result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
  217. }
  218. else
  219. result = fs->ops->unlink(fs, fullpath);
  220. }
  221. else result = -DFS_STATUS_ENOSYS;
  222. __exit:
  223. rt_free(fullpath);
  224. return result;
  225. }
  226. /**
  227. * this function will write some specified length data to file system.
  228. *
  229. * @param fd the file descriptor.
  230. * @param buf the data buffer to be written.
  231. * @param len the data buffer length
  232. *
  233. * @return the actual written data length.
  234. */
  235. int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len)
  236. {
  237. struct dfs_filesystem *fs;
  238. if (fd == RT_NULL)
  239. return -DFS_STATUS_EINVAL;
  240. fs = fd->fs;
  241. if (fs->ops->write == RT_NULL)
  242. return -DFS_STATUS_ENOSYS;
  243. return fs->ops->write(fd, buf, len);
  244. }
  245. /**
  246. * this function will flush buffer on a file descriptor.
  247. *
  248. * @param fd the file descriptor.
  249. *
  250. * @return 0 on successful, -1 on failed.
  251. */
  252. int dfs_file_flush(struct dfs_fd *fd)
  253. {
  254. struct dfs_filesystem *fs;
  255. if (fd == RT_NULL)
  256. return -DFS_STATUS_EINVAL;
  257. fs = fd->fs;
  258. if (fs->ops->flush == RT_NULL)
  259. return -DFS_STATUS_ENOSYS;
  260. return fs->ops->flush(fd);
  261. }
  262. /**
  263. * this function will seek the offset for specified file descriptor.
  264. *
  265. * @param fd the file descriptor.
  266. * @param offset the offset to be sought.
  267. *
  268. * @return the current position after seek.
  269. */
  270. int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
  271. {
  272. int result;
  273. struct dfs_filesystem *fs = fd->fs;
  274. if (fd == RT_NULL)
  275. return -DFS_STATUS_EINVAL;
  276. if (fs->ops->lseek == RT_NULL)
  277. return -DFS_STATUS_ENOSYS;
  278. result = fs->ops->lseek(fd, offset);
  279. /* update current position */
  280. if (result >= 0)
  281. fd->pos = result;
  282. return result;
  283. }
  284. /**
  285. * this function will get file information.
  286. *
  287. * @param path the file path.
  288. * @param buf the data buffer to save stat description.
  289. *
  290. * @return 0 on successful, -1 on failed.
  291. */
  292. int dfs_file_stat(const char *path, struct stat *buf)
  293. {
  294. int result;
  295. char *fullpath;
  296. struct dfs_filesystem *fs;
  297. fullpath = dfs_normalize_path(RT_NULL, path);
  298. if (fullpath == RT_NULL)
  299. {
  300. return -1;
  301. }
  302. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  303. {
  304. dfs_log(DFS_DEBUG_ERROR,
  305. ("can't find mounted filesystem on this path:%s", fullpath));
  306. rt_free(fullpath);
  307. return -DFS_STATUS_ENOENT;
  308. }
  309. if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
  310. (dfs_subdir(fs->path, fullpath) == RT_NULL))
  311. {
  312. /* it's the root directory */
  313. buf->st_dev = 0;
  314. buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  315. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  316. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  317. buf->st_size = 0;
  318. buf->st_mtime = 0;
  319. buf->st_blksize = 512;
  320. /* release full path */
  321. rt_free(fullpath);
  322. return DFS_STATUS_OK;
  323. }
  324. else
  325. {
  326. if (fs->ops->stat == RT_NULL)
  327. {
  328. rt_free(fullpath);
  329. dfs_log(DFS_DEBUG_ERROR,
  330. ("the filesystem didn't implement this function"));
  331. return -DFS_STATUS_ENOSYS;
  332. }
  333. /* get the real file path and get file stat */
  334. if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
  335. result = fs->ops->stat(fs, fullpath, buf);
  336. else
  337. result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
  338. }
  339. rt_free(fullpath);
  340. return result;
  341. }
  342. /**
  343. * this function will rename an old path name to a new path name.
  344. *
  345. * @param oldpath the old path name.
  346. * @param newpath the new path name.
  347. *
  348. * @return 0 on successful, -1 on failed.
  349. */
  350. int dfs_file_rename(const char *oldpath, const char *newpath)
  351. {
  352. int result;
  353. struct dfs_filesystem *oldfs, *newfs;
  354. char *oldfullpath, *newfullpath;
  355. result = DFS_STATUS_OK;
  356. newfullpath = RT_NULL;
  357. oldfullpath = RT_NULL;
  358. oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
  359. if (oldfullpath == RT_NULL)
  360. {
  361. result = -DFS_STATUS_ENOENT;
  362. goto __exit;
  363. }
  364. newfullpath = dfs_normalize_path(RT_NULL, newpath);
  365. if (newfullpath == RT_NULL)
  366. {
  367. result = -DFS_STATUS_ENOENT;
  368. goto __exit;
  369. }
  370. oldfs = dfs_filesystem_lookup(oldfullpath);
  371. newfs = dfs_filesystem_lookup(newfullpath);
  372. if (oldfs == newfs)
  373. {
  374. if (oldfs->ops->rename == RT_NULL)
  375. {
  376. result = -DFS_STATUS_ENOSYS;
  377. }
  378. else
  379. {
  380. if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
  381. result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
  382. else
  383. /* use sub directory to rename in file system */
  384. result = oldfs->ops->rename(oldfs,
  385. dfs_subdir(oldfs->path, oldfullpath),
  386. dfs_subdir(newfs->path, newfullpath));
  387. }
  388. }
  389. else
  390. {
  391. result = -DFS_STATUS_EXDEV;
  392. }
  393. __exit:
  394. rt_free(oldfullpath);
  395. rt_free(newfullpath);
  396. /* not at same file system, return EXDEV */
  397. return result;
  398. }
  399. #ifdef RT_USING_FINSH
  400. #include <finsh.h>
  401. static struct dfs_fd fd;
  402. static struct dirent dirent;
  403. void ls(const char *pathname)
  404. {
  405. struct stat stat;
  406. int length;
  407. char *fullpath, *path;
  408. fullpath = RT_NULL;
  409. if (pathname == RT_NULL)
  410. {
  411. #ifdef DFS_USING_WORKDIR
  412. /* open current working directory */
  413. path = rt_strdup(working_directory);
  414. #else
  415. path = rt_strdup("/");
  416. #endif
  417. if (path == RT_NULL)
  418. return ; /* out of memory */
  419. }
  420. else
  421. {
  422. path = (char *)pathname;
  423. }
  424. /* list directory */
  425. if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
  426. {
  427. rt_kprintf("Directory %s:\n", path);
  428. do
  429. {
  430. rt_memset(&dirent, 0, sizeof(struct dirent));
  431. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  432. if (length > 0)
  433. {
  434. rt_memset(&stat, 0, sizeof(struct stat));
  435. /* build full path for each file */
  436. fullpath = dfs_normalize_path(path, dirent.d_name);
  437. if (fullpath == RT_NULL)
  438. break;
  439. if (dfs_file_stat(fullpath, &stat) == 0)
  440. {
  441. rt_kprintf("%-20s", dirent.d_name);
  442. if ( DFS_S_ISDIR(stat.st_mode))
  443. {
  444. rt_kprintf("%-25s\n", "<DIR>");
  445. }
  446. else
  447. {
  448. rt_kprintf("%-25lu\n", stat.st_size);
  449. }
  450. }
  451. else
  452. rt_kprintf("BAD file: %s\n", dirent.d_name);
  453. rt_free(fullpath);
  454. }
  455. }while(length > 0);
  456. dfs_file_close(&fd);
  457. }
  458. else
  459. {
  460. rt_kprintf("No such directory\n");
  461. }
  462. if (pathname == RT_NULL)
  463. rt_free(path);
  464. }
  465. FINSH_FUNCTION_EXPORT(ls, list directory contents)
  466. void rm(const char *filename)
  467. {
  468. if (dfs_file_unlink(filename) < 0)
  469. {
  470. rt_kprintf("Delete %s failed\n", filename);
  471. }
  472. }
  473. FINSH_FUNCTION_EXPORT(rm, remove files or directories)
  474. void cat(const char* filename)
  475. {
  476. rt_uint32_t length;
  477. char buffer[81];
  478. if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
  479. {
  480. rt_kprintf("Open %s failed\n", filename);
  481. return;
  482. }
  483. do
  484. {
  485. rt_memset(buffer, 0, sizeof(buffer));
  486. length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
  487. if (length > 0)
  488. {
  489. rt_kprintf("%s", buffer);
  490. }
  491. }while (length > 0);
  492. dfs_file_close(&fd);
  493. }
  494. FINSH_FUNCTION_EXPORT(cat, print file)
  495. #define BUF_SZ 4096
  496. void copy(const char *src, const char *dst)
  497. {
  498. struct dfs_fd src_fd;
  499. rt_uint8_t *block_ptr;
  500. rt_uint32_t read_bytes;
  501. block_ptr = rt_malloc(BUF_SZ);
  502. if (block_ptr == RT_NULL)
  503. {
  504. rt_kprintf("out of memory\n");
  505. return;
  506. }
  507. if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
  508. {
  509. rt_free(block_ptr);
  510. rt_kprintf("Read %s failed\n", src);
  511. return;
  512. }
  513. if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
  514. {
  515. rt_free(block_ptr);
  516. dfs_file_close(&src_fd);
  517. rt_kprintf("Write %s failed\n", dst);
  518. return;
  519. }
  520. do
  521. {
  522. read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
  523. if (read_bytes > 0)
  524. {
  525. dfs_file_write(&fd, block_ptr, read_bytes);
  526. }
  527. } while (read_bytes > 0);
  528. dfs_file_close(&src_fd);
  529. dfs_file_close(&fd);
  530. rt_free(block_ptr);
  531. }
  532. FINSH_FUNCTION_EXPORT(copy, copy source file to destination file)
  533. #endif
  534. /* @} */