12

When I use FileUtils.copyDirectory(), the execute bits get turned off for executable files.
Do I have to turn them on manually?

FWIW, my umask is set to 0027 but it looks like FileUtils.copyDirectory() is not using that setting since the 'other' permissions, aside from the execute bit, are preserved.

Shahzeb
  • 4,745
  • 4
  • 27
  • 40
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • What operating system are you using? I wouldn't be surprised if behavior differs between Linux, FreeBSD and Mac OS X... – gutch Jul 27 '11 at 00:49
  • I'm on Linux. Since I have a workaround, I'll wait until the adoption of Java 7 in my company. – Noel Yap Jul 28 '11 at 18:57

2 Answers2

8

The upcoming Filesystem additions in Java 7 will help. Look at JSR-203. If you are using Linux, you can use the backport with Java 6.

The new API that you want is: Files.copy(Path, Path, CopyOptions). Note that CopyOptions has COPY_ATTRIBUTES, which will do what you want.

sebkur
  • 658
  • 2
  • 9
  • 18
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • Using this API, if you copy a non-empty directory, it creates just an empty directory in the target `path`. Any workaround? – Emadpres Apr 03 '20 at 01:00
  • `COPY_ATTRIBUTES` doesn't work on all file systems. On a macOS, it throws `UnsupportedOperationException`. – Abhijit Sarkar Jan 09 '21 at 23:34
3

I don't think its possible because of JVM limitations. The IO api and behavior is kinda shameful for the most popular language/platform in the world.

If you look at the FileUtils source code, during copy it creates the new file like this

File copiedFile = new File(destDir, srcFile.getName()); 

the file permissions are not preserved. And during actual copy the bytes are copied in batches ( buffered ) and written to the new file.

but, you could wait a couple of days or use the preview release of JDK7 which has apis to allow this to be possible.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
smartnut007
  • 6,324
  • 6
  • 45
  • 52