Multiples problems with File#mkdirs()
Hi, In multiples places in nuiton-utils, there is following code: boolean b = dir.mkdirs(); if (!b) { throw new IOException("Could not create directory " + dir); } This code is not correct, because mkdirs() can return false, without any errors, as the docs says: "return true if and only if the directory was created", so this can return false if directory already exists. Can we remove this practice ? This may also be the cause of this bug : http://forge.nuiton.org/issues/3195
On Wed, 9 Dec 2015 10:21:16 +0100 Eric Chatellier <chatellier@codelutin.com> wrote:
Le 05/12/2015 12:19, Eric Chatellier a écrit :
Can we remove this practice ?
Done. I am not quite confortable with your commit.
The solution is not ok. If it can be modiy by another thread, then we need to use a synchronization system. -- Tony Chemit -------------------- tél: +33 (0) 2 40 50 29 28 http://www.codelutin.com email: chemit@codelutin.com twitter: https://twitter.com/tchemit
On Sat, 5 Dec 2015 12:19:53 +0100 "Eric Chatellier" <chatellier@codelutin.com> wrote:
Hi,
In multiples places in nuiton-utils, there is following code:
boolean b = dir.mkdirs(); if (!b) { throw new IOException("Could not create directory " + dir); }
This code is not correct, because mkdirs() can return false, without any errors, as the docs says: "return true if and only if the directory was created", so this can return false if directory already exists.
Can we remove this practice ?
Short answer no, since you don't give the code just above this one which is if (!dir.exist()) ...
This may also be the cause of this bug : http://forge.nuiton.org/issues/3195
_______________________________________________ Nuiton-utils-devel mailing list Nuiton-utils-devel@list.nuiton.org http://list.nuiton.org/cgi-bin/mailman/listinfo/nuiton-utils-devel
-- Tony Chemit -------------------- tél: +33 (0) 2 40 50 29 28 http://www.codelutin.com email: chemit@codelutin.com twitter: https://twitter.com/tchemit
Le 09/12/2015 10:36, Tony Chemit a écrit :
Short answer no, since you don't give the code just above this one which is
if (!dir.exist()) ...
Or we could reverse condition: if (!dir.mkdirs()) { if (!dir.exist()) { throw new IOException("Could not create directory " + dir); } } But, i think the second if() condition could never trigger because the first dir.mkdirs() will throw a root IOException. -- Éric Chatellier - www.codelutin.com - 02.40.50.29.28
On Wed, 9 Dec 2015 10:52:37 +0100 Eric Chatellier <chatellier@codelutin.com> wrote:
Le 09/12/2015 10:36, Tony Chemit a écrit :
Short answer no, since you don't give the code just above this one which is
if (!dir.exist()) ...
Or we could reverse condition:
if (!dir.mkdirs()) { if (!dir.exist()) { throw new IOException("Could not create directory " + dir); } }
+1 this sound better.
But, i think the second if() condition could never trigger because the first dir.mkdirs() will throw a root IOException.
What is important to me, is to be sure when leaving the method, that the directory exists. -- Tony Chemit -------------------- tél: +33 (0) 2 40 50 29 28 http://www.codelutin.com email: chemit@codelutin.com twitter: https://twitter.com/tchemit
participants (2)
-
Eric Chatellier -
Tony Chemit