1
0

nofs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. /* Support for the 'nofs' filesystem
  20. *
  21. * This is a file system driver without the notion of files or directories.
  22. * It is implemented as a single file starting at byte 0 and potentially
  23. * spanning the entire partition.
  24. * It maintains an 'end-of-file' marker and ensures the last bytes of the file are
  25. * either EOF followed by ~~~~~~~~ (8 ~s) or just EOF regardless of fseeking
  26. * backward/forwards etc
  27. * The first three bytes of the filesystem are the UTF-8 byte order mark
  28. * (0xef, 0xbb, 0xbf) which should be ignored by any program which reads the
  29. * filesystem. If we read this on fopen, we search for an EOF marker to set the
  30. * current position (if mode & APPEND)
  31. */
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include "vfs.h"
  37. // Define to be the EOF marker to use
  38. //#define EOF_MARK { EOF }
  39. #define EOF_MARK { EOF, '~', '~', '~', '~', '~', '~', '~', '~' }
  40. // UTF BOM
  41. #define UTF_BOM_1 0xef
  42. #define UTF_BOM_2 0xbb
  43. #define UTF_BOM_3 0xbf
  44. static uint8_t eof_mark[] = EOF_MARK;
  45. static char nofs_name[] = "nofs";
  46. #ifdef NOFS_DEBUG
  47. static char nofs_debug_startup[] = "NOFS started up in debug mode\n";
  48. #endif
  49. struct nofs_fs
  50. {
  51. struct fs b;
  52. };
  53. static struct dirent *nofs_read_directory(struct fs *fs, char **name);
  54. static size_t nofs_fread(struct fs *fs, void *ptr, size_t byte_size, FILE *stream);
  55. static size_t nofs_fwrite(struct fs *fs, void *ptr, size_t byte_size, FILE *stream);
  56. static int nofs_fclose(struct fs *fs, FILE *fp);
  57. static FILE *nofs_fopen(struct fs *fs, struct dirent *path, const char *mode);
  58. static long nofs_fsize(FILE *fp);
  59. static int nofs_fseek(FILE *stream, long offset, int whence);
  60. static long nofs_ftell(FILE *fp);
  61. int nofs_init(struct block_device *parent, struct fs **fs)
  62. {
  63. // There is no way of identifying a nofs filesystem (aside from partition code)
  64. // Therefore just create the data structures and hope for the best
  65. struct nofs_fs *nofs = (struct nofs_fs *)malloc(sizeof(struct nofs_fs));
  66. if(fs == NULL)
  67. return -1;
  68. memset(nofs, 0, sizeof(struct nofs_fs));
  69. nofs->b.parent = parent;
  70. nofs->b.fs_name = nofs_name;
  71. nofs->b.flags = FS_FLAG_SUPPORTS_EMPTY_FNAME;
  72. nofs->b.fopen = nofs_fopen;
  73. nofs->b.fread = nofs_fread;
  74. nofs->b.fwrite = nofs_fwrite;
  75. nofs->b.fclose = nofs_fclose;
  76. nofs->b.fseek = nofs_fseek;
  77. nofs->b.fsize = nofs_fsize;
  78. nofs->b.ftell = nofs_ftell;
  79. nofs->b.read_directory = nofs_read_directory;
  80. nofs->b.block_size = parent->block_size;
  81. *fs = (struct fs *)nofs;
  82. printf("NOFS: found a nofs partition on %s\n", parent->device_name);
  83. #ifdef NOFS_DEBUG
  84. FILE *nfs_f = nofs_fopen(*fs, NULL, "a+");
  85. if(nfs_f == NULL)
  86. printf("NOFS: fopen failed %i\n", errno);
  87. nofs_fwrite(*fs, nofs_debug_startup, strlen(nofs_debug_startup), nfs_f);
  88. #endif
  89. return 0;
  90. }
  91. FILE *nofs_fopen(struct fs *fs, struct dirent *path, const char *mode)
  92. {
  93. struct vfs_file *f = (struct vfs_file *)malloc(sizeof(struct vfs_file));
  94. if(f == NULL)
  95. return NULL;
  96. memset(f, 0, sizeof(struct vfs_file));
  97. // Ignore path
  98. (void)path;
  99. // Interpret the mode argument
  100. int m = fs_interpret_mode(mode);
  101. // Read the first block
  102. uint8_t *block_0 = (uint8_t *)malloc(fs->block_size);
  103. if(block_0 == NULL)
  104. {
  105. free(f);
  106. return NULL;
  107. }
  108. long start_pos = 3;
  109. errno = 0;
  110. size_t bytes_read = block_read(fs->parent, block_0, fs->block_size, 0);
  111. if(bytes_read != fs->block_size)
  112. {
  113. free(f);
  114. free(block_0);
  115. if(errno == 0)
  116. errno = EFAULT;
  117. #ifdef NOFS_DEBUG
  118. printf("NOFS: unable to read block_0\n");
  119. #endif
  120. return NULL;
  121. }
  122. // Does the block contain a BOM?
  123. if((block_0[0] == UTF_BOM_1) && (block_0[1] == UTF_BOM_2) &&
  124. (block_0[2] == UTF_BOM_3))
  125. {
  126. #ifdef NOFS_DEBUG
  127. printf("NOFS: found UTF-8 BOM at start of partition, mode = %i, num_blocks = %i\n",
  128. m, fs->parent->num_blocks);
  129. #endif
  130. free(block_0);
  131. // If the mode argument includes an append, we have to search the
  132. // file looking for an EOF marker. We can only do this if the
  133. // underlying block device has a valid length
  134. if((m & VFS_MODE_APPEND) && fs->parent->num_blocks)
  135. {
  136. #ifdef NOFS_DEBUG
  137. printf("NOFS: searching for an EOF marker: ");
  138. #endif
  139. int found = 0;
  140. size_t block_no = 0;
  141. uint8_t *buf = (uint8_t *)malloc(fs->parent->block_size);
  142. if(buf == NULL)
  143. return NULL;
  144. while((found == 0) && (block_no < fs->parent->num_blocks))
  145. {
  146. bytes_read = fs->parent->read(fs->parent, buf,
  147. fs->parent->block_size, block_no);
  148. if(bytes_read == 0)
  149. break;
  150. for(size_t i = 0; i < bytes_read; i++)
  151. {
  152. if(buf[i] == EOF)
  153. {
  154. start_pos = block_no * fs->parent->block_size +
  155. i;
  156. found = 1;
  157. break;
  158. }
  159. }
  160. }
  161. #ifdef NOFS_DEBUG
  162. if(found)
  163. printf("found at position %i\n", start_pos);
  164. else
  165. printf("not found\n");
  166. #endif
  167. free(buf);
  168. }
  169. }
  170. else
  171. {
  172. #ifdef NOFS_DEBUG
  173. printf("NOFS: no BOM found: writing one\n");
  174. #endif
  175. // Write a UTF BOM followed by EOF to signify a new file
  176. block_0[0] = UTF_BOM_1;
  177. block_0[1] = UTF_BOM_2;
  178. block_0[2] = UTF_BOM_3;
  179. for(size_t i = 0; i < sizeof(eof_mark); i++)
  180. block_0[i + 3] = eof_mark[i];
  181. errno = 0;
  182. size_t bytes_written = block_write(fs->parent, block_0, fs->block_size, 0);
  183. free(block_0);
  184. if(bytes_written != fs->block_size)
  185. {
  186. free(f);
  187. if(errno == 0)
  188. errno = EFAULT;
  189. #ifdef NOFS_DEBUG
  190. printf("NOFS: unable to write block 0\n");
  191. #endif
  192. return NULL;
  193. }
  194. }
  195. // Now fill in the FILE structure
  196. f->fs = fs;
  197. f->pos = start_pos;
  198. f->mode = m;
  199. f->len = start_pos;
  200. #ifdef NOFS_DEBUG
  201. printf("NOFS: current file pointer %i\n", start_pos);
  202. #endif
  203. return (FILE *)f;
  204. }
  205. static uint32_t nofs_get_next_bdev_block_num(uint32_t f_block_idx, FILE *s, void *opaque, int add_blocks)
  206. {
  207. if((s->fs->parent->block_size) && (f_block_idx >= s->fs->parent->block_size))
  208. {
  209. errno = ENOSPC;
  210. return 0xffffffff;
  211. }
  212. if((f_block_idx > (s->len / s->fs->block_size)) && (add_blocks == 0))
  213. return 0xffffffff;
  214. (void)opaque;
  215. return f_block_idx;
  216. }
  217. size_t nofs_fread(struct fs *fs, void *ptr, size_t byte_size, FILE *stream)
  218. {
  219. return fs_fread(nofs_get_next_bdev_block_num, fs, ptr, byte_size, stream, NULL);
  220. }
  221. size_t nofs_fwrite(struct fs *fs, void *ptr, size_t byte_size, FILE *stream)
  222. {
  223. long old_len = stream->len;
  224. size_t ret = fs_fwrite(nofs_get_next_bdev_block_num, fs, ptr, byte_size, stream, NULL);
  225. if(stream->len > old_len)
  226. {
  227. // Terminate with an EOF if appropriate
  228. long new_pos = stream->len;
  229. fs_fwrite(nofs_get_next_bdev_block_num, fs, eof_mark, sizeof(eof_mark), stream, NULL);
  230. stream->pos = new_pos;
  231. stream->len = new_pos;
  232. }
  233. return ret;
  234. }
  235. static struct dirent *nofs_read_directory(struct fs *fs, char **name)
  236. {
  237. (void)fs;
  238. (void)name;
  239. return NULL;
  240. }
  241. static int nofs_fclose(struct fs *fs, FILE *fp)
  242. {
  243. (void)fs;
  244. (void)fp;
  245. return 0;
  246. }
  247. static long nofs_fsize(FILE *fp)
  248. {
  249. return fp->len - 3;
  250. }
  251. static int nofs_fseek(FILE *stream, long offset, int whence)
  252. {
  253. switch(whence)
  254. {
  255. case SEEK_SET:
  256. stream->pos = offset + 3;
  257. break;
  258. case SEEK_END:
  259. stream->pos = stream->len - offset;
  260. break;
  261. case SEEK_CUR:
  262. stream->pos += offset;
  263. break;
  264. default:
  265. return -1;
  266. }
  267. if(stream->pos < 3)
  268. stream->pos = 3;
  269. if(stream->pos > stream->len)
  270. stream->pos = stream->len;
  271. return 0;
  272. }
  273. static long nofs_ftell(FILE *fp)
  274. {
  275. return fp->pos - 3;
  276. }