Skip to content

Commit

Permalink
libfile: implement a function to cache a file
Browse files Browse the repository at this point in the history
Due to the nature of TFTP which can't lseek and due to the silliness
of our filesystem implementation which can't cache accesses we have to
manually cache files on TFTP filesystems sometimes. Make it easier
for them by providing a cache_file() function which copies the file
from TFTP to RAM.

Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
saschahauer committed Jan 25, 2018
1 parent 94e561b commit e660e51
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/libfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ int unlink_recursive(const char *path, char **failedpath);

char *make_temp(const char *template);

int cache_file(const char *path, char **newpath);

#endif /* __LIBFILE_H */
30 changes: 29 additions & 1 deletion lib/libfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ int open_and_lseek(const char *filename, int mode, loff_t pos)
* create a unique filename.
*
* Return: This function returns a filename which can be used as a temporary
* file lateron. The returned filename must be freed by the caller.
* file later on. The returned filename must be freed by the caller.
*/
char *make_temp(const char *template)
{
Expand All @@ -512,3 +512,31 @@ char *make_temp(const char *template)

return name;
}

/**
* cache_file - Cache a file in /tmp
* @path: The file to cache
* @newpath: The return path where the file is copied to
*
* This function copies a given file to /tmp and returns its name in @newpath.
* @newpath is dynamically allocated and must be freed by the caller.
*
* Return: 0 for success, negative error code otherwise.
*/
int cache_file(const char *path, char **newpath)
{
char *npath;
int ret;

npath = make_temp("filecache");

ret = copy_file(path, npath, 0);
if (ret) {
free(npath);
return ret;
}

*newpath = npath;

return 0;
}

0 comments on commit e660e51

Please sign in to comment.