Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Delete Problem

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project where my application monitors files/folders. The user can add/delete files/folders. Such files/folder can be remote shares too (NOT mapped drives)
Whenever a new file/folder is added my application also computes and maintains some data regarding these files.
Whenever files are deleted, my application needs to flush out the corresponding data. Unfortunately (for me) the flushing out is a non reversible operation.

Since I wanted the FS and my data to be in synch, I tried the following approach.
1) Check if my application has sufficient access permissions to delete the file/folder
2) If yes, flush out the data/delete file.

Since there is no File#canDelete() I used File#canWrite() (imagining both to effectively mean the same permissions).
Surprisingly, file.canWrite() returns true but file.delete() returns false!
Thus my data and the FS is out of synch.

Is there any way I can figure out if a particular file/folder can be deleted by my application?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does canWrite return for the parent folder? Usually, deleting a file is partly changing the parent folder.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns true for the parent as well !
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you delete the file from Windows explorer manually?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

Now I have set the permissions giving everyone full access. (initially there were no permissions and canWrite() was supposed to return false.)
Now the files are getting deleted. But that was never the problem.

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I tried this:
and it throws a SecurityException (inspite of the permissions)
Could this be because the delete code is invoked from a servlet?

If I try System.getSecurityManager it returns null!

Only if the file.canWrite() was working properly !
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
I got this working by calling the file.setLastModified(System.currentTimeMillis());
As expected this does NOT generate the exception so I am checking the return boolean and proceeding accordingly.

But I still cant figure out how canWrite() returns true !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic