storage.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #ifndef STORAGE_H_
  2. #define STORAGE_H_
  3. #include <stdint.h> /* uint8_t, uint64_t */
  4. #include <unistd.h> /* size_t */
  5. #include "crypto.h"
  6. #define STORAGE_FILE_OVERHEAD (CRYPTO_FILE_HLEN + CRYPTO_FILE_TLEN)
  7. typedef struct storage_read_internal STORAGE_R;
  8. typedef struct storage_write_internal STORAGE_W;
  9. typedef struct storage_delete_internal STORAGE_D;
  10. /**
  11. * storage_read_init(machinenum):
  12. * Prepare for read operations. Note that since reads are non-transactional,
  13. * this could be a no-op aside from storing the machine number.
  14. */
  15. STORAGE_R * storage_read_init(uint64_t);
  16. /**
  17. * storage_read_add_name_cache(S, class, name):
  18. * Add the file ${name} from class ${class} into the cache for the read cookie
  19. * ${S} returned from storage_read_init. The data will not be fetched yet;
  20. * but any future fetch will look in the cache first and will store the block
  21. * in the cache if it needs to be fetched.
  22. */
  23. int storage_read_add_name_cache(STORAGE_R *, char, const uint8_t[32]);
  24. /**
  25. * storage_read_set_cache_limit(S, size):
  26. * Set a limit of ${size} bytes on the cache associated with read cookie ${S}.
  27. */
  28. void storage_read_set_cache_limit(STORAGE_R *, size_t);
  29. /**
  30. * storage_read_file(S, buf, buflen, class, name):
  31. * Read the file ${name} from class ${class} using the read cookie ${S}
  32. * returned from storage_read_init into the buffer ${buf} of length ${buflen}.
  33. * Return 0 on success, 1 if the file does not exist; 2 if the file is not
  34. * ${buflen} bytes long or is corrupt; or -1 on error.
  35. */
  36. int storage_read_file(STORAGE_R *, uint8_t *, size_t, char,
  37. const uint8_t[32]);
  38. /**
  39. * storage_read_file_alloc(S, buf, buflen, class, name):
  40. * Allocate a buffer and read the file ${name} from class ${class} using the
  41. * read cookie ${S} into it; set ${buf} to point at the buffer, and
  42. * ${buflen} to the length of the buffer. Return 0, 1, 2, or -1 as per
  43. * storage_read_file.
  44. */
  45. int storage_read_file_alloc(STORAGE_R *, uint8_t **, size_t *, char,
  46. const uint8_t[32]);
  47. /**
  48. * storage_read_file_callback(S, buf, buflen, class, name, callback, cookie):
  49. * Read the file ${name} from class ${class} using the read cookie ${S}
  50. * returned from storage_read_init. If ${buf} is non-NULL, then read the
  51. * file (which should be ${buflen} bytes in length) into ${buf}; otherwise
  52. * malloc a buffer. Invoke ${callback}(${cookie}, status, b, blen) when
  53. * complete, where ${status} is 0, 1, 2, or -1 as per storage_read_file,
  54. * ${b} is the buffer into which the data was read (which will be ${buf} if
  55. * that value was non-NULL) and ${blen} is the length of the file.
  56. */
  57. int storage_read_file_callback(STORAGE_R *, uint8_t *, size_t, char,
  58. const uint8_t[32], int(*)(void *, int, uint8_t *, size_t), void *);
  59. /**
  60. * storage_read_free(S):
  61. * Close the read cookie ${S} and free any allocated memory.
  62. */
  63. void storage_read_free(STORAGE_R *);
  64. /**
  65. * storage_write_start(machinenum, lastseq, seqnum):
  66. * Start a write transaction, presuming that ${lastseq} is the sequence
  67. * number of the last committed transaction, or zeroes if there is no
  68. * previous transaction; and store the sequence number of the new transaction
  69. * into ${seqnum}.
  70. */
  71. STORAGE_W * storage_write_start(uint64_t, const uint8_t[32], uint8_t[32]);
  72. /**
  73. * storage_write_fexist(S, class, name):
  74. * Test if a file ${name} exists in class ${class}, as part of the write
  75. * transaction associated with the cookie ${S}; return 1 if the file
  76. * exists, 0 if not, and -1 on error. If ${S} is NULL, return 0 without doing
  77. * anything.
  78. */
  79. int storage_write_fexist(STORAGE_W *, char, const uint8_t[32]);
  80. /**
  81. * storage_write_file(S, buf, len, class, name):
  82. * Write ${len} bytes from ${buf} to the file ${name} in class ${class} as
  83. * part of the write transaction associated with the cookie ${S}. If ${S} is
  84. * NULL, return 0 without doing anything.
  85. */
  86. int storage_write_file(STORAGE_W *, uint8_t *, size_t, char,
  87. const uint8_t[32]);
  88. /**
  89. * storage_write_flush(S):
  90. * Make sure all files written as part of the transaction associated with
  91. * the cookie ${S} have been safely stored in preparation for being committed.
  92. * If ${S} is NULL, return 0 without doing anything.
  93. */
  94. int storage_write_flush(STORAGE_W *);
  95. /**
  96. * storage_write_end(S):
  97. * Make sure all files written as part of the transaction associated with
  98. * the cookie ${S} have been safely stored in preparation for being
  99. * committed; and close the transaction and free associated memory. If ${S}
  100. * is NULL, return 0 without doing anything.
  101. */
  102. int storage_write_end(STORAGE_W *);
  103. /**
  104. * storage_write_free(S):
  105. * Free any memory allocated as part of the write transaction associated with
  106. * the cookie ${S}; the transaction will not be committed.
  107. */
  108. void storage_write_free(STORAGE_W *);
  109. /**
  110. * storage_delete_start(machinenum, lastseq, seqnum):
  111. * Start a delete transaction, presuming that ${lastseq} is the sequence
  112. * number of the last committed transaction, or zeroes if there is no
  113. * previous transaction; and store the sequence number of the new transaction
  114. * into ${seqnum}.
  115. */
  116. STORAGE_D * storage_delete_start(uint64_t, const uint8_t[32], uint8_t[32]);
  117. /**
  118. * storage_fsck_start(machinenum, seqnum, readonly, whichkey):
  119. * Start a fsck transaction, and store the sequence number of said
  120. * transaction into ${seqnum}. If ${whichkey} is zero, use the write key
  121. * (in which case the transaction must be readonly).
  122. */
  123. STORAGE_D * storage_fsck_start(uint64_t, uint8_t[32], int, uint8_t);
  124. /**
  125. * storage_delete_file(S, class, name):
  126. * Delete the file ${name} from class ${class} as part of the delete
  127. * transaction associated with the cookie ${S}.
  128. */
  129. int storage_delete_file(STORAGE_D *, char, const uint8_t[32]);
  130. /**
  131. * storage_delete_flush(S):
  132. * Make sure all operations performed as part of the transaction associated
  133. * with the cookie ${S} have been safely stored in preparation for being
  134. * committed.
  135. */
  136. int storage_delete_flush(STORAGE_D *);
  137. /**
  138. * storage_delete_end(S):
  139. * Make sure that all operations performed as part of the transaction
  140. * associated with the cookie ${S} have been safely stored in
  141. * preparation for being committed; and close the transaction and free
  142. * associated memory.
  143. */
  144. int storage_delete_end(STORAGE_D *);
  145. /**
  146. * storage_delete_free(S):
  147. * Free any memory allocated as part of the delete transaction associated
  148. * with the cookie ${S}; the transaction will not be committed.
  149. */
  150. void storage_delete_free(STORAGE_D *);
  151. /**
  152. * storage_transaction_checkpoint(machinenum, seqnum, ckptnonce, whichkey):
  153. * Create a checkpoint ${ckptnonce} in the current write transaction, which
  154. * has nonce ${seqnum}. The value ${whichkey} is defined as in
  155. * storage_transaction_commit.
  156. */
  157. int storage_transaction_checkpoint(uint64_t, const uint8_t[32],
  158. const uint8_t[32], uint8_t);
  159. /**
  160. * storage_transaction_commit(machinenum, seqnum, whichkey, storage_modified):
  161. * Commit the transaction ${seqnum} if it is the most recent uncommitted
  162. * transaction. The value ${whichkey} specifies a key which should be used
  163. * to sign the commit request: 0 if the write key should be used, and 1 if
  164. * the delete key should be used. If the data on the server has been
  165. * modified, set ${*storage_modified} to 1.
  166. */
  167. int storage_transaction_commit(uint64_t, const uint8_t[32], uint8_t, int *);
  168. /**
  169. * storage_transaction_commitfromcheckpoint(machinenum, whichkey,
  170. * storage_modified):
  171. * If a write transaction is currently in progress and has a checkpoint,
  172. * commit it. The value ${whichkey} specifies a key which should be used
  173. * to sign the commit request: 0 if the write key should be used, and 1 if
  174. * the delete key should be used. If the data on the server has been
  175. * modified, set ${*storage_modified} to 1.
  176. */
  177. int storage_transaction_commitfromcheckpoint(uint64_t, uint8_t, int *);
  178. /**
  179. * storage_directory_read(machinenum, class, key, flist, nfiles):
  180. * Fetch a sorted list of files in the specified class. If ${key} is 0, use
  181. * NETPACKET_DIRECTORY requests (using the read key); otherwise, use
  182. * NETPACKET_DIRECTORY_D requests (using the delete key). Return the list
  183. * and the number of files via ${flist} and ${nfiles} respectively.
  184. */
  185. int storage_directory_read(uint64_t, char, int, uint8_t **, size_t *);
  186. #endif /* !STORAGE_H_ */