While copying files using rsync into directory, Java Watch service gets tempary file notification from OS instead of actual files

StackOverflow https://stackoverflow.com/questions/22626754

  •  20-06-2023
  •  | 
  •  

سؤال

When copying files using rsync, OS is giving notification for temp file instead of actual files to watch service.

For example below are few file notifications which are appended with some character after extension ".ser" and with prefix with ".":

.file1.ser.4QNNyx
.file2.ser.f08GVA
.file3.ser.UFBEi                           

Expected notification for file with below name:

 file1.ser
 file2.ser
 file3.ser  

Code snapshot as below,

  public void registerServiceWatch() {
    final ResourceWatchService watchService = new ResourceWatchService(directoryPath);
    IResourceObserver observer = new IResourceObserver() {
        @Override
        public void resourceModified(final String eventName, final Path filePath) {
            updateCache(eventName, filePath);
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            watchService.processEvents();
        }
    };
    watchService.addResourceObserver(observer);
    new Thread(runnable).start();
}              

If we copy files without rsync like cp, scp then we getting proper notification, but in case of rsync we not getting proper file modification, So what the issue with rsync,

Please feel fre to share thoughts on it.

rsync command:

rsync -rtvz <source> <destination>

هل كانت مفيدة؟

المحلول

--inplace option in rsync will write directly to target, avoiding creation of intermediate ..hash files

--temp-dir=DIR option will create temporary objects in a different directory

You can use any of them to prevent your watch from being notificated on temporary events

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top