1
0

dfs_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 buffer.
  137. *
  138. * @param fd the file descriptor.
  139. * @param buf the buffer to save the read data.
  140. * @param len the length of data buffer to be read.
  141. *
  142. * @return the actual read data bytes or 0 on end of file or failed.
  143. */
  144. int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
  145. {
  146. struct dfs_filesystem *fs;
  147. int result = 0;
  148. if (fd == RT_NULL)
  149. return -DFS_STATUS_EINVAL;
  150. fs = (struct dfs_filesystem *)fd->fs;
  151. if (fs->ops->read == RT_NULL)
  152. return -DFS_STATUS_ENOSYS;
  153. if ((result = fs->ops->read(fd, buf, len)) < 0)
  154. fd->flags |= DFS_F_EOF;
  155. return result;
  156. }
  157. /**
  158. * this function will fetch directory entries from a directory descriptor.
  159. *
  160. * @param fd the directory descriptor.
  161. * @param dirp the dirent buffer to save result.
  162. * @param nbytes the available room in the buffer.
  163. *
  164. * @return the read dirent, others on failed.
  165. */
  166. int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes)
  167. {
  168. struct dfs_filesystem *fs;
  169. /* parameter check */
  170. if (fd == RT_NULL || fd->type != FT_DIRECTORY)
  171. return -DFS_STATUS_EINVAL;
  172. fs = (struct dfs_filesystem *)fd->fs;
  173. if (fs->ops->getdents != RT_NULL)
  174. return fs->ops->getdents(fd, dirp, nbytes);
  175. return -DFS_STATUS_ENOSYS;
  176. }
  177. /**
  178. * this function will unlink (remove) a specified path file from file system.
  179. *
  180. * @param path the specified path file to be unlinked.
  181. *
  182. * @return 0 on successful, -1 on failed.
  183. */
  184. int dfs_file_unlink(const char *path)
  185. {
  186. int result;
  187. char *fullpath;
  188. struct dfs_filesystem *fs;
  189. result = DFS_STATUS_OK;
  190. /* Make sure we have an absolute path */
  191. fullpath = dfs_normalize_path(RT_NULL, path);
  192. if (fullpath == RT_NULL)
  193. {
  194. return -DFS_STATUS_EINVAL;
  195. }
  196. /* get filesystem */
  197. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  198. {
  199. result = -DFS_STATUS_ENOENT;
  200. goto __exit;
  201. }
  202. /* Check whether file is already open */
  203. if (fd_is_open(fullpath) == 0)
  204. {
  205. result = -DFS_STATUS_EBUSY;
  206. goto __exit;
  207. }
  208. if (fs->ops->unlink != RT_NULL)
  209. {
  210. if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
  211. {
  212. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  213. result = fs->ops->unlink(fs, "/");
  214. else
  215. result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
  216. }
  217. else
  218. result = fs->ops->unlink(fs, fullpath);
  219. }
  220. else result = -DFS_STATUS_ENOSYS;
  221. __exit:
  222. rt_free(fullpath);
  223. return result;
  224. }
  225. /**
  226. * this function will write some specified length data to file system.
  227. *
  228. * @param fd the file descriptor.
  229. * @param buf the data buffer to be written.
  230. * @param len the data buffer length
  231. *
  232. * @return the actual written data length.
  233. */
  234. int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len)
  235. {
  236. struct dfs_filesystem *fs;
  237. if (fd == RT_NULL)
  238. return -DFS_STATUS_EINVAL;
  239. fs = fd->fs;
  240. if (fs->ops->write == RT_NULL)
  241. return -DFS_STATUS_ENOSYS;
  242. return fs->ops->write(fd, buf, len);
  243. }
  244. /**
  245. * this function will flush buffer on a file descriptor.
  246. *
  247. * @param fd the file descriptor.
  248. *
  249. * @return 0 on successful, -1 on failed.
  250. */
  251. int dfs_file_flush(struct dfs_fd *fd)
  252. {
  253. struct dfs_filesystem *fs;
  254. if (fd == RT_NULL)
  255. return -DFS_STATUS_EINVAL;
  256. fs = fd->fs;
  257. if (fs->ops->flush == RT_NULL)
  258. return -DFS_STATUS_ENOSYS;
  259. return fs->ops->flush(fd);
  260. }
  261. /**
  262. * this function will seek the offset for specified file descriptor.
  263. *
  264. * @param fd the file descriptor.
  265. * @param offset the offset to be sought.
  266. *
  267. * @return the current position after seek.
  268. */
  269. int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
  270. {
  271. int result;
  272. struct dfs_filesystem *fs = fd->fs;
  273. if (fd == RT_NULL)
  274. return -DFS_STATUS_EINVAL;
  275. if (fs->ops->lseek == RT_NULL)
  276. return -DFS_STATUS_ENOSYS;
  277. result = fs->ops->lseek(fd, offset);
  278. /* update current position */
  279. if (result >= 0)
  280. fd->pos = result;
  281. return result;
  282. }
  283. /**
  284. * this function will get file information.
  285. *
  286. * @param path the file path.
  287. * @param buf the data buffer to save stat description.
  288. *
  289. * @return 0 on successful, -1 on failed.
  290. */
  291. int dfs_file_stat(const char *path, struct stat *buf)
  292. {
  293. int result;
  294. char *fullpath;
  295. struct dfs_filesystem *fs;
  296. fullpath = dfs_normalize_path(RT_NULL, path);
  297. if (fullpath == RT_NULL)
  298. {
  299. return -1;
  300. }
  301. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  302. {
  303. dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
  304. rt_free(fullpath);
  305. return -DFS_STATUS_ENOENT;
  306. }
  307. if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
  308. (dfs_subdir(fs->path, fullpath) == RT_NULL))
  309. {
  310. /* it's the root directory */
  311. buf->st_dev = 0;
  312. buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  313. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  314. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  315. buf->st_size = 0;
  316. buf->st_mtime = 0;
  317. buf->st_blksize = 512;
  318. /* release full path */
  319. rt_free(fullpath);
  320. return DFS_STATUS_OK;
  321. }
  322. else
  323. {
  324. if (fs->ops->stat == RT_NULL)
  325. {
  326. rt_free(fullpath);
  327. dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
  328. return -DFS_STATUS_ENOSYS;
  329. }
  330. /* get the real file path and get file stat */
  331. if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
  332. result = fs->ops->stat(fs, fullpath, buf);
  333. else
  334. result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
  335. }
  336. rt_free(fullpath);
  337. return result;
  338. }
  339. /**
  340. * this function will rename an old path name to a new path name.
  341. *
  342. * @param oldpath the old path name.
  343. * @param newpath the new path name.
  344. *
  345. * @return 0 on successful, -1 on failed.
  346. */
  347. int dfs_file_rename(const char *oldpath, const char *newpath)
  348. {
  349. int result;
  350. struct dfs_filesystem *oldfs, *newfs;
  351. char *oldfullpath, *newfullpath;
  352. result = DFS_STATUS_OK;
  353. newfullpath = RT_NULL;
  354. oldfullpath = RT_NULL;
  355. oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
  356. if (oldfullpath == RT_NULL)
  357. {
  358. result = -DFS_STATUS_ENOENT;
  359. goto __exit;
  360. }
  361. newfullpath = dfs_normalize_path(RT_NULL, newpath);
  362. if (newfullpath == RT_NULL)
  363. {
  364. result = -DFS_STATUS_ENOENT;
  365. goto __exit;
  366. }
  367. oldfs = dfs_filesystem_lookup(oldfullpath);
  368. newfs = dfs_filesystem_lookup(newfullpath);
  369. if (oldfs == newfs)
  370. {
  371. if (oldfs->ops->rename == RT_NULL)
  372. {
  373. result = -DFS_STATUS_ENOSYS;
  374. }
  375. else
  376. {
  377. if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
  378. result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
  379. else
  380. /* use sub directory to rename in file system */
  381. result = oldfs->ops->rename(oldfs, dfs_subdir(oldfs->path, oldfullpath),
  382. dfs_subdir(newfs->path, newfullpath));
  383. }
  384. }
  385. else
  386. {
  387. result = -DFS_STATUS_EXDEV;
  388. }
  389. __exit:
  390. rt_free(oldfullpath);
  391. rt_free(newfullpath);
  392. /* not at same file system, return EXDEV */
  393. return result;
  394. }
  395. #ifdef RT_USING_FINSH
  396. #include <finsh.h>
  397. static struct dfs_fd fd;
  398. static struct dirent dirent;
  399. void ls(const char *pathname)
  400. {
  401. struct stat stat;
  402. int length;
  403. char *fullpath, *path;
  404. fullpath = RT_NULL;
  405. if (pathname == RT_NULL)
  406. {
  407. #ifdef DFS_USING_WORKDIR
  408. /* open current working directory */
  409. path = rt_strdup(working_directory);
  410. #else
  411. path = rt_strdup("/");
  412. #endif
  413. if (path == RT_NULL) return ; /* out of memory */
  414. }
  415. else
  416. {
  417. path = (char *)pathname;
  418. }
  419. /* list directory */
  420. if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
  421. {
  422. rt_kprintf("Directory %s:\n", path);
  423. do
  424. {
  425. rt_memset(&dirent, 0, sizeof(struct dirent));
  426. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  427. if (length > 0)
  428. {
  429. rt_memset(&stat, 0, sizeof(struct stat));
  430. /* build full path for each file */
  431. fullpath = dfs_normalize_path(path, dirent.d_name);
  432. if (fullpath == RT_NULL)
  433. break;
  434. if (dfs_file_stat(fullpath, &stat) == 0)
  435. {
  436. rt_kprintf("%-20s", dirent.d_name);
  437. if ( DFS_S_ISDIR(stat.st_mode))
  438. {
  439. rt_kprintf("%-25s\n", "<DIR>");
  440. }
  441. else
  442. {
  443. rt_kprintf("%-25lu\n", stat.st_size);
  444. }
  445. }
  446. else
  447. rt_kprintf("BAD file: %s\n", dirent.d_name);
  448. rt_free(fullpath);
  449. }
  450. }while(length > 0);
  451. dfs_file_close(&fd);
  452. }
  453. else
  454. {
  455. rt_kprintf("No such directory\n");
  456. }
  457. if (pathname == RT_NULL)
  458. rt_free(path);
  459. }
  460. FINSH_FUNCTION_EXPORT(ls, list directory contents)
  461. void rm(const char *filename)
  462. {
  463. if (dfs_file_unlink(filename) < 0)
  464. {
  465. rt_kprintf("Delete %s failed\n", filename);
  466. }
  467. }
  468. FINSH_FUNCTION_EXPORT(rm, remove files or directories)
  469. void cat(const char* filename)
  470. {
  471. rt_uint32_t length;
  472. char buffer[81];
  473. if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
  474. {
  475. rt_kprintf("Open %s failed\n", filename);
  476. return;
  477. }
  478. do
  479. {
  480. rt_memset(buffer, 0, sizeof(buffer));
  481. length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
  482. if (length > 0)
  483. {
  484. rt_kprintf("%s", buffer);
  485. }
  486. }while (length > 0);
  487. dfs_file_close(&fd);
  488. }
  489. FINSH_FUNCTION_EXPORT(cat, print file)
  490. #define BUF_SZ 4096
  491. void copy(const char *src, const char *dst)
  492. {
  493. struct dfs_fd src_fd;
  494. rt_uint8_t *block_ptr;
  495. rt_uint32_t read_bytes;
  496. block_ptr = rt_malloc(BUF_SZ);
  497. if (block_ptr == RT_NULL)
  498. {
  499. rt_kprintf("out of memory\n");
  500. return;
  501. }
  502. if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
  503. {
  504. rt_free(block_ptr);
  505. rt_kprintf("Read %s failed\n", src);
  506. return;
  507. }
  508. if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
  509. {
  510. rt_free(block_ptr);
  511. dfs_file_close(&src_fd);
  512. rt_kprintf("Write %s failed\n", dst);
  513. return;
  514. }
  515. do
  516. {
  517. read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
  518. if (read_bytes > 0)
  519. {
  520. dfs_file_write(&fd, block_ptr, read_bytes);
  521. }
  522. } while (read_bytes > 0);
  523. dfs_file_close(&src_fd);
  524. dfs_file_close(&fd);
  525. rt_free(block_ptr);
  526. }
  527. FINSH_FUNCTION_EXPORT(copy, copy source file to destination file)
  528. #endif
  529. /* @} */