Behavior of 'mkdir'

View: New views
13 Messages — Rating Filter:   Alert me  

Behavior of 'mkdir'

by WMennerich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
the behavior of octaves mkdir is not equal to Matlabs mkdir:

Octave:

mkdir(test)

ans=1

mkdir(test) %second call

ans=0


************

Matlab:
mkdir(test)

ans=1

mkdir(test) %second call

ans=1
Warning: Directory already exists.

**************************
As I understand the difference is that octave tells you whether the directory was created or not:
The second call does not create the directory because it is already existing.
Hence, the ack is 'false'.
Matlab tells you whether the directory exists  or not, so also the second result is 'true'.

Would it be better to implement the mkdir function like the Matlab one?


Best, Wolfgang


Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 22-Jul-2008, WMennerich wrote:

|
| Hi,
| the behavior of octaves mkdir is not equal to Matlabs mkdir:
|
| Octave:
|
| mkdir(test)
|
| ans=1
|
| mkdir(test) %second call
|
| ans=0
|
|
| ************
|
| Matlab:
| mkdir(test)
|
| ans=1
|
| mkdir(test) %second call
|
| ans=1
| Warning: Directory already exists.
|
| **************************
| As I understand the difference is that octave tells you whether the
| directory was created or not:
| The second call does not create the directory because it is already
| existing.
| Hence, the ack is 'false'.
| Matlab tells you whether the directory exists  or not, so also the second
| result is 'true'.
|
| Would it be better to implement the mkdir function like the Matlab one?

Sure, I'd consider a patch that improves compatibility in this case.

jwe

Re: Behavior of 'mkdir'

by WMennerich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It depends on what we want to have:
The octave 'mkdir' is more accurate: If a directory already exists,
it can not be created. Hence, the behavior of octave seems to me more
correct.
If you get a 'true' from 'mkdir' with octave, you know that the directory
exists and it is empy.
If you get a 'true' from 'mkdir' with Matlab, you only know, that the directory
exists and you must do an extra check whether it is empty or not.

best, Wolfgang





Re: Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 23-Jul-2008, WMennerich wrote:

|
| It depends on what we want to have:
| The octave 'mkdir' is more accurate: If a directory already exists,
| it can not be created. Hence, the behavior of octave seems to me more
| correct.

Then maybe you should submit a bug report to the MathWorks?

jwe

Re: Behavior of 'mkdir'

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 23, 2008 at 11:23 AM, WMennerich <W.Mennerich@...> wrote:

>
> It depends on what we want to have:
> The octave 'mkdir' is more accurate: If a directory already exists,
> it can not be created. Hence, the behavior of octave seems to me more
> correct.
> If you get a 'true' from 'mkdir' with octave, you know that the directory
> exists and it is empy.
> If you get a 'true' from 'mkdir' with Matlab, you only know, that the
> directory
> exists and you must do an extra check whether it is empty or not.
>
> best, Wolfgang
>
>

I'd like to also notice that Matlab manual states that mkdir returns 1
on success and 0 on failure, so apparently they consider a mkdir of
existing directory as success (but they explain more in the next
paragraph).
In the same situation, GNU's mkdir reports a failure, so I assume
that's what POSIX wants. Can anyone check the Windows MKDIR? I'd
expect the same behaviour. Given that, I think that Octave's way makes
more sense and I'd vote for leaving it as it is.

--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

Re: Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 23-Jul-2008, Jaroslav Hajek wrote:

| I'd like to also notice that Matlab manual states that mkdir returns 1
| on success and 0 on failure, so apparently they consider a mkdir of
| existing directory as success (but they explain more in the next
| paragraph).
| In the same situation, GNU's mkdir reports a failure, so I assume
| that's what POSIX wants. Can anyone check the Windows MKDIR? I'd
| expect the same behaviour. Given that, I think that Octave's way makes
| more sense and I'd vote for leaving it as it is.

Making more sense and providing something that is useful to Octave
users who want compatibility appear to conflict here.  So it may be
better to provide compatible behavior.  I know that is what I would
prefer to have if I were porting some Matlab code to Octave that
relied on this feature, because then the code would just work and I
wouldn't have to pause and think about what to do and meanwhile curse
the idiot Octave developers who tried to do the "right thing" (more
likely, I wouldn't know what the "right thing" was and would just
think the developers were idiots).

jwe

Re: Behavior of 'mkdir'

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 23, 2008 at 3:25 PM, John W. Eaton <jwe@...> wrote:

