file_dev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #define _LARGEFILE64_SOURCE
  29. #define _FILE_OFFSET_BITS 64
  30. #include <ext4_config.h>
  31. #include <ext4_blockdev.h>
  32. #include <ext4_errno.h>
  33. #include <stdio.h>
  34. #include <stdbool.h>
  35. #include <string.h>
  36. /**@brief Default filename.*/
  37. static const char *fname = "ExtFs.img";
  38. /**@brief Image block size.*/
  39. #define EXT4_FILEDEV_BSIZE 4096
  40. /**@brief Image file descriptor.*/
  41. static FILE *dev_file;
  42. #define DROP_LINUXCACHE_BUFFERS 0
  43. /**********************BLOCKDEV INTERFACE**************************************/
  44. static int file_dev_open(struct ext4_blockdev *bdev);
  45. static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  46. uint32_t blk_cnt);
  47. static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  48. uint64_t blk_id, uint32_t blk_cnt);
  49. static int file_dev_close(struct ext4_blockdev *bdev);
  50. /******************************************************************************/
  51. EXT4_BLOCKDEV_STATIC_INSTANCE(file_dev, EXT4_FILEDEV_BSIZE, 0, file_dev_open,
  52. file_dev_bread, file_dev_bwrite, file_dev_close, 0, 0);
  53. /******************************************************************************/
  54. static int file_dev_open(struct ext4_blockdev *bdev)
  55. {
  56. dev_file = fopen(fname, "r+b");
  57. if (!dev_file)
  58. return EIO;
  59. /*No buffering at file.*/
  60. setbuf(dev_file, 0);
  61. if (fseeko(dev_file, 0, SEEK_END))
  62. return EFAULT;
  63. file_dev.part_offset = 0;
  64. file_dev.part_size = ftello(dev_file);
  65. file_dev.bdif->ph_bcnt = file_dev.part_size / file_dev.bdif->ph_bsize;
  66. return EOK;
  67. }
  68. /******************************************************************************/
  69. static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  70. uint32_t blk_cnt)
  71. {
  72. if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
  73. return EIO;
  74. if (!blk_cnt)
  75. return EOK;
  76. if (!fread(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
  77. return EIO;
  78. return EOK;
  79. }
  80. static void drop_cache(void)
  81. {
  82. #if defined(__linux__) && DROP_LINUXCACHE_BUFFERS
  83. int fd;
  84. char *data = "3";
  85. sync();
  86. fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
  87. write(fd, data, sizeof(char));
  88. close(fd);
  89. #endif
  90. }
  91. /******************************************************************************/
  92. static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  93. uint64_t blk_id, uint32_t blk_cnt)
  94. {
  95. if (fseeko(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET))
  96. return EIO;
  97. if (!blk_cnt)
  98. return EOK;
  99. if (!fwrite(buf, bdev->bdif->ph_bsize * blk_cnt, 1, dev_file))
  100. return EIO;
  101. drop_cache();
  102. return EOK;
  103. }
  104. /******************************************************************************/
  105. static int file_dev_close(struct ext4_blockdev *bdev)
  106. {
  107. fclose(dev_file);
  108. return EOK;
  109. }
  110. /******************************************************************************/
  111. struct ext4_blockdev *file_dev_get(void)
  112. {
  113. return &file_dev;
  114. }
  115. /******************************************************************************/
  116. void file_dev_name_set(const char *n)
  117. {
  118. fname = n;
  119. }
  120. /******************************************************************************/