1

There are some workarounds, but I wanted to raise an issue with the real thing.

I've an external server uploading files to /upload folder and I'm using java.nio.file.Files atomic move to copy them into /dest folder and then processing the moved files.

The issue is that it evident that after the call to nio move the source file is not yet flushed in the source but still move executed atomically and once it flushed it is flushed to the destination moved file.

OS is Ubuntu. Pseudo is:

Path origFilePath = resolveOrigFilePath();
Path targetFilePath = resolveOrigFilePath();

//logging shows empty file in source, length = 0

   logger.warn("Atomic move file [{}], can read mode: [{}],can write mode: [{}] source size: [{}]",
            origFilePath, origFilePath.toFile().canRead(), origFilePath.toFile().canWrite(), origFilePath
                    .toFile().length());

//execute atomic move of file

    Path target = Files.move(origFilePath, targetFilePath, StandardCopyOption.ATOMIC_MOVE);

//logging shows empty file moved and original file is not exists anymore

    logger.warn(
            "Atomic moved file [{}] to [{}], target read mode: [{}], target write mode: [{}], target size: [{}], orig target size: [{}], is original exists [{}]",
            origFilePath, target, target.toFile().canWrite(), target.toFile().canRead(),
            target.toFile().length(), targetPath.toFile().length(), sensorFilePath.toFile().exists());

. . //processing the destination file which is still empty . . //after a short duration the destination file has content, while no other programatic access to the file is done

. .

Any idea what can be the reason in the underlying implementation?

  • Solid-state or 'regular' harddrive? :) – Shark Jun 24 '13 at 09:45
  • currently I'm looking on the *source directory* permissions as I'm getting printout that the source file is not writable. For removing a file you need a write permission on the containing folder. – Sharon Kohen Jun 28 '13 at 16:01
  • From your description there's no problem with the move, it's with caching of whoever reads and writes your file. You need to make sure it's flushed before you want to process it. – Thorsten Schöning Aug 11 '14 at 08:56

1 Answers1

0

Before you start moving files make sure you do OutputStream.flush() and more importantly FileDescriptor.sync():

        FileOutputStream out = new FileOutputStream(file, false);

        // Write your data here.

        out.flush();

        FileDescriptor fd = out.getFD();
        fd.sync();

        out.close();
ATrubka
  • 3,982
  • 5
  • 33
  • 52