]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
bb742ede VM |
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 | */ | |
817c2820 VM |
7 | #ifndef INCLUDE_filebuf_h__ |
8 | #define INCLUDE_filebuf_h__ | |
9 | ||
10 | #include "fileops.h" | |
11 | #include "hash.h" | |
0c3bae62 | 12 | #include <zlib.h> |
817c2820 VM |
13 | |
14 | #ifdef GIT_THREADS | |
15 | # define GIT_FILEBUF_THREADS | |
16 | #endif | |
17 | ||
72a3fe42 VM |
18 | #define GIT_FILEBUF_HASH_CONTENTS (1 << 0) |
19 | #define GIT_FILEBUF_APPEND (1 << 2) | |
20 | #define GIT_FILEBUF_FORCE (1 << 3) | |
21 | #define GIT_FILEBUF_TEMPORARY (1 << 4) | |
4d53f3e2 CMN |
22 | #define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5) |
23 | #define GIT_FILEBUF_DEFLATE_SHIFT (6) | |
817c2820 | 24 | |
86194b24 VM |
25 | #define GIT_FILELOCK_EXTENSION ".lock\0" |
26 | #define GIT_FILELOCK_EXTLENGTH 6 | |
27 | ||
06280457 | 28 | typedef struct git_filebuf git_filebuf; |
817c2820 VM |
29 | struct git_filebuf { |
30 | char *path_original; | |
31 | char *path_lock; | |
32 | ||
06280457 | 33 | int (*write)(git_filebuf *file, void *source, size_t len); |
72a3fe42 | 34 | |
2a612fe3 ET |
35 | bool compute_digest; |
36 | git_hash_ctx digest; | |
817c2820 VM |
37 | |
38 | unsigned char *buffer; | |
72a3fe42 VM |
39 | unsigned char *z_buf; |
40 | ||
41 | z_stream zs; | |
42 | int flush_mode; | |
817c2820 VM |
43 | |
44 | size_t buf_size, buf_pos; | |
45 | git_file fd; | |
e1de726c | 46 | bool fd_is_open; |
4d53f3e2 | 47 | bool do_not_buffer; |
dda708e7 | 48 | int last_error; |
817c2820 VM |
49 | }; |
50 | ||
b762e576 RB |
51 | #define GIT_FILEBUF_INIT {0} |
52 | ||
dda708e7 VM |
53 | /* |
54 | * The git_filebuf object lifecycle is: | |
b762e576 | 55 | * - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT. |
dda708e7 | 56 | * |
b762e576 | 57 | * - Call git_filebuf_open() to initialize the filebuf for use. |
dda708e7 | 58 | * |
b762e576 | 59 | * - Make as many calls to git_filebuf_write(), git_filebuf_printf(), |
dda708e7 VM |
60 | * git_filebuf_reserve() as you like. The error codes for these |
61 | * functions don't need to be checked. They are stored internally | |
62 | * by the file buffer. | |
63 | * | |
b762e576 | 64 | * - While you are writing, you may call git_filebuf_hash() to get |
dda708e7 VM |
65 | * the hash of all you have written so far. This function will |
66 | * fail if any of the previous writes to the buffer failed. | |
67 | * | |
b762e576 RB |
68 | * - To close the git_filebuf, you may call git_filebuf_commit() or |
69 | * git_filebuf_commit_at() to save the file, or | |
70 | * git_filebuf_cleanup() to abandon the file. All of these will | |
dda708e7 VM |
71 | * free the git_filebuf object. Likewise, all of these will fail |
72 | * if any of the previous writes to the buffer failed, and set | |
73 | * an error code accordingly. | |
b762e576 | 74 | */ |
72a3fe42 | 75 | int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len); |
817c2820 | 76 | int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len); |
afeecf4f | 77 | int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); |
817c2820 | 78 | |
1d3a8aeb ET |
79 | int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode); |
80 | int git_filebuf_commit(git_filebuf *lock); | |
81 | int git_filebuf_commit_at(git_filebuf *lock, const char *path); | |
817c2820 VM |
82 | void git_filebuf_cleanup(git_filebuf *lock); |
83 | int git_filebuf_hash(git_oid *oid, git_filebuf *file); | |
9bea8e85 | 84 | int git_filebuf_flush(git_filebuf *file); |
744cc03e | 85 | int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file); |
817c2820 VM |
86 | |
87 | #endif |