diff options
-rw-r--r-- | commands/memset.c | 2 | ||||
-rw-r--r-- | commands/mm.c | 2 | ||||
-rw-r--r-- | commands/mw.c | 2 | ||||
-rw-r--r-- | fs/ramfs.c | 2 | ||||
-rw-r--r-- | lib/libfile.c | 20 |
5 files changed, 23 insertions, 5 deletions
diff --git a/commands/memset.c b/commands/memset.c index f871e07c95..f99bf86c04 100644 --- a/commands/memset.c +++ b/commands/memset.c @@ -56,7 +56,7 @@ static int do_memset(int argc, char *argv[]) c = strtoull_suffix(argv[optind + 1], NULL, 0); n = strtoull_suffix(argv[optind + 2], NULL, 0); - fd = open_and_lseek(file, mode | O_WRONLY, s); + fd = open_and_lseek(file, mode | O_WRONLY | O_CREAT, s); if (fd < 0) return 1; diff --git a/commands/mm.c b/commands/mm.c index 6d2a887892..c7f62fca54 100644 --- a/commands/mm.c +++ b/commands/mm.c @@ -53,7 +53,7 @@ static int do_mem_mm(int argc, char *argv[]) value = simple_strtoull(argv[optind++], NULL, 0); mask = simple_strtoull(argv[optind++], NULL, 0); - fd = open_and_lseek(filename, mode | O_RDWR, adr); + fd = open_and_lseek(filename, mode | O_RDWR | O_CREAT, adr); if (fd < 0) return 1; diff --git a/commands/mw.c b/commands/mw.c index 7ff589abb1..2912997a31 100644 --- a/commands/mw.c +++ b/commands/mw.c @@ -52,7 +52,7 @@ static int do_mem_mw(int argc, char *argv[]) adr = strtoull_suffix(argv[optind++], NULL, 0); - fd = open_and_lseek(filename, mode | O_WRONLY, adr); + fd = open_and_lseek(filename, mode | O_WRONLY | O_CREAT, adr); if (fd < 0) return 1; diff --git a/fs/ramfs.c b/fs/ramfs.c index fb8d0580d8..7548bdac9f 100644 --- a/fs/ramfs.c +++ b/fs/ramfs.c @@ -109,7 +109,7 @@ static struct ramfs_chunk *ramfs_get_chunk(void) if (!data) return NULL; - data->data = malloc(CHUNK_SIZE); + data->data = calloc(CHUNK_SIZE, 1); if (!data->data) { free(data); return NULL; diff --git a/lib/libfile.c b/lib/libfile.c index d22519b8f4..0052e789fc 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -501,7 +501,7 @@ int open_and_lseek(const char *filename, int mode, loff_t pos) { int fd, ret; - fd = open(filename, mode | O_RDONLY); + fd = open(filename, mode); if (fd < 0) { perror("open"); return fd; @@ -510,6 +510,24 @@ int open_and_lseek(const char *filename, int mode, loff_t pos) if (!pos) return fd; + if (mode & (O_WRONLY | O_RDWR)) { + struct stat s; + + ret = fstat(fd, &s); + if (ret) { + perror("fstat"); + return ret; + } + + if (s.st_size < pos) { + ret = ftruncate(fd, pos); + if (ret) { + perror("ftruncate"); + return ret; + } + } + } + ret = lseek(fd, pos, SEEK_SET); if (ret == -1) { perror("lseek"); |