/src/libgit2/src/util/filebuf.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) the libgit2 contributors. All rights reserved. |
3 | | * |
4 | | * This file is part of libgit2, distributed under the GNU GPL v2 with |
5 | | * a Linking Exception. For full terms see the included COPYING file. |
6 | | */ |
7 | | #ifndef INCLUDE_filebuf_h__ |
8 | | #define INCLUDE_filebuf_h__ |
9 | | |
10 | | #include "git2_util.h" |
11 | | |
12 | | #include "futils.h" |
13 | | #include "hash.h" |
14 | | #include <zlib.h> |
15 | | |
16 | | #ifdef GIT_THREADS |
17 | | # define GIT_FILEBUF_THREADS |
18 | | #endif |
19 | | |
20 | 0 | #define GIT_FILEBUF_HASH_SHA1 (1 << 0) |
21 | 0 | #define GIT_FILEBUF_HASH_SHA256 (1 << 1) |
22 | 0 | #define GIT_FILEBUF_APPEND (1 << 2) |
23 | 0 | #define GIT_FILEBUF_CREATE_LEADING_DIRS (1 << 3) |
24 | 0 | #define GIT_FILEBUF_TEMPORARY (1 << 4) |
25 | 0 | #define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5) |
26 | 0 | #define GIT_FILEBUF_FSYNC (1 << 6) |
27 | 0 | #define GIT_FILEBUF_DEFLATE_SHIFT (7) |
28 | | |
29 | 0 | #define GIT_FILELOCK_EXTENSION ".lock\0" |
30 | 0 | #define GIT_FILELOCK_EXTLENGTH 6 |
31 | | |
32 | | typedef struct git_filebuf git_filebuf; |
33 | | struct git_filebuf { |
34 | | char *path_original; |
35 | | char *path_lock; |
36 | | |
37 | | int (*write)(git_filebuf *file, void *source, size_t len); |
38 | | |
39 | | bool compute_digest; |
40 | | git_hash_ctx digest; |
41 | | |
42 | | unsigned char *buffer; |
43 | | unsigned char *z_buf; |
44 | | |
45 | | z_stream zs; |
46 | | int flush_mode; |
47 | | |
48 | | size_t buf_size, buf_pos; |
49 | | git_file fd; |
50 | | bool fd_is_open; |
51 | | bool created_lock; |
52 | | bool did_rename; |
53 | | bool do_not_buffer; |
54 | | bool do_fsync; |
55 | | int last_error; |
56 | | }; |
57 | | |
58 | 0 | #define GIT_FILEBUF_INIT {0} |
59 | | |
60 | | /* |
61 | | * The git_filebuf object lifecycle is: |
62 | | * - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT. |
63 | | * |
64 | | * - Call git_filebuf_open() to initialize the filebuf for use. |
65 | | * |
66 | | * - Make as many calls to git_filebuf_write(), git_filebuf_printf(), |
67 | | * git_filebuf_reserve() as you like. The error codes for these |
68 | | * functions don't need to be checked. They are stored internally |
69 | | * by the file buffer. |
70 | | * |
71 | | * - While you are writing, you may call git_filebuf_hash() to get |
72 | | * the hash of all you have written so far. This function will |
73 | | * fail if any of the previous writes to the buffer failed. |
74 | | * |
75 | | * - To close the git_filebuf, you may call git_filebuf_commit() or |
76 | | * git_filebuf_commit_at() to save the file, or |
77 | | * git_filebuf_cleanup() to abandon the file. All of these will |
78 | | * free the git_filebuf object. Likewise, all of these will fail |
79 | | * if any of the previous writes to the buffer failed, and set |
80 | | * an error code accordingly. |
81 | | */ |
82 | | int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len); |
83 | | int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len); |
84 | | int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); |
85 | | |
86 | | int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode); |
87 | | int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size); |
88 | | int git_filebuf_commit(git_filebuf *lock); |
89 | | int git_filebuf_commit_at(git_filebuf *lock, const char *path); |
90 | | void git_filebuf_cleanup(git_filebuf *lock); |
91 | | int git_filebuf_hash(unsigned char *out, git_filebuf *file); |
92 | | int git_filebuf_flush(git_filebuf *file); |
93 | | int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file); |
94 | | |
95 | | GIT_INLINE(int) git_filebuf_hash_flags(git_hash_algorithm_t algorithm) |
96 | 0 | { |
97 | 0 | switch (algorithm) { |
98 | 0 | case GIT_HASH_ALGORITHM_SHA1: |
99 | 0 | return GIT_FILEBUF_HASH_SHA1; |
100 | 0 | case GIT_HASH_ALGORITHM_SHA256: |
101 | 0 | return GIT_FILEBUF_HASH_SHA256; |
102 | 0 | default: |
103 | 0 | return 0; |
104 | 0 | } |
105 | 0 | } Unexecuted instantiation: commit_graph.c:git_filebuf_hash_flags Unexecuted instantiation: merge_file.c:git_filebuf_hash_flags Unexecuted instantiation: odb_loose.c:git_filebuf_hash_flags Unexecuted instantiation: repository.c:git_filebuf_hash_flags Unexecuted instantiation: settings.c:git_filebuf_hash_flags Unexecuted instantiation: submodule.c:git_filebuf_hash_flags Unexecuted instantiation: tree.c:git_filebuf_hash_flags Unexecuted instantiation: filebuf.c:git_filebuf_hash_flags Unexecuted instantiation: attr_file.c:git_filebuf_hash_flags Unexecuted instantiation: blob.c:git_filebuf_hash_flags Unexecuted instantiation: checkout.c:git_filebuf_hash_flags Unexecuted instantiation: config_file.c:git_filebuf_hash_flags Unexecuted instantiation: diff.c:git_filebuf_hash_flags Unexecuted instantiation: diff_generate.c:git_filebuf_hash_flags Unexecuted instantiation: diff_tform.c:git_filebuf_hash_flags Unexecuted instantiation: email.c:git_filebuf_hash_flags Unexecuted instantiation: index.c:git_filebuf_hash_flags Unexecuted instantiation: indexer.c:git_filebuf_hash_flags Unexecuted instantiation: iterator.c:git_filebuf_hash_flags Unexecuted instantiation: merge.c:git_filebuf_hash_flags Unexecuted instantiation: midx.c:git_filebuf_hash_flags Unexecuted instantiation: patch_generate.c:git_filebuf_hash_flags Unexecuted instantiation: pathspec.c:git_filebuf_hash_flags Unexecuted instantiation: refdb_fs.c:git_filebuf_hash_flags Unexecuted instantiation: reflog.c:git_filebuf_hash_flags Unexecuted instantiation: refs.c:git_filebuf_hash_flags Unexecuted instantiation: diff_file.c:git_filebuf_hash_flags Unexecuted instantiation: fetchhead.c:git_filebuf_hash_flags |
106 | | |
107 | | #endif |