test.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * https://gitlab.com/bztsrc/minix3fs
  3. *
  4. * Copyright (C) 2023 bzt (MIT license)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
  19. * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * @brief simple test for mfs.h
  24. *
  25. * Compile with:
  26. * gcc test.c -o test
  27. *
  28. * Run with:
  29. * ./test (mfs image file) (path)
  30. */
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #define MFS_IMPLEMENTATION
  35. #include "mfs.h"
  36. FILE *f = NULL;
  37. /**
  38. * Emulate sector reads
  39. */
  40. void loadsec(uint32_t sec, uint32_t cnt, void *dst) { fseek(f, sec << 9, SEEK_SET); fread(dst, cnt, 512, f); }
  41. /**
  42. * Main function
  43. */
  44. int main(int argc, char **argv)
  45. {
  46. int i, j;
  47. uint64_t offs, len;
  48. uint8_t *data;
  49. /* check arguments */
  50. if(argc < 3) {
  51. printf(" %s <mfsimgfile> <path>\r\n\r\n", argv[0]);
  52. return 1;
  53. }
  54. /* open file system image file */
  55. f = fopen(argv[1], "rb");
  56. if(!f) { fprintf(stderr, "unable to open image\r\n"); return 2; }
  57. /* test open / read / close functions */
  58. len = mfs_open(argv[2]);
  59. printf("mfs_open(%s) = %ld (inode %d)\r\n", argv[2], len, mfs_inode);
  60. if(len == -1UL) { fprintf(stderr, "Minix3 File System not found in image\r\n"); fclose(f); return 3; }
  61. if(!len) { fprintf(stderr, "unable to locate path '%s' in image\r\n", argv[2]); fclose(f); return 3; }
  62. data = malloc(len);
  63. if(!data) { fprintf(stderr, "unable to allocate memory\r\n"); fclose(f); return 4; }
  64. offs = len;
  65. len = mfs_read(0, offs, data);
  66. printf("mfs_read(0,%ld,ptr) = %ld\r\n", offs, len);
  67. /* hexdump file contents */
  68. for(offs = 0, j = 16; offs < len; offs += j, data += j) {
  69. j = offs + 16 < len ? 16 : len - offs;
  70. printf("%06lx: ", offs);
  71. for(i = 0; i < 16; i++) if(i < j) printf("%02x ", data[i]); else printf(" ");
  72. printf(" ");
  73. for(i = 0; i < j; i++) printf("%c", data[i] >= 32 && data[i] < 127 ? data[i] : '.');
  74. printf("\n");
  75. }
  76. mfs_close();
  77. printf("mfs_close()\r\n");
  78. /* teardown */
  79. fclose(f);
  80. return 0;
  81. }