> On 23-Jul-2008, Jaroslav Hajek wrote:
>
> | I'd like to also notice that Matlab manual states that mkdir returns 1
> | on success and 0 on failure, so apparently they consider a mkdir of
> | existing directory as success (but they explain more in the next
> | paragraph).
> | In the same situation, GNU's mkdir reports a failure, so I assume
> | that's what POSIX wants. Can anyone check the Windows MKDIR? I'd
> | expect the same behaviour. Given that, I think that Octave's way makes
> | more sense and I'd vote for leaving it as it is.
>
> Making more sense and providing something that is useful to Octave
> users who want compatibility appear to conflict here.  So it may be
> better to provide compatible behavior.  I know that is what I would
> prefer to have if I were porting some Matlab code to Octave that
> relied on this feature, because then the code would just work and I
> wouldn't have to pause and think about what to do and meanwhile curse
> the idiot Octave developers who tried to do the "right thing" (more
> likely, I wouldn't know what the "right thing" was and would just
> think the developers were idiots).
>

OK, I take the point. In that case, I suppose the issue should be
mentioned explicitly in the docs, because there are plenty of Octave
users without much prior exposition to Matlab, and if I were to guess
the behaviour, I would guess the way it works now.

Wolfgang, do you want to fix this yourself?

> jwe
>



--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

Re: Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 23-Jul-2008, Jaroslav Hajek wrote:

| OK, I take the point.

Just to be clear, I'm certainly not saying it is idiotic to want to do
the right thing, just that we seem to have a lot of users who, when
they are in a situation like I described, appear to conclude that
those of us working on Octave must be idiots for not understanding
clearly how Matlab works.  Or at least that's what I get from reading
some of the messages on the lists.

| In that case, I suppose the issue should be
| mentioned explicitly in the docs, because there are plenty of Octave
| users without much prior exposition to Matlab, and if I were to guess
| the behaviour, I would guess the way it works now.

Docs?  Well, at least we could point to it when there are complaints.
It's funny, people complain about insufficient documentation, but does
anyone actually read the manual?

jwe

Re: Behavior of 'mkdir'

by WMennerich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Jaroslav Hajek-2 wrote:
Wolfgang, do you want to fix this yourself?
I already found a way to implement my code for working fine with both,
Matlab and Octave. So, there is no need for me to change this.
The more I thing about it, the more I find that octave does
it in the best way now.
Even if it makes trouble to run matlab written code with octave,
the people which use the 'mkdir' command normally are not new
to Matlab/Ocatave and may have enough background to come clear with
these differences.

Best, Wolfgang

Re: Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 24-Jul-2008, WMennerich wrote:

| I already found a way to implement my code for working fine with both,
| Matlab and Octave.

How?  What is your solution?

| The more I thing about it, the more I find that octave does
| it in the best way now.

Not if what you want is for Matlab code to work without change in
Octave.

| Even if it makes trouble to run matlab written code with octave,
| the people which use the 'mkdir' command normally are not new
| to Matlab/Ocatave and may have enough background to come clear with
| these differences.

I don't think this conclusion is justified.  How do you know who is
using the mkdir function or what their level of expertise is?

jwe

Re: Behavior of 'mkdir'

by WMennerich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

First:

This (simple) solution should work in both environments:

>
>[ack msg]=mkdir('testDirectory')
>
>if ~ack
>    if strcmp(msg,'File exists')
>        % 'Wrong alert'  by octave, -->do nothing
>    else
>        % some other thing is wrong, e.g. throw an errror here.
>    end
>end

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'.


Second:

Yes, you may be right, one can not conclude that programmers using 'mkdir' have
more background than others: This conclusion came from my own experience: With my first matlab programms, I was happy that it runs somehow, but I was far away from playing
with directories and stuff like that, e.g user inputs, loading and writing data, ....
;-)

Wolfgang

Re: Behavior of 'mkdir'

by shaiay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Thu, Jul 24, 2008 at 7:31 PM, WMennerich <W.Mennerich@...> wrote:

First:

This (simple) solution should work in both environments:

>
>[ack msg]=mkdir('testDirectory')
>
>if ~ack
>    if strcmp(msg,'File exists')
>        % 'Wrong alert'  by octave, -->do nothing
>    else
>        % some other thing is wrong, e.g. throw an errror here.
>    end
>end

Maybe you can submit a patch like this to the mkdir documentation?
 

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'

So can you submit a patch?

 Shai


Re: Behavior of 'mkdir'

by John W. Eaton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <jwe@...>
# 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  <jwe@...>
+
+ * dirfns.cc (Fmkdir): If directory already exists, return status =
+ true, but also set error message.
+
 2008-07-23  John W. Eaton  <jwe@...>
 
  * 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 ();