summaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-03-18 17:59:46 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2013-06-20 08:42:46 +0200
commit2d2ec6619b84fd70ceaa5f4a347c428ff24339ad (patch)
tree09d43c20189826110822f3bb3b33ab78483804cf /fs
parent1e2cc039145bf41c1472eb17411622642017bac0 (diff)
downloadbarebox-2d2ec6619b84fd70ceaa5f4a347c428ff24339ad.tar.gz
barebox-2d2ec6619b84fd70ceaa5f4a347c428ff24339ad.tar.xz
read_file: Make it work on tftp servers which do not pass size
Some tftp servers (for example netkit-tftp) do not pass the filesize. Add a workaround for read_file which reads the file into a temporary file which then is copied to a buffer. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'fs')
-rw-r--r--fs/fs.c18
-rw-r--r--fs/tftp.c5
2 files changed, 22 insertions, 1 deletions
diff --git a/fs/fs.c b/fs/fs.c
index 7e2fb78845..d913a503f2 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -38,10 +38,21 @@ void *read_file(const char *filename, size_t *size)
int fd;
struct stat s;
void *buf = NULL;
+ const char *tmpfile = "/.read_file_tmp";
+ int ret;
+again:
if (stat(filename, &s))
return NULL;
+ if (s.st_size == FILESIZE_MAX) {
+ ret = copy_file(filename, tmpfile, 0);
+ if (ret)
+ return NULL;
+ filename = tmpfile;
+ goto again;
+ }
+
buf = xzalloc(s.st_size + 1);
fd = open(filename, O_RDONLY);
@@ -56,12 +67,19 @@ void *read_file(const char *filename, size_t *size)
if (size)
*size = s.st_size;
+ if (filename == tmpfile)
+ unlink(tmpfile);
+
return buf;
err_out1:
close(fd);
err_out:
free(buf);
+
+ if (filename == tmpfile)
+ unlink(tmpfile);
+
return NULL;
}
diff --git a/fs/tftp.c b/fs/tftp.c
index f86a7d1b0f..50efe0d77c 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -598,7 +598,10 @@ static int tftp_stat(struct device_d *dev, const char *filename, struct stat *s)
return PTR_ERR(priv);
s->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
- s->st_size = priv->filesize;
+ if (priv->filesize)
+ s->st_size = priv->filesize;
+ else
+ s->st_size = FILESIZE_MAX;
tftp_do_close(priv);