|
Packit |
ae9e2a |
/*
|
|
Packit |
ae9e2a |
* Copyright (C) the libgit2 contributors. All rights reserved.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
|
Packit |
ae9e2a |
* a Linking Exception. For full terms see the included COPYING file.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
#ifndef INCLUDE_filebuf_h__
|
|
Packit |
ae9e2a |
#define INCLUDE_filebuf_h__
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#include "fileops.h"
|
|
Packit |
ae9e2a |
#include "hash.h"
|
|
Packit |
ae9e2a |
#include <zlib.h>
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#ifdef GIT_THREADS
|
|
Packit |
ae9e2a |
# define GIT_FILEBUF_THREADS
|
|
Packit |
ae9e2a |
#endif
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_HASH_CONTENTS (1 << 0)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_APPEND (1 << 2)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_FORCE (1 << 3)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_TEMPORARY (1 << 4)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_FSYNC (1 << 6)
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_DEFLATE_SHIFT (7)
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#define GIT_FILELOCK_EXTENSION ".lock\0"
|
|
Packit |
ae9e2a |
#define GIT_FILELOCK_EXTLENGTH 6
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
typedef struct git_filebuf git_filebuf;
|
|
Packit |
ae9e2a |
struct git_filebuf {
|
|
Packit |
ae9e2a |
char *path_original;
|
|
Packit |
ae9e2a |
char *path_lock;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
int (*write)(git_filebuf *file, void *source, size_t len);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
bool compute_digest;
|
|
Packit |
ae9e2a |
git_hash_ctx digest;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
unsigned char *buffer;
|
|
Packit |
ae9e2a |
unsigned char *z_buf;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
z_stream zs;
|
|
Packit |
ae9e2a |
int flush_mode;
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
size_t buf_size, buf_pos;
|
|
Packit |
ae9e2a |
git_file fd;
|
|
Packit |
ae9e2a |
bool fd_is_open;
|
|
Packit |
ae9e2a |
bool created_lock;
|
|
Packit |
ae9e2a |
bool did_rename;
|
|
Packit |
ae9e2a |
bool do_not_buffer;
|
|
Packit |
ae9e2a |
bool do_fsync;
|
|
Packit |
ae9e2a |
int last_error;
|
|
Packit |
ae9e2a |
};
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#define GIT_FILEBUF_INIT {0}
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
/*
|
|
Packit |
ae9e2a |
* The git_filebuf object lifecycle is:
|
|
Packit |
ae9e2a |
* - Allocate git_filebuf, preferably using GIT_FILEBUF_INIT.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* - Call git_filebuf_open() to initialize the filebuf for use.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* - Make as many calls to git_filebuf_write(), git_filebuf_printf(),
|
|
Packit |
ae9e2a |
* git_filebuf_reserve() as you like. The error codes for these
|
|
Packit |
ae9e2a |
* functions don't need to be checked. They are stored internally
|
|
Packit |
ae9e2a |
* by the file buffer.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* - While you are writing, you may call git_filebuf_hash() to get
|
|
Packit |
ae9e2a |
* the hash of all you have written so far. This function will
|
|
Packit |
ae9e2a |
* fail if any of the previous writes to the buffer failed.
|
|
Packit |
ae9e2a |
*
|
|
Packit |
ae9e2a |
* - To close the git_filebuf, you may call git_filebuf_commit() or
|
|
Packit |
ae9e2a |
* git_filebuf_commit_at() to save the file, or
|
|
Packit |
ae9e2a |
* git_filebuf_cleanup() to abandon the file. All of these will
|
|
Packit |
ae9e2a |
* free the git_filebuf object. Likewise, all of these will fail
|
|
Packit |
ae9e2a |
* if any of the previous writes to the buffer failed, and set
|
|
Packit |
ae9e2a |
* an error code accordingly.
|
|
Packit |
ae9e2a |
*/
|
|
Packit |
ae9e2a |
int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len);
|
|
Packit |
ae9e2a |
int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
|
|
Packit |
ae9e2a |
int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode);
|
|
Packit |
ae9e2a |
int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size);
|
|
Packit |
ae9e2a |
int git_filebuf_commit(git_filebuf *lock);
|
|
Packit |
ae9e2a |
int git_filebuf_commit_at(git_filebuf *lock, const char *path);
|
|
Packit |
ae9e2a |
void git_filebuf_cleanup(git_filebuf *lock);
|
|
Packit |
ae9e2a |
int git_filebuf_hash(git_oid *oid, git_filebuf *file);
|
|
Packit |
ae9e2a |
int git_filebuf_flush(git_filebuf *file);
|
|
Packit |
ae9e2a |
int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file);
|
|
Packit |
ae9e2a |
|
|
Packit |
ae9e2a |
#endif
|