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