dfs_file.c 13 KB

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