diff options
author | Uwe Kleine-König <u.kleine-koenig@pengutronix.de> | 2018-08-09 10:33:12 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-08-10 08:21:41 +0200 |
commit | 686d35f88db5779786b862205a503cffdb1a5bde (patch) | |
tree | 35d6e214088baee1845c0a78464d0a9c1d5fb77f /lib | |
parent | cde598f22ea3460d411fb9a1e5ec4d24bf3882d9 (diff) | |
download | barebox-686d35f88db5779786b862205a503cffdb1a5bde.tar.gz |
libfile: open_and_lseek: enlarge small files enough to make lseek possible
This makes the following do the expected thing:
barebox@barebox sandbox:/ ls -l lala
-rwxrwxrwx 4 lala
barebox@barebox sandbox:/ mw -d lala 72 0
Without this patch mw dies with
lseek: Invalid argument
memset, memcpy and probably others benefit in the same way.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libfile.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/libfile.c b/lib/libfile.c index 83c6399a5b..0052e789fc 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -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"); |