You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Erik Huelsmann <e....@gmx.net> on 2003/11/30 09:08:37 UTC

[PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

I was about to commit this change when Greg Hudson said he would not have
coded it this way, so I'm submitting for review.

The patch is also available
at:
http://encorps.dnsalias.com/svn-public/patches/897-libsvn_subr-apr_file-wrappers.log-patch

Comments please?

Log:
[[[
* subversion/libsvn_subr/io.c:
  (svn_io_file_read,svn_io_file_read_full,
   svn_io_file_write,svn_io_file_write_full): 
   New. Wrappers around apr_file_ calls returning svn_error_t's.
  (do_io_file_wrapper_cleanup): 
   New. Implements cleanup procedure for the file wrappers above.
  (file_name_get): Ensure that NULL is set on error 
   in *fname_utf8: a requirement for do_io_file_wrapper_cleanup.
  (svn_io_file_open): Return an error string formatted like the ones
   from do_io_file_wrapper_cleanup.
  (svn_io_file_close): Use do_io_file_wrapper_cleanup for 
   error handling.

* subversion/include/svn_io.h:
  Add declarations for svn_io_file_read(_full) and
  svn_io_file_write(_full) functions.
]]]

Index: subversion/include/svn_io.h
===================================================================
--- subversion/include/svn_io.h	(revision 7872)
+++ subversion/include/svn_io.h	(working copy)
@@ -644,6 +644,32 @@
 svn_io_file_close (apr_file_t *file, apr_pool_t *pool);
 
 
+/** Wrapper for @c apr_file_read(), which see. */
+svn_error_t *
+svn_io_file_read (apr_file_t *file, void *buf, 
+                  apr_size_t *nbytes, apr_pool_t *pool);
+
+
+/** Wrapper for @c apr_file_write_full(), which see. */
+svn_error_t *
+svn_io_file_read_full (apr_file_t *file, void *buf, 
+                       apr_size_t nbytes, apr_size_t *bytes_read,
+                       apr_pool_t *pool);
+
+
+/** Wrapper for @c apr_file_write(), which see. */
+svn_error_t *
+svn_io_file_write (apr_file_t *file, void *buf, 
+                   apr_size_t *nbytes, apr_pool_t *pool);
+
+
+/** Wrapper for @c apr_file_write_full(), which see. */
+svn_error_t *
+svn_io_file_write_full (apr_file_t *file, void *buf, 
+                        apr_size_t nbytes, apr_size_t *bytes_written,
+                        apr_pool_t *pool);
+
+
 /** Wrapper for @c apr_stat(), which see.  @a fname is utf8-encoded. */
 svn_error_t *
 svn_io_stat (apr_finfo_t *finfo, const char *fname,
Index: subversion/libsvn_subr/io.c
===================================================================
--- subversion/libsvn_subr/io.c	(revision 7872)
+++ subversion/libsvn_subr/io.c	(working copy)
@@ -973,13 +973,17 @@
 }
 
 
-/* Get the name of FILE, or NULL if FILE is an unnamed stream. */
+/* Get the name of FILE, or NULL if FILE is an unnamed stream. 
+   On error *fname_utf8 is guaranteed to contain NULL.
+ */
 static svn_error_t *
 file_name_get (const char **fname_utf8, apr_file_t *file, apr_pool_t *pool)
 {
   apr_status_t apr_err;
   const char *fname;
 
+  *fname_utf8 = NULL;
+
   apr_err = apr_file_name_get (&fname, file);
   if (apr_err)
     return svn_error_create
@@ -987,10 +991,15 @@
        "failed to get file name from APR");
 
   if (fname)
-    SVN_ERR (svn_path_cstring_to_utf8 (fname_utf8, fname, pool));
-  else
-    *fname_utf8 = NULL;
+    {
+      svn_error_t *err;
 
+      err = svn_path_cstring_to_utf8 (&fname, fname, pool);
+      if (err)
+        return err;
+    }
+
+  *fname_utf8 = fname;
   return SVN_NO_ERROR;
 }
 
