Mercurial > libjeffpc
changeset 699:67a81f305a11
file cache: import the bare minimum implementation
This will act as the fallback implementation if no modern event monitoring
system API is available.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Wed, 13 Mar 2019 10:32:54 -0400 |
parents | 6a94380509f8 |
children | de3dce4eb8ca |
files | CMakeLists.txt file_cache.c include/jeffpc/file-cache.h mapfile-vers |
diffstat | 4 files changed, 107 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Sun Mar 17 09:56:34 2019 -0400 +++ b/CMakeLists.txt Wed Mar 13 10:32:54 2019 -0400 @@ -92,6 +92,7 @@ buffer_stdio.c cstr.c error.c + file_cache.c fmt_cbor.c hexdump.c init.c @@ -156,6 +157,7 @@ include/jeffpc/buffer.h include/jeffpc/cstr.h include/jeffpc/error.h + include/jeffpc/file-cache.h include/jeffpc/hexdump.h include/jeffpc/int.h include/jeffpc/io.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/file_cache.c Wed Mar 13 10:32:54 2019 -0400 @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include <jeffpc/file-cache.h> +#include <jeffpc/io.h> + +/* + * This is the fallback implementation. It doesn't actually do any caching, + * it simply reads the file contents every time. + */ + +#pragma weak file_cache_init +int file_cache_init(void) +{ + /* nothing to do */ + return 0; +} + +#pragma weak file_cache_get +struct str *file_cache_get(const char *name, uint64_t *rev) +{ + struct stat statbuf; + char *tmp; + + if (rev) + *rev = 0; + + tmp = read_file_common(AT_FDCWD, name, &statbuf); + if (IS_ERR(tmp)) + return ERR_CAST(tmp); + + return str_alloc(tmp); +} + +#pragma weak file_cache_has_newer +bool file_cache_has_newer(const char *name, uint64_t rev) +{ + /* pretend that the file has changed to make the caller read it again */ + return true; +} + +#pragma weak file_cache_uncache_all +void file_cache_uncache_all(void) +{ + /* nothing to do */ +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/jeffpc/file-cache.h Wed Mar 13 10:32:54 2019 -0400 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __JEFFPC_FILE_CACHE_H +#define __JEFFPC_FILE_CACHE_H + +#include <jeffpc/int.h> +#include <jeffpc/str.h> + +extern int file_cache_init(void); +extern void file_cache_uncache_all(void); +extern struct str *file_cache_get(const char *name, uint64_t *rev); +extern bool file_cache_has_newer(const char *name, uint64_t rev); + +#endif