file-cache.h revision c18dcef4da347b735861aa35fc6518c6c2e97755
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen#ifndef FILE_CACHE_H
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen#define FILE_CACHE_H
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Create a new file cache. It works very much like file-backed mmap()ed
bdd36cfdba3ff66d25570a9ff568d69e1eb543cfTimo Sirainen memory, but it works more nicely with remote filesystems (no SIGBUS). */
b1f37113a5760bee842c5a7678bb5fa6f5bd8b60Timo Sirainenstruct file_cache *file_cache_new(int fd);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenstruct file_cache *file_cache_new_path(int fd, const char *path);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Destroy the cache and set cache pointer to NULL. */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenvoid file_cache_free(struct file_cache **cache);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Change cached file descriptor. Invalidates the whole cache. */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenvoid file_cache_set_fd(struct file_cache *cache, int fd);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Change the memory allocated for the cache. This can be used to immediately
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen set the maximum size so there's no need to grow the memory area with
a23210844b06c53ebca34b162f3a268d5f1cd4d1Aki Tuomi possibly slow copying. */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenint file_cache_set_size(struct file_cache *cache, uoff_t size);
530b5909a99e7575156496f1b8e4d1ff5b058484Timo Sirainen
530b5909a99e7575156496f1b8e4d1ff5b058484Timo Sirainen/* Read data from file, returns how many bytes was actually read or -1 if
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen error occurred. */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenssize_t file_cache_read(struct file_cache *cache, uoff_t offset, size_t size);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Returns pointer to beginning of cached file. Only parts of the returned
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen memory that are valid are the ones that have been file_cache_read().
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen Note that the pointer may become invalid after calling file_cache_read(). */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenconst void *file_cache_get_map(struct file_cache *cache, size_t *size_r);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Update cached memory area. Mark fully written pages as cached. */
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainenvoid file_cache_write(struct file_cache *cache, const void *data, size_t size,
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen uoff_t offset);
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen/* Invalidate cached memory area. It will be read again next time it's tried
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen to be accessed. */
70ac869db925653b57f721cd045c467612fd5ee9Timo Sirainenvoid file_cache_invalidate(struct file_cache *cache,
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen uoff_t offset, uoff_t size);
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen#endif
1c633f71ec2060e5bfa500a97f34cd881a958ecdTimo Sirainen