Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File canWrite() returns false for owner even when permissions provided are 750  RSS feed

 
ruth abraham
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing an issue wherein certainPath.canWrite() returns false even when the directory has full permissions for the owner drwxr-x---.
Any ideas about what could be causing this?

Thanks!
Ruth
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think canWrite() will ever return true for a directory, it is only for files.
 
ruth abraham
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,
It is a File object. But in the file system it is a directory, meaning isDirectory() will result in true.
For e.g.
File newFileObj = new File("/basepath/myPath/); as opposed to File newFileObj = new File("/basepath/myPath/myFile.ext);
newFileObj.canWrite(); //gives false even though the permissions for path are 777 for the user that executes this.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
canWrite() is only supposed to be used when the path the File object refers to is a file and not a directory. So:
Should return true if you have write permissions. Whereas if you are referring to a directory ie:
There is no specified return value. The API docs say "Tests whether the application can modify the file denoted by this abstract pathname.". Note the use of 'file' and not 'directory' or 'file or directory'.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The API docs say "Tests whether the application can modify the file denoted by this abstract pathname.". Note the use of 'file' and not 'directory' or 'file or directory'.

But compare that to the phrasing of the JavaDoc for isFile():
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

Thus, in the API, "file" can refer to a directory as well, when not qualified by "normal".

Unfortunately, the fact that they elsewhere often say "file or directory" makes it less than clear what they mean by "file" in those cases where they say neither "normal file" nor "file or directory".

Running a quick test on my system, I see that canWrite() does return true for a directory, if I have write access to it. Likewise canRead() and other methods like isHidden(). Perhaps this behavior varies on other systems. I'm on 64-bit Ubuntu right now, using JDK 1.7.0_06-b24.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruth - are you certain the path is correct? Do newFileObject.exists() and newFileObject.isDirectory() return the expected values? What OS and JDK version are you using?
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:But compare that to the phrasing of the JavaDoc for isFile():
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

Thus, in the API, "file" can refer to a directory as well, when not qualified by "normal".

I'd never noticed that inconsistency in terminology before, well spotted.

Running a quick test on my system, I see that canWrite() does return true for a directory ... I'm on 64-bit Ubuntu right now, using JDK 1.7.0_06-b24

Just ran a test on Win 7 using Java 7 and Java 6 and they both return true as well.
Looks like my earlier comments were wrong, sorry Ruth for any confusion caused.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!