dfs_jffs2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * File : rtthread.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2012, 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. * 2012-1-7 prife the first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include "cyg/infra/cyg_type.h"
  17. #include "cyg/fileio/fileio.h"
  18. #include "codes.h"
  19. #undef mode_t
  20. #include <dfs_fs.h>
  21. #include <dfs_def.h>
  22. #include "dfs_jffs2.h"
  23. #include "jffs2_config.h"
  24. #include "porting.h"
  25. #include <string.h>
  26. #if DEVICE_PART_MAX > 1
  27. #error "support only one jffs2 partition on a flash device!"
  28. #endif
  29. /* make sure the following struct var had been initilased to 0! */
  30. struct device_part
  31. {
  32. struct cyg_mtab_entry * mte;
  33. struct rt_mtd_device *dev;
  34. };
  35. static struct device_part device_partition[DEVICE_PART_MAX] = {0};
  36. #define jffs2_mount jffs2_fste.mount
  37. #define jffs2_umount jffs2_fste.umount
  38. #define jffs2_open jffs2_fste.open
  39. #define jffs2_file_unlink jffs2_fste.unlink
  40. #define jffs2_mkdir jffs2_fste.mkdir
  41. #define jffs2_rmdir jffs2_fste.rmdir
  42. #define jffs2_rename jffs2_fste.rename
  43. #define jffs2_link jffs2_fste.link
  44. #define jffs2_opendir jffs2_fste.opendir
  45. #define jffs2_chdir jffs2_fste.chdir
  46. #define jffs2_ops_stat jffs2_fste.stat
  47. #define jffs2_getinfo jffs2_fste.getinfo
  48. #define jffs2_setinfo jffs2_fste.setinfo
  49. #define jffs2_file_read jffs2_fileops.fo_read
  50. #define jffs2_file_write jffs2_fileops.fo_write
  51. #define jffs2_file_lseek jffs2_fileops.fo_lseek
  52. #define jffs2_file_ioctl jffs2_fileops.fo_ioctl
  53. #define jffs2_file_select jffs2_fileops.fo_select
  54. #define jffs2_file_fsync jffs2_fileops.fo_fsync
  55. #define jffs2_file_colse jffs2_fileops.fo_close
  56. #define jffs2_file_fstat jffs2_fileops.fo_fstat
  57. #define jffs2_file_getinfo jffs2_fileops.fo_getinfo
  58. #define jffs2_file_setinfo jffs2_fileops.fo_setinfo
  59. #define jffs2_dir_read jffs2_dirops.fo_read
  60. //#define jffs2_dir_write jffs2_dirops.fo_write
  61. #define jffs2_dir_lseek jffs2_dirops.fo_lseek
  62. //#define jffs2_dir_ioctl jffs2_dirops.fo_ioctl
  63. #define jffs2_dir_select jffs2_dirops.fo_select
  64. //#define jffs2_dir_fsync jffs2_dirops.fo_fsync
  65. #define jffs2_dir_colse jffs2_dirops.fo_close
  66. //#define jffs2_dir_fstat jffs2_dirops.fo_fstat
  67. //#define jffs2_dir_getinfo jffs2_dirops.fo_getinfo
  68. //#define jffs2_dir_setinfo jffs2_dirops.fo_setinfo
  69. /*
  70. * RT-Thread Device Interface for jffs2
  71. */
  72. /* these code is in src/flashio.c */
  73. static int jffs2_result_to_dfs(int result)
  74. {
  75. int status = -1;
  76. if (result < 0)
  77. result = -result;
  78. switch (result)
  79. {
  80. case ENOERR:/** no error */
  81. break;
  82. case EACCES:/** Tried to open read-only file for writing, or files sharing mode
  83. does not allow specified operations, or given path is directory */
  84. status = -DFS_STATUS_EINVAL;
  85. break;/* no suitable */
  86. case EEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
  87. status = -DFS_STATUS_EEXIST;
  88. break;
  89. case EINVAL: /** Invalid oflag or pmode argument */
  90. status = -DFS_STATUS_EINVAL;
  91. break;
  92. case EMFILE: /** No more file handles available(too many open files) */
  93. rt_kprintf("dfs_jffs2.c error: no more file handles available\r\n");
  94. status = -1;
  95. break;//fixme
  96. case ENOENT: /** file or path not found */
  97. status = -DFS_STATUS_ENOENT;
  98. break;
  99. case EBADF: /** invalid file handle */
  100. status = -DFS_STATUS_EBADF;
  101. break;
  102. case ENOMEM:/** no enough memory */
  103. status = -DFS_STATUS_ENOMEM;
  104. break;
  105. case EIO: /** I/O error from lower level flash operation */
  106. status = -DFS_STATUS_EIO;
  107. break;
  108. case ENOTDIR: /** Not a directory */
  109. status = -DFS_STATUS_ENOTDIR;
  110. break;
  111. case EISDIR: /** Is a directory */
  112. status = -DFS_STATUS_EISDIR;
  113. break;
  114. case ENOSPC: /**//* No space left on device */
  115. status = -DFS_STATUS_ENOSPC;
  116. break;
  117. default:
  118. rt_kprintf("dfs_jffs2.c error: %d\r\n", result);
  119. status = -1;
  120. break; /* unknown error! */
  121. }
  122. return status;
  123. }
  124. /*
  125. * RT-Thread DFS Interface for jffs2
  126. */
  127. static int dfs_jffs2_mount(struct dfs_filesystem* fs,
  128. unsigned long rwflag,
  129. const void* data)
  130. {
  131. unsigned index;
  132. struct cyg_mtab_entry * mte;
  133. int result;
  134. /* find a empty entry in partition table */
  135. for (index = 0; index < DEVICE_PART_MAX; index ++)
  136. {
  137. if (device_partition[index].dev == RT_NULL)
  138. break;
  139. }
  140. if (index == DEVICE_PART_MAX)
  141. return -DFS_STATUS_ENOSPC;
  142. mte = rt_malloc(sizeof(struct cyg_mtab_entry));
  143. if (mte == RT_NULL)
  144. return -DFS_STATUS_ENOMEM;
  145. mte->name = fs->path;
  146. mte->fsname = "jffs2";
  147. mte->devname = NULL;
  148. /* note that, i use mte->data to store rtt's device
  149. * while, in jffs2_mount, mte->data will be copy into
  150. * s_dev in struct super_block, and mte->data will be
  151. * filled with jffs2_sb(see the source of jffs2_mount.
  152. */
  153. mte->data = (CYG_ADDRWORD)fs->dev_id;
  154. device_partition[index].dev = RT_MTD_DEVICE(fs->dev_id);
  155. /* after jffs2_mount, mte->data will not be dev_id any more */
  156. result = jffs2_mount(NULL, mte);
  157. if (result != 0)
  158. {
  159. return jffs2_result_to_dfs(result);
  160. }
  161. /* save this pointer */
  162. device_partition[index].mte = mte;
  163. return 0;
  164. }
  165. static int _find_fs(struct cyg_mtab_entry ** mte, rt_device_t dev_id)
  166. {
  167. unsigned index;
  168. /* find device index */
  169. for (index = 0; index < DEVICE_PART_MAX; index++)
  170. {
  171. if (device_partition[index].dev == RT_MTD_DEVICE(dev_id))
  172. {
  173. *mte = device_partition[index].mte;
  174. return 0;
  175. }
  176. }
  177. rt_kprintf("error, could not found the fs!");
  178. return -1;
  179. }
  180. static int dfs_jffs2_unmount(struct dfs_filesystem* fs)
  181. {
  182. int result;
  183. unsigned index;
  184. /* find device index, then umount it */
  185. for (index = 0; index < DEVICE_PART_MAX; index++)
  186. {
  187. if (device_partition[index].dev == RT_MTD_DEVICE(fs->dev_id))
  188. {
  189. result = jffs2_umount(device_partition[index].mte);
  190. if (result)
  191. return jffs2_result_to_dfs(result);
  192. rt_free(device_partition[index].mte);
  193. device_partition[index].dev = NULL;
  194. device_partition[index].mte = NULL;
  195. return DFS_STATUS_OK;
  196. }
  197. }
  198. return -DFS_STATUS_ENOENT;
  199. }
  200. static int dfs_jffs2_mkfs(const char* device_name)
  201. {
  202. /* just erase all blocks on this nand partition */
  203. return -DFS_STATUS_ENOSYS;
  204. }
  205. static int dfs_jffs2_statfs(struct dfs_filesystem* fs,
  206. struct statfs *buf)
  207. {
  208. /* since the limit of unsigned long, so the max size of flash device is 4G */
  209. struct cyg_mtab_entry * mte;
  210. struct jffs2_fs_info info;
  211. int result;
  212. result = _find_fs(&mte, fs->dev_id);
  213. if (result)
  214. return -DFS_STATUS_ENOENT;
  215. RT_ASSERT(mte->data != NULL);
  216. jffs2_get_info_from_sb((void *)mte->data, &info);
  217. buf->f_bsize = info.sector_size;
  218. buf->f_blocks = info.nr_blocks;
  219. buf->f_bfree = info.free_size; //fixme need test!
  220. // jffs2_sb = (struct super_block)(mte->data);
  221. // c = JFFS2_SB_INFO(jffs2_sb);
  222. //
  223. // buf->f_bsize = c->sector_size;
  224. // buf->f_blocks = c->nr_blocks;
  225. // buf->f_bfree = c->free_size; //fixme need test!
  226. return 0;
  227. }
  228. static const char jffs2_root_path[] = ".";
  229. static int dfs_jffs2_open(struct dfs_fd* file)
  230. {
  231. int oflag, mode;
  232. int result;
  233. cyg_file * jffs2_file;
  234. struct cyg_mtab_entry * mte;
  235. const char * name;
  236. oflag = file->flags;
  237. jffs2_file = rt_malloc(sizeof(cyg_file));
  238. if (jffs2_file == RT_NULL)
  239. return -DFS_STATUS_ENOMEM;
  240. /* just escape '/' provided by dfs code */
  241. name = file->path;
  242. if ((name[0] == '/') && (name[1] == 0))
  243. name = jffs2_root_path;
  244. else /* name[0] still will be '/' */
  245. name ++;
  246. result = _find_fs(&mte, file->fs->dev_id);
  247. if (result)
  248. {
  249. rt_free(jffs2_file);
  250. return -DFS_STATUS_ENOENT;
  251. }
  252. if (oflag & DFS_O_DIRECTORY) /* operations about dir */
  253. {
  254. if (oflag & DFS_O_CREAT) /* create a dir*/
  255. {
  256. /* fixme, should test file->path can end with '/' */
  257. result = jffs2_mkdir(mte, mte->root, name);
  258. if (result)
  259. {
  260. rt_free(jffs2_file);
  261. return jffs2_result_to_dfs(result);
  262. }
  263. }
  264. /* open dir */
  265. result = jffs2_opendir(mte, mte->root, name, jffs2_file);
  266. if (result)
  267. {
  268. rt_free(jffs2_file);
  269. return jffs2_result_to_dfs(result);
  270. }
  271. #ifdef CONFIG_JFFS2_NO_RELATIVEDIR
  272. jffs2_file->f_offset = 2;
  273. #endif
  274. /* save this pointer, it will be used by dfs_jffs2_getdents*/
  275. file->data = jffs2_file;
  276. return 0;
  277. }
  278. /* regular file operations */
  279. mode = 0;
  280. if (oflag & DFS_O_RDONLY) mode |= JFFS2_O_RDONLY;
  281. if (oflag & DFS_O_WRONLY) mode |= JFFS2_O_WRONLY;
  282. if (oflag & DFS_O_RDWR) mode |= JFFS2_O_RDWR;
  283. /* Opens the file, if it is existing. If not, a new file is created. */
  284. if (oflag & DFS_O_CREAT) mode |= JFFS2_O_CREAT;
  285. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  286. if (oflag & DFS_O_TRUNC) mode |= JFFS2_O_TRUNC;
  287. /* Creates a new file. The function fails if the file is already existing. */
  288. if (oflag & DFS_O_EXCL) mode |= JFFS2_O_EXCL;
  289. // if (oflag & DFS_O_APPEND) mode |= JFFS2_O_APPEND;
  290. result = jffs2_open(mte, 0, name, mode, jffs2_file);
  291. if (result != 0)
  292. {
  293. rt_free(jffs2_file);
  294. return jffs2_result_to_dfs(result);
  295. }
  296. /* save this pointer, it will be used when calling read()£¬write(),
  297. flush(), lessk(), and will be rt_free when calling close()*/
  298. file->data = jffs2_file;
  299. file->pos = jffs2_file->f_offset;
  300. file->size = 0;
  301. jffs2_file_lseek(jffs2_file, (off_t *)(&(file->size)), SEEK_END);
  302. jffs2_file->f_offset = (off_t)file->pos;
  303. if (oflag & DFS_O_APPEND)
  304. {
  305. file->pos = file->size;
  306. jffs2_file->f_offset = file->size;
  307. }
  308. return 0;
  309. }
  310. static int dfs_jffs2_close(struct dfs_fd* file)
  311. {
  312. int result;
  313. cyg_file * jffs2_file;
  314. RT_ASSERT(file->data != NULL);
  315. jffs2_file = (cyg_file *)(file->data);
  316. if (file->flags & DFS_O_DIRECTORY) /* operations about dir */
  317. {
  318. result = jffs2_dir_colse(jffs2_file);
  319. if (result)
  320. return jffs2_result_to_dfs(result);
  321. rt_free(jffs2_file);
  322. return 0;
  323. }
  324. /* regular file operations */
  325. result = jffs2_file_colse(jffs2_file);
  326. if (result)
  327. return jffs2_result_to_dfs(result);
  328. /* release memory */
  329. rt_free(jffs2_file);
  330. return 0;
  331. }
  332. static int dfs_jffs2_ioctl(struct dfs_fd* file, int cmd, void* args)
  333. {
  334. return -DFS_STATUS_ENOSYS;
  335. }
  336. static int dfs_jffs2_read(struct dfs_fd* file, void* buf, rt_size_t len)
  337. {
  338. cyg_file * jffs2_file;
  339. struct CYG_UIO_TAG uio_s;
  340. struct CYG_IOVEC_TAG iovec;
  341. int char_read;
  342. int result;
  343. RT_ASSERT(file->data != NULL);
  344. jffs2_file = (cyg_file *)(file->data);
  345. uio_s.uio_iov = &iovec;
  346. uio_s.uio_iov->iov_base = buf;
  347. uio_s.uio_iov->iov_len = len;
  348. uio_s.uio_iovcnt = 1; //must be 1
  349. //uio_s.uio_offset //not used...
  350. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  351. char_read = jffs2_file->f_offset; /* the current offset */
  352. result = jffs2_file_read(jffs2_file, &uio_s);
  353. if (result)
  354. return jffs2_result_to_dfs(result);
  355. /* update position */
  356. file->pos = jffs2_file->f_offset;
  357. char_read = jffs2_file->f_offset - char_read;
  358. return char_read;
  359. }
  360. static int dfs_jffs2_write(struct dfs_fd* file,
  361. const void* buf,
  362. rt_size_t len)
  363. {
  364. cyg_file * jffs2_file;
  365. struct CYG_UIO_TAG uio_s;
  366. struct CYG_IOVEC_TAG iovec;
  367. int char_write;
  368. int result;
  369. RT_ASSERT(file->data != NULL);
  370. jffs2_file = (cyg_file *)(file->data);
  371. uio_s.uio_iov = &iovec;
  372. uio_s.uio_iov->iov_base = (void *)buf;
  373. uio_s.uio_iov->iov_len = len;
  374. uio_s.uio_iovcnt = 1; //must be 1
  375. //uio_s.uio_offset //not used...
  376. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  377. char_write = jffs2_file->f_offset;
  378. result = jffs2_file_write(jffs2_file, &uio_s);
  379. if (result)
  380. return jffs2_result_to_dfs(result);
  381. /* update position */
  382. file->pos = jffs2_file->f_offset;
  383. char_write = jffs2_file->f_offset - char_write;
  384. return char_write;
  385. }
  386. static int dfs_jffs2_flush(struct dfs_fd* file)
  387. {
  388. /* infact, jffs2 not support, jffs2_fo_sync just return ok */
  389. return -DFS_STATUS_ENOSYS;
  390. }
  391. /* fixme warning: the offset is rt_off_t, so maybe the size of a file is must <= 2G*/
  392. static int dfs_jffs2_lseek(struct dfs_fd* file,
  393. rt_off_t offset)
  394. {
  395. cyg_file * jffs2_file;
  396. int result;
  397. RT_ASSERT(file->data != NULL);
  398. jffs2_file = (cyg_file *)(file->data);
  399. /* set offset as current offset */
  400. result = jffs2_file_lseek(jffs2_file, &offset, SEEK_SET);
  401. if (result)
  402. return jffs2_result_to_dfs(result);
  403. /* update file position */
  404. file->pos = offset;
  405. return offset;
  406. }
  407. /* return the size of struct dirent*/
  408. static int dfs_jffs2_getdents(struct dfs_fd* file,
  409. struct dirent* dirp,
  410. rt_uint32_t count)
  411. {
  412. cyg_file * jffs2_file;
  413. struct CYG_UIO_TAG uio_s;
  414. struct CYG_IOVEC_TAG iovec;
  415. struct jffs2_dirent jffs2_d;
  416. struct dirent * d;
  417. rt_uint32_t index;
  418. #if !defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  419. struct jffs2_stat s;
  420. cyg_mtab_entry * mte;
  421. char * fullname;
  422. #endif
  423. int result;
  424. RT_ASSERT(file->data != RT_NULL);
  425. jffs2_file = (cyg_file*)(file->data);
  426. //set jffs2_d
  427. memset(&jffs2_d, 0, sizeof(struct jffs2_dirent));
  428. //set CYG_UIO_TAG uio_s
  429. uio_s.uio_iov = &iovec;
  430. uio_s.uio_iov->iov_base = &jffs2_d;
  431. uio_s.uio_iov->iov_len = sizeof(struct jffs2_dirent);;
  432. uio_s.uio_iovcnt = 1; //must be 1
  433. uio_s.uio_offset = 0;//not used...
  434. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  435. #if !defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  436. result = _find_fs(&mte, file->fs->dev_id);
  437. if (result)
  438. return -DFS_STATUS_ENOENT;
  439. #endif
  440. /* make integer count, usually count is 1 */
  441. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  442. if (count == 0)
  443. return -DFS_STATUS_EINVAL;
  444. index = 0;
  445. /* usually, the while loop should only be looped only once! */
  446. while (1)
  447. {
  448. d = dirp + index;
  449. result = jffs2_dir_read(jffs2_file, &uio_s);
  450. /* if met a error or all entry are read over, break while*/
  451. if (result || jffs2_d.d_name[0] == 0)
  452. break;
  453. #if defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  454. switch(jffs2_d.d_type & JFFS2_S_IFMT)
  455. {
  456. case JFFS2_S_IFREG: d->d_type = DFS_DT_REG; break;
  457. case JFFS2_S_IFDIR: d->d_type = DFS_DT_DIR; break;
  458. default: d->d_type = DFS_DT_UNKNOWN; break;
  459. }
  460. #else
  461. fullname = rt_malloc(FILE_PATH_MAX);
  462. if(fullname == RT_NULL)
  463. return -DFS_STATUS_ENOMEM;
  464. /* make a right entry */
  465. if ((file->path[0] == '/') )
  466. {
  467. if (file->path[1] == 0)
  468. strcpy(fullname, jffs2_d.d_name);
  469. else
  470. rt_sprintf(fullname, "%s/%s", file->path+1, jffs2_d.d_name);
  471. }
  472. else
  473. rt_sprintf(fullname, "%s/%s", file->path, jffs2_d.d_name);
  474. result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s);
  475. if (result)
  476. return jffs2_result_to_dfs(result);
  477. rt_free(fullname);
  478. /* convert to dfs stat structure */
  479. switch(s.st_mode & JFFS2_S_IFMT)
  480. {
  481. case JFFS2_S_IFREG: d->d_type = DFS_DT_REG; break;
  482. case JFFS2_S_IFDIR: d->d_type = DFS_DT_DIR; break;
  483. default: d->d_type = DFS_DT_UNKNOWN; break;
  484. }
  485. #endif
  486. /* write the rest fields of struct dirent* dirp */
  487. d->d_namlen = rt_strlen(jffs2_d.d_name);
  488. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  489. rt_strncpy(d->d_name, jffs2_d.d_name, d->d_namlen + 1);
  490. index ++;
  491. if (index * sizeof(struct dirent) >= count)
  492. break;
  493. }
  494. if (result)
  495. return jffs2_result_to_dfs(result);
  496. return index * sizeof(struct dirent);
  497. }
  498. static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
  499. {
  500. int result;
  501. struct jffs2_stat s;
  502. cyg_mtab_entry * mte;
  503. result = _find_fs(&mte, fs->dev_id);
  504. if (result)
  505. return -DFS_STATUS_ENOENT;
  506. /* deal path */
  507. if (path[0] == '/')
  508. path++;
  509. /* judge file type, dir is to be delete by rmdir, others by unlink */
  510. result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
  511. if (result)
  512. return jffs2_result_to_dfs(result);
  513. switch(s.st_mode & JFFS2_S_IFMT)
  514. {
  515. case JFFS2_S_IFREG:
  516. result = jffs2_file_unlink(mte, mte->root, path);
  517. break;
  518. case JFFS2_S_IFDIR:
  519. result = jffs2_rmdir(mte, mte->root, path);
  520. break;
  521. default:
  522. /* unknown file type */
  523. return -1;
  524. }
  525. if (result)
  526. return jffs2_result_to_dfs(result);
  527. return 0;
  528. }
  529. static int dfs_jffs2_rename(struct dfs_filesystem* fs,
  530. const char* oldpath,
  531. const char* newpath)
  532. {
  533. int result;
  534. cyg_mtab_entry * mte;
  535. result = _find_fs(&mte, fs->dev_id);
  536. if (result)
  537. return -DFS_STATUS_ENOENT;
  538. if (*oldpath == '/')
  539. oldpath += 1;
  540. if (*newpath == '/')
  541. newpath += 1;
  542. result = jffs2_rename(mte, mte->root, oldpath, mte->root, newpath);
  543. if (result)
  544. return jffs2_result_to_dfs(result);
  545. return 0;
  546. }
  547. static int dfs_jffs2_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
  548. {
  549. int result;
  550. struct jffs2_stat s;
  551. cyg_mtab_entry * mte;
  552. /* deal the path for jffs2 */
  553. RT_ASSERT(!((path[0] == '/') && (path[1] == 0)));
  554. if (path[0] == '/')
  555. path++;
  556. result = _find_fs(&mte, fs->dev_id);
  557. if (result)
  558. return -DFS_STATUS_ENOENT;
  559. // result = jffs2_ops_stat(mte, mte->root, path, &s);
  560. result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
  561. if (result)
  562. return jffs2_result_to_dfs(result);
  563. /* convert to dfs stat structure */
  564. switch(s.st_mode & JFFS2_S_IFMT)
  565. {
  566. case JFFS2_S_IFREG:
  567. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  568. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  569. break;
  570. case JFFS2_S_IFDIR:
  571. st->st_mode = DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  572. break;
  573. default:
  574. st->st_mode = DFS_DT_UNKNOWN; //fixme
  575. break;
  576. }
  577. st->st_dev = 0;
  578. st->st_size = s.st_size;
  579. st->st_mtime = s.st_mtime;
  580. /*st->st_blksize = ;*/ //fixme: what's this field?
  581. return 0;
  582. }
  583. static const struct dfs_filesystem_operation dfs_jffs2_ops =
  584. {
  585. "jffs2", /* file system type: jffs2 */
  586. DFS_FS_FLAG_DEFAULT, /* use Relative Path */
  587. dfs_jffs2_mount,
  588. dfs_jffs2_unmount,
  589. dfs_jffs2_mkfs,
  590. dfs_jffs2_statfs,
  591. dfs_jffs2_open,
  592. dfs_jffs2_close,
  593. dfs_jffs2_ioctl,
  594. dfs_jffs2_read,
  595. dfs_jffs2_write,
  596. dfs_jffs2_flush,
  597. dfs_jffs2_lseek,
  598. dfs_jffs2_getdents,
  599. dfs_jffs2_unlink,
  600. dfs_jffs2_stat,
  601. dfs_jffs2_rename,
  602. };
  603. int dfs_jffs2_init(void)
  604. {
  605. /* register fatfs file system */
  606. dfs_register(&dfs_jffs2_ops);
  607. return 0;
  608. }