luat_luadb_rtt.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <rtdevice.h>
  2. #include <rtthread.h>
  3. #include <luat_base.h>
  4. #include <dfs_file.h>
  5. #include <dfs_fs.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "luat_luadb.h"
  9. #define LUAT_LOG_TAG "luadb.dfs"
  10. #include "luat_log.h"
  11. // 注意: rtt的dfs文件路径是/开头的,而luadb的路径是不带/的,需要转一下
  12. static int _dfs_luadb_open (struct dfs_fd *fd) {
  13. int _fd = luat_luadb_open((luadb_fs_t*)fd->fs->data, fd->path + 1, fd->flags, 0);
  14. if (_fd != 0) {
  15. fd->data = (void*) _fd;
  16. }
  17. return _fd;
  18. }
  19. static int _dfs_luadb_close (struct dfs_fd *fd) {
  20. return luat_luadb_close((luadb_fs_t*)fd->fs->data, (int)fd->data);
  21. }
  22. static int _dfs_luadb_ioctl (struct dfs_fd *fd, int cmd, void *args) {
  23. return -EIO;
  24. }
  25. static int _dfs_luadb_read (struct dfs_fd *fd, void *buf, size_t count) {
  26. return luat_luadb_read((luadb_fs_t*)fd->fs->data, (int)fd->data, buf, count);
  27. }
  28. //static int _dfs_luadb_write (struct dfs_fd *fd, const void *buf, size_t count);
  29. //static int _dfs_luadb_flush (struct dfs_fd *fd);
  30. static int _dfs_luadb_lseek (struct dfs_fd *fd, off_t offset) {
  31. return luat_luadb_lseek((luadb_fs_t*)fd->fs->data, (int)fd->data, offset, SEEK_SET);
  32. }
  33. //static int _dfs_luadb_getdents (struct dfs_fd *fd, struct dirent *dirp, uint32_t count);
  34. static const struct dfs_file_ops _dfs_luadb_fops = {
  35. _dfs_luadb_open,
  36. _dfs_luadb_close,
  37. _dfs_luadb_ioctl,
  38. _dfs_luadb_read,
  39. NULL,
  40. NULL,
  41. _dfs_luadb_lseek,
  42. NULL,
  43. // RT_NULL, /* poll interface */
  44. };
  45. static int _dfs_luadb_mount (struct dfs_filesystem *fs, unsigned long rwflag, const void *data) {
  46. if (data == NULL) {
  47. LLOGE("luadb mount data == NULL");
  48. return -EIO;
  49. }
  50. const char* ptr = (const char*) data;
  51. luadb_fs_t* _fs = luat_luadb_mount(ptr);
  52. if (_fs == NULL) {
  53. LLOGE("luat_luadb_mount return NULL");
  54. return -EIO;
  55. }
  56. fs->data = _fs;
  57. return 0;
  58. }
  59. static int _dfs_luadb_unmount (struct dfs_filesystem *fs) {
  60. if (fs->data == NULL) {
  61. return 0;
  62. }
  63. luat_luadb_umount((luadb_fs_t *)fs->data);
  64. return 0;
  65. }
  66. /* make a file system */
  67. // static int _dfs_luadb_mkfs (rt_device_t devid) {
  68. // return -EIO;
  69. // }
  70. static int _dfs_luadb_statfs (struct dfs_filesystem *fs, struct statfs *buf) {
  71. buf->f_bsize = 1024;
  72. buf->f_blocks = 64;
  73. buf->f_bfree = 0;
  74. return 0;
  75. }
  76. //static int _dfs_luadb_unlink (struct dfs_filesystem *fs, const char *pathname);
  77. static int _dfs_luadb_stat (struct dfs_filesystem *fs, const char *filename, struct stat *buf) {
  78. luadb_file_t* f = luat_luadb_stat((luadb_fs_t *)fs->data, filename + 1);
  79. if (f == NULL) {
  80. return -EIO;
  81. }
  82. buf->st_size = f->size;
  83. return 0;
  84. }
  85. //static int _dfs_luadb_rename (struct dfs_filesystem *fs, const char *oldpath, const char *newpath);
  86. static const struct dfs_filesystem_ops _dfs_luadb_ops = {
  87. "luadb",
  88. DFS_FS_FLAG_DEFAULT,
  89. &_dfs_luadb_fops,
  90. _dfs_luadb_mount,
  91. _dfs_luadb_unmount,
  92. NULL,
  93. _dfs_luadb_statfs,
  94. NULL,
  95. _dfs_luadb_stat,
  96. NULL,
  97. };
  98. int dfs_luadb_init(void)
  99. {
  100. /* register luadb file system */
  101. return dfs_register(&_dfs_luadb_ops);
  102. }
  103. INIT_COMPONENT_EXPORT(dfs_luadb_init);