octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Behavior of 'mkdir'


From: John W. Eaton
Subject: Re: Behavior of 'mkdir'
Date: Thu, 24 Jul 2008 13:08:38 -0400

On 24-Jul-2008, WMennerich wrote:

| But another thing which could increase the Octave/Matlab compatiblility:
| 
| The message string of the mkdir-function differs in the discussed case:
| 
| Matlab wrotes: 'Directory already exists.' (also with the point at the end)
| 
| Octave wrotes: 'File exists'
| 
| I would suggest to fit the message string given back by the octave mkdir to
| the output which comes from matlab to make strcmp-constructs more compatible
| between Matlab and Octave. Also because 'File exists' may be
| misleading: Sure, in UnixU/Linux everything is a 'file', also directories.
| But since
| the name of this function is 'mkdir' and not 'mkfile' or something else, the
| message should have a relation to 'directory'.

I have no desire to change error messages so that they match exactly
the Matlab error messages (you would do that, but not change the
behavior of the mkdir function?).

In Octave, this message comes from the C-level strerror function.
The text is system dependent.  If Octave is modified to support
different locale settings, then the message will change if you change
your locale setting to another language.  So I think checking the
text to decide what to do is probably not a good programming strategy.

I think it might be best to simply change the behavior to match
Matlab's in this case.  However, I don't think that we need to
duplicate the behavior exactly.  I see that Matlab also returns
status=1 and msg="Directory already exists." when the "directory" is
just a regular file.  That is probably not a good thing to do.  How
about the attached patch?

jwe

# HG changeset patch
# User John W. Eaton <address@hidden>
# Date 1216919016 14400
# Node ID b6d4c644b4b61ada8f5f7ede36072a9b64005389
# Parent  4f9e8eeb2059cf6d20e821958233d4e773ef4cff
Fmkdir: improve compatibility

diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2008-07-24  John W. Eaton  <address@hidden>
+
+       * dirfns.cc (Fmkdir): If directory already exists, return status =
+       true, but also set error message.
+
 2008-07-23  John W. Eaton  <address@hidden>
 
        * ov-usr_fcn.cc (octave_user_function::do_multi_index_op):
diff --git a/src/dirfns.cc b/src/dirfns.cc
--- a/src/dirfns.cc
+++ b/src/dirfns.cc
@@ -257,16 +257,31 @@
     {
       std::string msg;
 
-      int status = file_ops::mkdir (file_ops::tilde_expand (dirname),
-                                   0777, msg);
+      dirname = file_ops::tilde_expand (dirname);
 
-      if (status < 0)
+      file_stat fs (dirname);
+
+      if (fs && fs.is_dir ())
        {
+         // For compatibility with Matlab, we return true when the
+         // directory already exists.
+
          retval(2) = "mkdir";
-         retval(1) = msg;
+         retval(1) = "directory exists";
+         retval(0) = true;
        }
       else
-       retval(0) = true;
+       {
+         int status = file_ops::mkdir (dirname, 0777, msg);
+
+         if (status < 0)
+           {
+             retval(2) = "mkdir";
+             retval(1) = msg;
+           }
+         else
+           retval(0) = true;
+       }
     }
   else
     print_usage ();

reply via email to

[Prev in Thread] Current Thread [Next in Thread]