@@ -1685,44 +1694,100 @@
                   apr_int32_t flag, apr_fileperms_t perm,
                   apr_pool_t *pool)
 {
+  const char *err_str;
   const char *fname_apr;
   apr_status_t status;
+  char errbuf[255];
 
   SVN_ERR (svn_path_cstring_from_utf8 (&fname_apr, fname, pool));
   status = apr_file_open (new_file, fname_apr, flag, perm, pool);
 
   if (status)
-    return svn_error_createf (status, NULL,
-                              "svn_io_file_open: can't open '%s'", fname);
+    {
+      apr_strerror (status, errbuf, sizeof(errbuf));
+      SVN_ERR (svn_utf_cstring_to_utf8 (&err_str, errbuf, pool));
+      return svn_error_createf 
+        (status, NULL, "Can't open file '%s': %s", fname, err_str);
+    }
   else
     return SVN_NO_ERROR;  
 }
 
 
+static svn_error_t *
+do_io_file_wrapper_cleanup (apr_file_t *file, apr_status_t status, 
+                            const char *op, apr_pool_t *pool)
+{
+  const char *name;
+  const char *errstr;
+  char errbuf[255];
+  svn_error_t *err;
+
+  if (! status)
+    return SVN_NO_ERROR;
+
+  svn_error_clear (file_name_get (&name, file, pool));
+  name = (name) ? apr_psprintf (pool, "file '%s'", name) : "stream";
+  apr_strerror (status, errbuf, sizeof(errbuf));
+  err = svn_utf_cstring_to_utf8 (&errstr, errbuf, pool);
+  errstr = (err) ? "" : apr_psprintf (pool, ": %s", errstr);
+  svn_error_clear (err);
+
+  return svn_error_createf (status, NULL, "Can't %s %s%s", op, name,
errstr);
+}
+
 svn_error_t *
 svn_io_file_close (apr_file_t *file, apr_pool_t *pool)
 {
-  apr_status_t status;
+  return do_io_file_wrapper_cleanup
+    ( file, apr_file_close (file),
+      "close", pool);
+}
 
-  status = apr_file_close (file);
 
-  if (status)
-    {
-      const char *fname_utf8;
-      SVN_ERR (file_name_get (&fname_utf8, file, pool));
-      
-      if (NULL == fname_utf8)
-        fname_utf8 = "(stdin/out/err?)";
+svn_error_t *
+svn_io_file_read (apr_file_t *file, void *buf, 
+                  apr_size_t *nbytes, apr_pool_t *pool)
+{
+  return do_io_file_wrapper_cleanup
+    ( file, apr_file_read (file, buf, nbytes),
+      "read", pool);
+}
 
-      return svn_error_createf (status, NULL,
-                                "svn_io_file_close: can't close '%s'",
-                                fname_utf8);
-    }
-  return SVN_NO_ERROR;  
+
+svn_error_t *
+svn_io_file_read_full (apr_file_t *file, void *buf, 
+                        apr_size_t nbytes, apr_size_t *bytes_read,
+                        apr_pool_t *pool)
+{
+  return do_io_file_wrapper_cleanup
+    ( file, apr_file_read_full (file, buf, nbytes, bytes_read),
+      "read", pool);
 }
 
 
 svn_error_t *
