file-cache.h revision d5cebe7f98e63d4e2822863ef2faa4971e8b3a5d
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen#ifndef __FILE_CACHE_H
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen#define __FILE_CACHE_H
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen/* Create a new file cache. It works very much like file-backed mmap()ed
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen memory, but it works more nicely with remote filesystems (no SIGBUS). */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenstruct file_cache *file_cache_new(int fd);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen/* Destroy the cache and set cache pointer to NULL. */
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid file_cache_free(struct file_cache **cache);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen/* Change cached file descriptor. Invalidates the whole cache. */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenvoid file_cache_set_fd(struct file_cache *cache, int fd);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen/* Read data from file, returns how many bytes was actually read or -1 if
137ea7ca34005345aa2304a940149b7f3774d727Timo Sirainen error occurred. */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenssize_t file_cache_read(struct file_cache *cache, uoff_t offset, size_t size);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen/* Returns pointer to beginning of cached file. Only parts of the returned
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen memory that are valid are the ones that have been file_cache_read().
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen Note that the pointer may become invalid after calling file_cache_read(). */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenconst void *file_cache_get_map(struct file_cache *cache, size_t *size_r);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
b22f3db16d33b8e509b7331e46108c313e7f3b94Timo Sirainen/* Update cached memory area. Mark fully written pages as cached. */
b22f3db16d33b8e509b7331e46108c313e7f3b94Timo Sirainenvoid file_cache_write(struct file_cache *cache, const void *data, size_t size,
b22f3db16d33b8e509b7331e46108c313e7f3b94Timo Sirainen uoff_t offset);
b22f3db16d33b8e509b7331e46108c313e7f3b94Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen/* Invalidate cached memory area. It will be read again next time it's tried
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen to be accessed. */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenvoid file_cache_invalidate(struct file_cache *cache,
74eff344f67ca582eec8c38dfdc6f5f6faa21fd8Timo Sirainen uoff_t offset, uoff_t size);
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainen#endif