multitape_internal.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef MULTITAPE_INTERNAL_H_
  2. #define MULTITAPE_INTERNAL_H_
  3. #include <sys/types.h>
  4. #include <stdint.h>
  5. #include "chunks.h"
  6. #include "ctassert.h"
  7. #include "storage.h"
  8. /* On-disk chunk header structure. Integers are little-endian. */
  9. struct chunkheader {
  10. uint8_t hash[32];
  11. uint8_t len[4];
  12. uint8_t zlen[4];
  13. };
  14. CTASSERT(sizeof(struct chunkheader) == 40);
  15. /* On-disk entry header structure. */
  16. struct entryheader {
  17. uint8_t hlen[4];
  18. uint8_t clen[8];
  19. uint8_t tlen[4];
  20. };
  21. CTASSERT(sizeof(struct entryheader) == 16);
  22. /* Archive metadata structure. */
  23. struct tapemetadata {
  24. char * name;
  25. time_t ctime;
  26. int argc;
  27. char ** argv;
  28. uint8_t indexhash[32];
  29. uint64_t indexlen;
  30. size_t metadatalen; /* Filled in by _get. */
  31. };
  32. /* Unpacked archive metaindex structure. */
  33. struct tapemetaindex {
  34. uint8_t * hindex;
  35. size_t hindexlen;
  36. uint8_t * cindex;
  37. size_t cindexlen;
  38. uint8_t * tindex;
  39. size_t tindexlen;
  40. };
  41. /*
  42. * Maximum chunk size. This is chosen so that after deflating (which might
  43. * add up to 0.1% + 13 bytes to the size) and adding cryptographic wrapping
  44. * (which will add 296 bytes) the final maximum file size is <= 2^18.
  45. */
  46. #define MAXCHUNK 261120
  47. /*
  48. * Maximum index fragment size. The metaindex is stored as a series of
  49. * fragments of this length plus a final fragment containing whatever is
  50. * left.
  51. */
  52. #define MAXIFRAG MAXCHUNK
  53. /**
  54. * multitape_cleanstate(cachedir, machinenum, key, storage_modified):
  55. * Complete any pending checkpoint or commit. The value ${key} should be 0
  56. * if the write access key should be used to sign a commit request, or 1 if
  57. * the delete access key should be used. If the data on the server has been
  58. * modified, set ${*storage_modified} to 1.
  59. */
  60. int multitape_cleanstate(const char *, uint64_t, uint8_t, int *);
  61. /**
  62. * multitape_checkpoint(cachedir, machinenum, seqnum):
  63. * Create a checkpoint in the current write transaction.
  64. */
  65. int multitape_checkpoint(const char *, uint64_t, const uint8_t[32]);
  66. /**
  67. * multitape_commit(cachedir, machinenum, seqnum, key, storage_modified):
  68. * Commit the most recent transaction. The value ${key} is defined as in
  69. * multitape_cleanstate. If the data on the server has been modified, set
  70. * ${*storage_modified} to 1.
  71. */
  72. int multitape_commit(const char *, uint64_t, const uint8_t[32], uint8_t,
  73. int *);
  74. /**
  75. * multitape_chunkiter_tmd(S, C, tmd, func, cookie, quiet):
  76. * Call ${func} on ${cookie} and each struct chunkheader involved in the
  77. * archive associated with the metadata ${tmd}. If ${C} is non-NULL, call
  78. * chunks_stats_extrastats on ${C} and the length of each metadata fragment.
  79. * If ${quiet}, don't print any warnings about corrupt or missing files.
  80. * Return 0 (success), 1 (a required file is missing), 2 (a required file is
  81. * corrupt), -1 (error), or the first non-zero value returned by ${func}.
  82. */
  83. int multitape_chunkiter_tmd(STORAGE_R *, CHUNKS_S *,
  84. const struct tapemetadata *,
  85. int(void *, struct chunkheader *), void *, int);
  86. /**
  87. * multitape_metadata_ispresent(S, tapename):
  88. * Return 1 if there is already a metadata file for the specified archive
  89. * name, 0 if not, or -1 on error.
  90. */
  91. int multitape_metadata_ispresent(STORAGE_W *, const char *);
  92. /**
  93. * multitape_metadata_put(S, C, mdat):
  94. * Store archive metadata. Call chunks_write_extrastats on ${C} and the
  95. * metadata file length.
  96. */
  97. int multitape_metadata_put(STORAGE_W *, CHUNKS_W *, struct tapemetadata *);
  98. /**
  99. * multitape_metadata_get_byhash(S, C, mdat, tapehash, quiet):
  100. * Read and parse metadata for the archive for which the metadata file is
  101. * named ${tapehash}. If ${C} is non-NULL, call chunks_stats_extrastats on
  102. * ${C} and the length of the metadata file. If ${quiet}, don't print any
  103. * warnings about corrupt or missing files. Return 0 on success, 1 if the
  104. * metadata file does not exist, 2 if the metadata file is corrupt, or -1 on
  105. * error.
  106. */
  107. int multitape_metadata_get_byhash(STORAGE_R *, CHUNKS_S *,
  108. struct tapemetadata *, const uint8_t[32], int);
  109. /**
  110. * multitape_metadata_get_byname(S, C, mdat, tapename, quiet):
  111. * Read and parse metadata for the archive named ${tapename}. If ${C} is
  112. * non-NULL, call chunks_stats_extrastats on ${C} and the length of the
  113. * metadata file. If ${quiet}, don't print any warnings about corrupt or
  114. * missing files. Return 0 on success, 1 if the metadata file does not
  115. * exist, 2 if the metadata file is corrupt, or -1 on error.
  116. */
  117. int multitape_metadata_get_byname(STORAGE_R *, CHUNKS_S *,
  118. struct tapemetadata *, const char *, int);
  119. /**
  120. * multitape_metadata_free(mdat):
  121. * Free pointers within ${mdat} (but not ${mdat} itself).
  122. */
  123. void multitape_metadata_free(struct tapemetadata *);
  124. /**
  125. * multitape_metadata_recrypt(obuf, obuflen, nbuf, nbuflen):
  126. * Decrypt and re-encrypt the provided metadata file.
  127. */
  128. int multitape_metadata_recrypt(uint8_t *, size_t, uint8_t **, size_t *);
  129. /**
  130. * multitape_metadata_delete(S, C, mdat):
  131. * Delete specified metadata file; ${mdat} must have been initialized by a
  132. * call to multitape_metadata_get_by(hash|name). Call
  133. * chunks_delete_extrastats on ${C} and the metadata file length.
  134. */
  135. int multitape_metadata_delete(STORAGE_D *, CHUNKS_D *, struct tapemetadata *);
  136. /**
  137. * multitape_metaindex_fragname(namehash, fragnum, fraghash):
  138. * Compute ${fraghash = SHA256(namehash || fragnum)}, which is the name
  139. * of the file containing the ${fragnum}'th part of the index corresponding to
  140. * the metadata with file name ${namehash}.
  141. */
  142. void multitape_metaindex_fragname(const uint8_t[32], uint32_t, uint8_t[32]);
  143. /**
  144. * multitape_metaindex_put(S, C, mind, mdat):
  145. * Store the provided archive metaindex, and update the archive metadata
  146. * with the metaindex parameters. Call chunks_write_extrastats on ${C} and
  147. * the length(s) of file(s) containing the metaindex.
  148. */
  149. int multitape_metaindex_put(STORAGE_W *, CHUNKS_W *, struct tapemetaindex *,
  150. struct tapemetadata *);
  151. /**
  152. * multitape_metaindex_get(S, C, mind, mdat, quiet):
  153. * Read and parse the metaindex for the archive associated with ${mdat}. If
  154. * ${C} is non-NULL, call chunks_stats_extrastats on ${C} and the length(s)
  155. * of file(s) containing the metaindex. Return 0 on success, 1 if the
  156. * metaindex file does not exist, 2 if the metaindex file is corrupt, or -1
  157. * on error.
  158. */
  159. int multitape_metaindex_get(STORAGE_R *, CHUNKS_S *, struct tapemetaindex *,
  160. const struct tapemetadata *, int);
  161. /**
  162. * multitape_metaindex_free(mind):
  163. * Free pointers within ${mind} (but not ${mind} itself).
  164. */
  165. void multitape_metaindex_free(struct tapemetaindex *);
  166. /**
  167. * multitape_metaindex_delete(S, C, mdat):
  168. * Delete the metaindex file associated with the provided metadata. Call
  169. * chunks_delete_extrastats on ${C} and the length(s) of file(s) containing
  170. * the metaindex.
  171. */
  172. int multitape_metaindex_delete(STORAGE_D *, CHUNKS_D *,
  173. struct tapemetadata *);
  174. /**
  175. * multitape_lock(cachedir):
  176. * Lock the given cache directory using lockf(3) or flock(2); return the file
  177. * descriptor of the lock file, or -1 on error.
  178. */
  179. int multitape_lock(const char *);
  180. /**
  181. * multitape_sequence(cachedir, seqnum):
  182. * Set ${seqnum} to the sequence number of the last committed transaction in
  183. * the cache directory ${cachedir}, or 0 if no transactions have ever been
  184. * committed.
  185. */
  186. int multitape_sequence(const char *, uint8_t[32]);
  187. #endif /* !MULTITAPE_INTERNAL_H_ */