snapshot.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #ifndef _SNAPSHOT_H__
  14. #define _SNAPSHOT_H__
  15. #define PATH_MAX 256
  16. #define OWRT 0x4f575254
  17. #define DATA 0x44415441
  18. #define CONF 0x434f4e46
  19. struct file_header {
  20. uint32_t magic;
  21. uint32_t type;
  22. uint32_t seq;
  23. uint32_t length;
  24. uint32_t md5[4];
  25. };
  26. static inline int
  27. is_config(struct file_header *h)
  28. {
  29. return ((h->magic == OWRT) && (h->type == CONF));
  30. }
  31. static inline int
  32. valid_file_size(int fs)
  33. {
  34. if ((fs > 8 * 1024 * 1204) || (fs <= 0))
  35. return -1;
  36. return 0;
  37. }
  38. static inline void
  39. hdr_to_be32(struct file_header *hdr)
  40. {
  41. uint32_t *h = (uint32_t *) hdr;
  42. int i;
  43. for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
  44. h[i] = cpu_to_be32(h[i]);
  45. }
  46. static inline void
  47. be32_to_hdr(struct file_header *hdr)
  48. {
  49. uint32_t *h = (uint32_t *) hdr;
  50. int i;
  51. for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
  52. h[i] = be32_to_cpu(h[i]);
  53. }
  54. static inline int
  55. pad_file_size(struct volume *v, int size)
  56. {
  57. int mod;
  58. size += sizeof(struct file_header);
  59. mod = size % v->block_size;
  60. if (mod) {
  61. size -= mod;
  62. size += v->block_size;
  63. }
  64. return size;
  65. }
  66. int verify_file_hash(char *file, uint32_t *hash);
  67. int snapshot_next_free(struct volume *v, uint32_t *seq);
  68. int config_find(struct volume *v, struct file_header *conf, struct file_header *sentinel);
  69. int snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint32_t type);
  70. int snapshot_read_file(struct volume *v, int block, char *file, uint32_t type);
  71. int sentinel_write(struct volume *v, uint32_t _seq);
  72. int volatile_write(struct volume *v, uint32_t _seq);
  73. #endif