+svn_io_file_write (apr_file_t *file, void *buf, 
+                   apr_size_t *nbytes, apr_pool_t *pool)
+{
+  return do_io_file_wrapper_cleanup
+    ( file, apr_file_write (file, buf, nbytes),
+      "read", pool);
+}
+
+
+svn_error_t *
+svn_io_file_write_full (apr_file_t *file, void *buf, 
+                        apr_size_t nbytes, apr_size_t *bytes_written,
+                        apr_pool_t *pool)
+{
+  return do_io_file_wrapper_cleanup
+    ( file, apr_file_write_full (file, buf, nbytes, bytes_written),
+      "read", pool);
+}
+
+
+svn_error_t *
 svn_io_stat (apr_finfo_t *finfo, const char *fname,
              apr_int32_t wanted, apr_pool_t *pool)
 {

-- 
HoHoHo! Seid Ihr auch alle sch�n brav gewesen?

GMX Weihnachts-Special: Die 1. Adresse f�r Weihnachts-
m�nner und -frauen! http://www.gmx.net/de/cgi/specialmail

+++ GMX - die erste Adresse f�r Mail, Message, More! +++


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by Erik Huelsmann <e....@gmx.net>.
[ snip: yes I meant that ]

> Three comments:

> > +/** Wrapper for @c apr_file_read(), which see. */
> 
> I guess by "which see," you mean, "see the documentation for that
> function," but I don't think that's very clear.

Ok; it's the way some other functions in that header are documented too, but
I can fix all of them.

[snip]
> > +      svn_error_t *err;
> >  
> > +      err = svn_path_cstring_to_utf8 (&fname, fname, pool);
> > +      if (err)
> > +        return err;
> > +    }
> > +  *fname_utf8 = fname;
> >    return SVN_NO_ERROR;
> 
> I guess the point here is to guarantee that *fname_utf8 doesn't get
> garbage of UTF-8 conversion fails.  But you also wrote out an SVN_ERR()
> the long way; SVN_ERR(svn_path_cstring_to_utf8 (&fname, fname, pool))
> should do fine.

That is indeed the point, but the if(err) return err is an ooops. Good I
submitted for review...

> >    if (status)
> > -    return svn_error_createf (status, NULL,
> > -                              "svn_io_file_open: can't open '%s'",
> fname);
> > +    {
> > +      apr_strerror (status, errbuf, sizeof(errbuf));
> > +      SVN_ERR (svn_utf_cstring_to_utf8 (&err_str, errbuf, pool));
> > +      return svn_error_createf 
> > +        (status, NULL, "Can't open file '%s': %s", fname, err_str);
> > +    }
> 
> The behavior on error is different from that of the IO wrapper.  But
> then, UTF-8 conversion of an APR error string is never going to fail, so
> perhaps this isn't worth worrying about.

I'll have a look at it and see if it needs lots of code to fix this. 

Fixed commit comming up. Thanks for the review.


bye,


Erik.

-- 
Neu bei GMX: Preissenkung f�r MMS-Versand und FreeMMS!

Ideal f�r alle, die gerne MMS verschicken:
25 FreeMMS/Monat mit GMX TopMail.
http://www.gmx.net/de/cgi/produktemail

+++ GMX - die erste Adresse f�r Mail, Message, More! +++


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by Julian Foad <ju...@btopenworld.com>.
Greg Hudson wrote:
> On Sun, 2003-11-30 at 04:08, Erik Huelsmann wrote:
> 
>>I was about to commit this change when Greg Hudson said he would not have
>>coded it this way, so I'm submitting for review.
> 
> Er, I think what Erik meant is: he wrote something, I said it didn't
> look right to me, we talked about it and came up with something new, but
> based on that he wants review on the finished product.
> 
> Three comments:
> 
>>+/** Wrapper for @c apr_file_read(), which see. */
> 
> I guess by "which see," you mean, "see the documentation for that
> function," but I don't think that's very clear.

I think Eric was just copying this from all the existing functions which somebody else wrote.  I do vaguely recall someone on the mailing list a few months ago having trouble understanding this construct, so maybe we should change the wording in every case, but not just within Erik's new functions.

- Julian


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by Branko Čibej <br...@xbc.nu>.
mark benedetto king wrote:

>On Sun, Nov 30, 2003 at 11:42:03AM -0500, Greg Hudson wrote:
>  
>
>>On Sun, 2003-11-30 at 04:08, Erik Huelsmann wrote:
>>    
>>
>>>I was about to commit this change when Greg Hudson said he would not have
>>>coded it this way, so I'm submitting for review.
>>>      
>>>
>>Er, I think what Erik meant is: he wrote something, I said it didn't
>>look right to me, we talked about it and came up with something new, but
>>based on that he wants review on the finished product.
>>
>>Three comments:
>>
>>    
>>
>>>+/** Wrapper for @c apr_file_read(), which see. */
>>>      
>>>
>>I guess by "which see," you mean, "see the documentation for that
>>function," but I don't think that's very clear.
>>
>>    
>>
>
>There are a bunch of comments like this running around.  I agree, though,
>that this construction seems a bit awkward, especially since Doxygen will
>make the reference into a link.
>  
>

Huh, that's perfectly clear and gramatically correct English.

(Whilst it may not be American, we /are/ an international community... :-)

-- 
Brane Čibej   <br...@xbc.nu>   http://www.xbc.nu/brane/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by mark benedetto king <mb...@lowlatency.com>.
On Sun, Nov 30, 2003 at 11:42:03AM -0500, Greg Hudson wrote:
> On Sun, 2003-11-30 at 04:08, Erik Huelsmann wrote:
> > I was about to commit this change when Greg Hudson said he would not have
> > coded it this way, so I'm submitting for review.
> 
> Er, I think what Erik meant is: he wrote something, I said it didn't
> look right to me, we talked about it and came up with something new, but
> based on that he wants review on the finished product.
> 
> Three comments:
> 
> > +/** Wrapper for @c apr_file_read(), which see. */
> 
> I guess by "which see," you mean, "see the documentation for that
> function," but I don't think that's very clear.
> 

There are a bunch of comments like this running around.  I agree, though,
that this construction seems a bit awkward, especially since Doxygen will
make the reference into a link.

--ben


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by Greg Hudson <gh...@MIT.EDU>.
On Sun, 2003-11-30 at 04:08, Erik Huelsmann wrote:
> I was about to commit this change when Greg Hudson said he would not have
> coded it this way, so I'm submitting for review.

Er, I think what Erik meant is: he wrote something, I said it didn't
look right to me, we talked about it and came up with something new, but
based on that he wants review on the finished product.

Three comments:

> +/** Wrapper for @c apr_file_read(), which see. */

I guess by "which see," you mean, "see the documentation for that
function," but I don't think that's very clear.

>    if (fname)
> -    SVN_ERR (svn_path_cstring_to_utf8 (fname_utf8, fname, pool));
> -  else
> -    *fname_utf8 = NULL;
> +    {
> +      svn_error_t *err;
>  
> +      err = svn_path_cstring_to_utf8 (&fname, fname, pool);
> +      if (err)
> +        return err;
> +    }
> +  *fname_utf8 = fname;
>    return SVN_NO_ERROR;

I guess the point here is to guarantee that *fname_utf8 doesn't get
garbage of UTF-8 conversion fails.  But you also wrote out an SVN_ERR()
the long way; SVN_ERR(svn_path_cstring_to_utf8 (&fname, fname, pool))
should do fine.
 
>    if (status)
> -    return svn_error_createf (status, NULL,
> -                              "svn_io_file_open: can't open '%s'", fname);
> +    {
> +      apr_strerror (status, errbuf, sizeof(errbuf));
> +      SVN_ERR (svn_utf_cstring_to_utf8 (&err_str, errbuf, pool));
> +      return svn_error_createf 
> +        (status, NULL, "Can't open file '%s': %s", fname, err_str);
> +    }

The behavior on error is different from that of the IO wrapper.  But
then, UTF-8 conversion of an APR error string is never going to fail, so
perhaps this isn't worth worrying about.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: [PATCH] extend svn_io_file_* with calls for read(_full) and write(_full)

