Raw
1 #ifndef WRITE_OR_DIE_H
2 #define WRITE_OR_DIE_H
3
4 void maybe_flush_or_die(FILE *, const char *);
5 __attribute__((format (printf, 2, 3)))
6 void fprintf_or_die(FILE *, const char *fmt, ...);
7 void fwrite_or_die(FILE *f, const void *buf, size_t count);
8 void fflush_or_die(FILE *f);
9 void write_or_die(int fd, const void *buf, size_t count);
10
11 /*
12 * These values are used to help identify parts of a repository to fsync.
13 * FSYNC_COMPONENT_NONE identifies data that will not be a persistent part of the
14 * repository and so shouldn't be fsynced.
15 */
16 enum fsync_component {
17 FSYNC_COMPONENT_NONE,
18 FSYNC_COMPONENT_LOOSE_OBJECT = 1 << 0,
19 FSYNC_COMPONENT_PACK = 1 << 1,
20 FSYNC_COMPONENT_PACK_METADATA = 1 << 2,
21 FSYNC_COMPONENT_COMMIT_GRAPH = 1 << 3,
22 FSYNC_COMPONENT_INDEX = 1 << 4,
23 FSYNC_COMPONENT_REFERENCE = 1 << 5,
24 FSYNC_COMPONENT_OBJECT_MAP = 1 << 6,
25 };
26
27 #define FSYNC_COMPONENTS_OBJECTS (FSYNC_COMPONENT_LOOSE_OBJECT | \
28 FSYNC_COMPONENT_PACK)
29
30 #define FSYNC_COMPONENTS_DERIVED_METADATA (FSYNC_COMPONENT_PACK_METADATA | \
31 FSYNC_COMPONENT_COMMIT_GRAPH)
32
33 #define FSYNC_COMPONENTS_DEFAULT ((FSYNC_COMPONENTS_OBJECTS | \
34 FSYNC_COMPONENTS_DERIVED_METADATA) & \
35 ~FSYNC_COMPONENT_LOOSE_OBJECT)
36
37 #define FSYNC_COMPONENTS_COMMITTED (FSYNC_COMPONENTS_OBJECTS | \
38 FSYNC_COMPONENT_REFERENCE)
39
40 #define FSYNC_COMPONENTS_ADDED (FSYNC_COMPONENTS_COMMITTED | \
41 FSYNC_COMPONENT_INDEX)
42
43 #define FSYNC_COMPONENTS_ALL (FSYNC_COMPONENT_LOOSE_OBJECT | \
44 FSYNC_COMPONENT_PACK | \
45 FSYNC_COMPONENT_PACK_METADATA | \
46 FSYNC_COMPONENT_COMMIT_GRAPH | \
47 FSYNC_COMPONENT_INDEX | \
48 FSYNC_COMPONENT_REFERENCE | \
49 FSYNC_COMPONENT_OBJECT_MAP)
50
51 #ifndef FSYNC_COMPONENTS_PLATFORM_DEFAULT
52 #define FSYNC_COMPONENTS_PLATFORM_DEFAULT FSYNC_COMPONENTS_DEFAULT
53 #endif
54
55 /* IO helper functions */
56 void fsync_or_die(int fd, const char *);
57 int fsync_component(enum fsync_component component, int fd);
58 void fsync_component_or_die(enum fsync_component component, int fd, const char *msg);
59
60 /*
61 * A bitmask indicating which components of the repo should be fsynced.
62 */
63 extern enum fsync_component fsync_components;
64 extern int fsync_object_files;
65 extern int use_fsync;
66
67 enum fsync_method {
68 FSYNC_METHOD_FSYNC,
69 FSYNC_METHOD_WRITEOUT_ONLY,
70 FSYNC_METHOD_BATCH,
71 };
72
73 extern enum fsync_method fsync_method;
74
75 static inline int batch_fsync_enabled(enum fsync_component component)
76 {
77 return (fsync_components & component) && (fsync_method == FSYNC_METHOD_BATCH);
78 }
79
80 #endif /* WRITE_OR_DIE_H */