You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Stas Bekman <st...@stason.org> on 2002/04/17 11:01:46 UTC

apr file questions

I've two questions regarding coding with apr_file_ family

1. Is it a good idea to apr_file_mktemp with APR_BUFFERED flag on? The 
current default is off.

2. What's the idiomatic apr read till eof? From grepping the source 
code, I see apr_file_eof is hardly ever used. Is something wrong with:

         while (!apr_file_eof(fp)) {
             rc = apr_file_read(fp, buf, &nbytes);
             ...
         }

I saw code like this:

         while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
         {
            ...
         }

which one is better to use?

Thanks!
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Re: apr file questions

Posted by Jeff Trawick <tr...@attglobal.net>.
Stas Bekman <st...@stason.org> writes:

> Jeff Trawick wrote:
> > Stas Bekman <st...@stason.org> writes:
> >
> >>2. What's the idiomatic apr read till eof? From grepping the source
> >>code, I see apr_file_eof is hardly ever used. Is something wrong with:
> >>
> >>         while (!apr_file_eof(fp)) {
> >>             rc = apr_file_read(fp, buf, &nbytes);
> >>             ...
> >>         }
> >>
> >>I saw code like this:
> >>
> >>         while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
> >>         {
> >>            ...
> >>         }
> >>
> >>which one is better to use?
> > I would suggest the second one.  What if you hit an I/O error?
> 
> That's what rc = apr_file_read() is for, the following line with
> ... is for error checking.
> 
>  > I
>  > always did
>  >
>  >   while (!feof() && !ferror())
>  >
>  > with stdio.
> 
> If you use a second or your example and have an IO error, this code
> won't be able to tell eof from an error, and simply move on. Is this
> good?

obviously you would save the apr_status_t :)

-- 
Jeff Trawick | trawick@attglobal.net
Born in Roswell... married an alien...

Re: apr file questions

Posted by Stas Bekman <st...@stason.org>.
Jeff Trawick wrote:
> Stas Bekman <st...@stason.org> writes:
> 
> 
>>2. What's the idiomatic apr read till eof? From grepping the source
>>code, I see apr_file_eof is hardly ever used. Is something wrong with:
>>
>>         while (!apr_file_eof(fp)) {
>>             rc = apr_file_read(fp, buf, &nbytes);
>>             ...
>>         }
>>
>>I saw code like this:
>>
>>         while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
>>         {
>>            ...
>>         }
>>
>>which one is better to use?
> 
> 
> I would suggest the second one.  What if you hit an I/O error? 

That's what rc = apr_file_read() is for, the following line with ... is 
for error checking.

 > I
 > always did
 >
 >   while (!feof() && !ferror())
 >
 > with stdio.

If you use a second or your example and have an IO error, this code 
won't be able to tell eof from an error, and simply move on. Is this good?

Thanks Jeff
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Re: apr file questions

Posted by Jon Travis <jt...@covalent.net>.
On Thu, Apr 18, 2002 at 10:40:12AM -0400, Cliff Woolley wrote:
> On 18 Apr 2002, Jeff Trawick wrote:
> 
> Since APR_EOF is one of the errors you might get back from
> apr_file_read(), I agree with Jeff.  This:
> 
> > >          while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
> > >          {
> > >             ...
> > >          }
> 
> is probably preferable.  You'll most likely want to hang onto that return
> code, though, like this:
> 
>              while ((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS)
>              {
>                 ...
>              }

Of course there is some amount of suckiness associated with this, because
len will get mucked with by apr_file_read.  I've seen this happen in more
than one case where len gets reset so short reads occur.  Ideally you
always get the initial size back until the last couple times, but for files
which grow as you are reading them, etc. this won't work out very well.
So, you'd need:

len = sizeof(buffer)
while((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS){
   ...
   len = sizeof(buffer)
}

I've been preferring to stick the buffer scoped locally to the loop, and
have the read occur inside -- that way there is only one initialization of
the length, and it's obvious:

while(!apr_file_eof(file)){
   char somebuf[1024];
   apr_size_t len = sizeof(somebuf);

   if((rc = apr_file_read(file, somebuf, &len) .. blah blah blah
}

-- Jon


Re: apr file questions

Posted by Stas Bekman <st...@stason.org>.
Cliff Woolley wrote:
> On 18 Apr 2002, Jeff Trawick wrote:
> 
> Since APR_EOF is one of the errors you might get back from
> apr_file_read(), I agree with Jeff.  This:
> 
> 
>>>         while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
>>>         {
>>>            ...
>>>         }
>>
> 
> is probably preferable.  You'll most likely want to hang onto that return
> code, though, like this:
> 
>              while ((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS)
>              {
>                 ...
>              }

Thanks Cliff and Jeff. Now I know how to do it right.


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com



Re: apr file questions

Posted by Cliff Woolley <jw...@virginia.edu>.
On 18 Apr 2002, Jeff Trawick wrote:

Since APR_EOF is one of the errors you might get back from
apr_file_read(), I agree with Jeff.  This:

> >          while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
> >          {
> >             ...
> >          }

is probably preferable.  You'll most likely want to hang onto that return
code, though, like this:

             while ((rc = apr_file_read(file, buffer, &len)) != APR_SUCCESS)
             {
                ...
             }

--Cliff

--------------------------------------------------------------
   Cliff Woolley
   cliffwoolley@yahoo.com
   Charlottesville, VA



Re: apr file questions

Posted by Jeff Trawick <tr...@attglobal.net>.
Stas Bekman <st...@stason.org> writes:

> 2. What's the idiomatic apr read till eof? From grepping the source
> code, I see apr_file_eof is hardly ever used. Is something wrong with:
> 
>          while (!apr_file_eof(fp)) {
>              rc = apr_file_read(fp, buf, &nbytes);
>              ...
>          }
> 
> I saw code like this:
> 
>          while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
>          {
>             ...
>          }
> 
> which one is better to use?

I would suggest the second one.  What if you hit an I/O error?  I
always did

  while (!feof() && !ferror())

with stdio.

-- 
Jeff Trawick | trawick@attglobal.net
Born in Roswell... married an alien...