9

I have some code that is designed to open a local master file, make additions, and save the file both by overwriting the master file and overwriting a write protected copy on an accessible network location. This is done by saving the modified file to a temp file and then copying over the other two files.

String tempFileName= "File.tmp";
String fileName= "File.xlsm";
String serverPath="\\\\network path\\";
File serverFile = new File(serverPath+fileName);

Files.copy(Paths.get(tempFileName),Paths.get(fileName),
   StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING);

if(serverFile.exists()){serverFile.setWritable(true, false);}

Files.copy(Paths.get(tempFileName),Paths.get(serverPath+fileName),
   StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING);

serverFile.setWritable(false,false);

Files.delete(Paths.get(tempFileName));

This code works well most of the time however, some of the time the code completes successfully without exception but with the network location file deleted. The local master file is saved and updated correctly but the file that should exist on the network is simply gone.

What makes this more difficult is that i have been unable to reproduce this problem under any controlled circumstances. So i ask you for any guidance on how this could occur from a file copy/overwrite operation.

Thank you

UPDATE:

I had a hunch and checked network access logs to the server file path. The deletion of the file occurs if and only if the file is being accessed by a user other than the creator but not all of the time. Again though, this is accessed as read only so a user having the file open should not affect overwriting a new version and most of the time does not. Digging deeper it seems that occasionally if and only if the file is opened by another user and java is trying to overwrite the file an AccessDenied Exception is thrown and the file is deleted.

I believe this must be a bug in setWritable() or Files.copy (or a combination) as the file should not be deleted in any case and isWritable() returns true every time. I have tried other methods for setting/UN-setting read only permissions and have come up empty. The current work around that I have in place simply catches the exception and loops until the file is deleted and a fresh copy is in place. This works but is really a hack so if anyone has any better solutions/suggestions I welcome them.

David
  • 133
  • 1
  • 2
  • 5
  • How big is your file? Could it perhaps be caused by a network disconnect while you are copying the file? – Byron Mar 08 '13 at 20:09
  • @Byron That was my only working theory but I have run network connectivity tests over long periods of time with no drop outs – David Mar 11 '13 at 20:35
  • @Byron Also the file will be large eventually but currently is only ~500k – David Mar 14 '13 at 23:55
  • Excuse my ignorance, but what is that Files class you're using? – stmoebius Mar 24 '13 at 20:53
  • @stmoebius The files class is just the standard java 7 nio.file.Files class – David Mar 27 '13 at 23:16
  • `if the file is opened by another user and java is trying to overwrite the file an AccessDenied Exception is thrown`. Do you have the details of such an exception? – geert3 Dec 30 '14 at 20:32

1 Answers1

-2

See How does FileLock work?, you could do something like:

  1. Wait for file to become available
  2. Lock file
  3. Overwrite/delete/other
  4. Unlock (if applicable)

This should prevent access by other users during the process of modifying the file.

Community
  • 1
  • 1
Fallso
  • 1,311
  • 1
  • 9
  • 18
  • 1
    If and only if the other users also use file locking. – user207421 Dec 01 '14 at 09:58
  • I suppose it depends upon the filesystem implementation but in my local tests, if I lock a file, short of manually removing the lock programatically I cannot delete, write or rename that file whilst the lock is open. So not sure why I've been downvoted here! – Fallso Jan 08 '15 at 15:24