aboutsummaryrefslogtreecommitdiff
path: root/files.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-03-03 17:24:23 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-03-03 17:24:23 -0800
commitced0004c87662060958250fb9e294b4f39906e24 (patch)
tree579e0fac469824d6d80f5d597e08b53ae0a1e24e /files.c
parentb9c2024743ba57e179ab675448d0d6d1166086a4 (diff)
Fix for permissions/ownership getting screwed up with inplace modifying
of files. With the new change, temporary files are no longer created on the filesystem and the file is entirely modified inplace (with an in-memory copy kept on hand for matching).
Diffstat (limited to 'files.c')
-rw-r--r--files.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/files.c b/files.c
index 4218c56..4192f9e 100644
--- a/files.c
+++ b/files.c
@@ -67,7 +67,7 @@ file_t *load_file(file_t **files, const char *filename)
if (fd < 0) return NULL;
size_t length;
file_t *f = new(file_t);
- f->filename = strdup(filename);
+ f->filename = memcheck(strdup(filename));
struct stat sb;
if (fstat(fd, &sb) == -1)
@@ -110,13 +110,15 @@ file_t *load_file(file_t **files, const char *filename)
//
// Create a virtual file from a string.
//
-file_t *spoof_file(file_t **files, const char *filename, const char *text)
+file_t *spoof_file(file_t **files, const char *filename, const char *text, ssize_t _len)
{
if (filename == NULL) filename = "";
file_t *f = new(file_t);
- f->filename = strdup(filename);
- f->contents = strdup(text);
- f->end = &f->contents[strlen(text)];
+ size_t len = _len == -1 ? strlen(text) : (size_t)_len;
+ f->filename = memcheck(strdup(filename));
+ f->contents = xcalloc(len+1, sizeof(char));
+ memcpy(f->contents, text, len);
+ f->end = &f->contents[len];
populate_lines(f);
if (files != NULL) {
f->next = *files;