Posted by Julian Foad <ju...@btopenworld.com>.
Erik Huelsmann wrote:
> 
> * subversion/libsvn_subr/io.c:
>   (svn_io_file_read,svn_io_file_read_full,
>    svn_io_file_write,svn_io_file_write_full): 
>    New. Wrappers around apr_file_ calls returning svn_error_t's.
>   (do_io_file_wrapper_cleanup): 
>    New. Implements cleanup procedure for the file wrappers above.
>   (file_name_get): Ensure that NULL is set on error 
>    in *fname_utf8: a requirement for do_io_file_wrapper_cleanup.
>   (svn_io_file_open): Return an error string formatted like the ones
>    from do_io_file_wrapper_cleanup.
>   (svn_io_file_close): Use do_io_file_wrapper_cleanup for 
>    error handling.
> 
[...]
> Index: subversion/libsvn_subr/io.c
> ===================================================================
> --- subversion/libsvn_subr/io.c	(revision 7872)
> +++ subversion/libsvn_subr/io.c	(working copy)
> @@ -973,13 +973,17 @@
>  }
>  
>  
> -/* Get the name of FILE, or NULL if FILE is an unnamed stream. */
> +/* Get the name of FILE, or NULL if FILE is an unnamed stream. 
> +   On error *fname_utf8 is guaranteed to contain NULL.
> + */
>  static svn_error_t *
>  file_name_get (const char **fname_utf8, apr_file_t *file, apr_pool_t *pool)
>  {
>    apr_status_t apr_err;
>    const char *fname;
>  
> +  *fname_utf8 = NULL;
> +
>    apr_err = apr_file_name_get (&fname, file);
>    if (apr_err)
>      return svn_error_create
> @@ -987,10 +991,15 @@
>         "failed to get file name from APR");
>  
>    if (fname)
> -    SVN_ERR (svn_path_cstring_to_utf8 (fname_utf8, fname, pool));
> -  else
> -    *fname_utf8 = NULL;
> +    {
> +      svn_error_t *err;
>  
> +      err = svn_path_cstring_to_utf8 (&fname, fname, pool);
> +      if (err)
> +        return err;
> +    }
> +
> +  *fname_utf8 = fname;
>    return SVN_NO_ERROR;
>  }

It would be better to leave this function as it was, but in your new function "do_io_file_wrapper_cleanup", if file_name_get returns an error, don't try to use the resulting file name.

[...]
> +static svn_error_t *
> +do_io_file_wrapper_cleanup (apr_file_t *file, apr_status_t status, 
> +                            const char *op, apr_pool_t *pool)
> +{
> +  const char *name;
> +  const char *errstr;
> +  char errbuf[255];
> +  svn_error_t *err;
> +
> +  if (! status)
> +    return SVN_NO_ERROR;
> +
> +  svn_error_clear (file_name_get (&name, file, pool));
> +  name = (name) ? apr_psprintf (pool, "file '%s'", name) : "stream";

Following on from what I said above: If file_name_get returns an error, don't try to use the resulting file name.  Like when you call svn_utf_cstring_to_utf8 just below here, except that in this case there will be three possible results: valid file name, or NULL which you convert to "stream", or error which probably means an unconvertible file name (the code above would say "stream", but it is probably not a stream because in practice I don't think apr_file_name_get ever fails so file_name_get can only fail on the string conversion).

> +  apr_strerror (status, errbuf, sizeof(errbuf));
> +  err = svn_utf_cstring_to_utf8 (&errstr, errbuf, pool);
> +  errstr = (err) ? "" : apr_psprintf (pool, ": %s", errstr);
> +  svn_error_clear (err);
> +
> +  return svn_error_createf (status, NULL, "Can't %s %s%s", op, name,
> errstr);
> +}
> +
>  svn_error_t *
>  svn_io_file_close (apr_file_t *file, apr_pool_t *pool)
>  {
> -  apr_status_t status;
> +  return do_io_file_wrapper_cleanup
> +    ( file, apr_file_close (file),
> +      "close", pool);

Inconsistent spacing of parentheses (also repeated below).

> +}
[...]

Otherwise good.  I'm glad you are doing this.

- Julian


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org