mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Jan Weitzel <J.Weitzel@phytec.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] read_file: Make it work on tftp servers which do not pass size
Date: Thu, 20 Jun 2013 17:24:28 +0200	[thread overview]
Message-ID: <20130620152428.GU32299@pengutronix.de> (raw)
In-Reply-To: <1371726931.3466.3.camel@lws-weitzel>

On Thu, Jun 20, 2013 at 01:15:31PM +0200, Jan Weitzel wrote:
> Am Mittwoch, den 19.06.2013, 22:58 +0200 schrieb Sascha Hauer:
> > 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>
> > ---
> >  fs/fs.c      | 18 ++++++++++++++++++
> >  fs/tftp.c    |  5 ++++-
> >  include/fs.h |  2 ++
> >  3 files changed, 24 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fs/fs.c b/fs/fs.c
> > index dc3a6e3..7046f2c 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 98cbb37..1c37acf 100644
> > --- a/fs/tftp.c
> > +++ b/fs/tftp.c
> > @@ -597,7 +597,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;
> Maybe we can determine the size here? Commands like "ls -l" and
> "ubiformat" asks about the filesize via stat. I'm not sure how much
> overheat it will be.

How do you want to do that? You would have to transfer the whole file
first and see how big it is. That works for small files we expect to fit
into memory like the ones read_file normally is called with. If you want
to transfer a rootfs image it might happen that it's bigger than the
available memory.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2013-06-20 15:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-19 20:58 Sascha Hauer
2013-06-19 21:32 ` Alexander Aring
2013-06-19 21:57   ` Alexander Aring
2013-06-20  6:42     ` Sascha Hauer
2013-06-20  7:28       ` Alexander Aring
2013-06-20  8:12         ` Sascha Hauer
2013-06-20 11:15 ` Jan Weitzel
2013-06-20 15:24   ` Sascha Hauer [this message]
2013-06-21  7:03     ` Jan Weitzel
2013-06-21  7:10       ` Sascha Hauer
2013-06-21  7:46         ` Jan Weitzel
2013-06-21  8:21           ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130620152428.GU32299@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=J.Weitzel@phytec.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox