56
56
import java .util .Collections ;
57
57
import java .util .EnumSet ;
58
58
import java .util .Map ;
59
+ import java .util .Optional ;
59
60
import java .util .Set ;
60
61
import java .util .stream .Collectors ;
61
62
@@ -304,7 +305,7 @@ void createDirectory(CryptoPath cleartextDir, FileAttribute<?>... attrs) throws
304
305
// create dir if and only if the dirFile has been created right now (not if it has been created before):
305
306
try {
306
307
Files .createDirectories (ciphertextDir .path );
307
- longFileNameProvider .persistCachedIfDeflated (ciphertextDirFile );
308
+ longFileNameProvider .getCached (ciphertextDirFile ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
308
309
} catch (IOException e ) {
309
310
// make sure there is no orphan dir file:
310
311
Files .delete (ciphertextDirFile );
@@ -355,7 +356,7 @@ private FileChannel newFileChannel(CryptoPath cleartextFilePath, EffectiveOpenOp
355
356
} else {
356
357
// might also throw FileAlreadyExists:
357
358
FileChannel ch = openCryptoFiles .getOrCreate (ciphertextPath ).newFileChannel (options );
358
- longFileNameProvider .persistCachedIfDeflated (ciphertextPath );
359
+ longFileNameProvider .getCached (ciphertextPath ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
359
360
return ch ;
360
361
}
361
362
}
@@ -423,8 +424,10 @@ private void copySymlink(CryptoPath cleartextSource, CryptoPath cleartextTarget,
423
424
Path ciphertextSourceFile = cryptoPathMapper .getCiphertextFilePath (cleartextSource , CiphertextFileType .SYMLINK );
424
425
Path ciphertextTargetFile = cryptoPathMapper .getCiphertextFilePath (cleartextTarget , CiphertextFileType .SYMLINK );
425
426
CopyOption [] resolvedOptions = ArrayUtils .without (options , LinkOption .NOFOLLOW_LINKS ).toArray (CopyOption []::new );
427
+ Optional <LongFileNameProvider .DeflatedFileName > deflatedFileName = longFileNameProvider .getCached (ciphertextTargetFile );
426
428
Files .copy (ciphertextSourceFile , ciphertextTargetFile , resolvedOptions );
427
- longFileNameProvider .persistCachedIfDeflated (ciphertextTargetFile );
429
+ deflatedFileName .ifPresent (LongFileNameProvider .DeflatedFileName ::persist );
430
+
428
431
} else {
429
432
CryptoPath resolvedSource = symlinks .resolveRecursively (cleartextSource );
430
433
CryptoPath resolvedTarget = symlinks .resolveRecursively (cleartextTarget );
@@ -436,17 +439,19 @@ private void copySymlink(CryptoPath cleartextSource, CryptoPath cleartextTarget,
436
439
private void copyFile (CryptoPath cleartextSource , CryptoPath cleartextTarget , CopyOption [] options ) throws IOException {
437
440
Path ciphertextSourceFile = cryptoPathMapper .getCiphertextFilePath (cleartextSource , CiphertextFileType .FILE );
438
441
Path ciphertextTargetFile = cryptoPathMapper .getCiphertextFilePath (cleartextTarget , CiphertextFileType .FILE );
442
+ Optional <LongFileNameProvider .DeflatedFileName > deflatedFileName = longFileNameProvider .getCached (ciphertextTargetFile );
439
443
Files .copy (ciphertextSourceFile , ciphertextTargetFile , options );
440
- longFileNameProvider . persistCachedIfDeflated ( ciphertextTargetFile );
444
+ deflatedFileName . ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
441
445
}
442
446
443
447
private void copyDirectory (CryptoPath cleartextSource , CryptoPath cleartextTarget , CopyOption [] options ) throws IOException {
444
448
// DIRECTORY (non-recursive as per contract):
445
449
Path ciphertextTargetDirFile = cryptoPathMapper .getCiphertextFilePath (cleartextTarget , CiphertextFileType .DIRECTORY );
446
450
if (Files .notExists (ciphertextTargetDirFile )) {
447
451
// create new:
452
+ Optional <LongFileNameProvider .DeflatedFileName > deflatedFileName = longFileNameProvider .getCached (ciphertextTargetDirFile );
448
453
createDirectory (cleartextTarget );
449
- longFileNameProvider . persistCachedIfDeflated ( ciphertextTargetDirFile );
454
+ deflatedFileName . ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
450
455
} else if (ArrayUtils .contains (options , StandardCopyOption .REPLACE_EXISTING )) {
451
456
// keep existing (if empty):
452
457
Path ciphertextTargetDir = cryptoPathMapper .getCiphertextDir (cleartextTarget ).path ;
@@ -525,7 +530,7 @@ private void moveSymlink(CryptoPath cleartextSource, CryptoPath cleartextTarget,
525
530
Path ciphertextTargetFile = cryptoPathMapper .getCiphertextFilePath (cleartextTarget , CiphertextFileType .SYMLINK );
526
531
try (OpenCryptoFiles .TwoPhaseMove twoPhaseMove = openCryptoFiles .prepareMove (ciphertextSourceFile , ciphertextTargetFile )) {
527
532
Files .move (ciphertextSourceFile , ciphertextTargetFile , options );
528
- longFileNameProvider .persistCachedIfDeflated (ciphertextTargetFile );
533
+ longFileNameProvider .getCached (ciphertextTargetFile ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
529
534
twoPhaseMove .commit ();
530
535
}
531
536
}
@@ -537,7 +542,7 @@ private void moveFile(CryptoPath cleartextSource, CryptoPath cleartextTarget, Co
537
542
Path ciphertextTargetFile = cryptoPathMapper .getCiphertextFilePath (cleartextTarget , CiphertextFileType .FILE );
538
543
try (OpenCryptoFiles .TwoPhaseMove twoPhaseMove = openCryptoFiles .prepareMove (ciphertextSourceFile , ciphertextTargetFile )) {
539
544
Files .move (ciphertextSourceFile , ciphertextTargetFile , options );
540
- longFileNameProvider .persistCachedIfDeflated (ciphertextTargetFile );
545
+ longFileNameProvider .getCached (ciphertextTargetFile ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
541
546
twoPhaseMove .commit ();
542
547
}
543
548
}
@@ -550,7 +555,7 @@ private void moveDirectory(CryptoPath cleartextSource, CryptoPath cleartextTarge
550
555
if (!ArrayUtils .contains (options , StandardCopyOption .REPLACE_EXISTING )) {
551
556
// try to move, don't replace:
552
557
Files .move (ciphertextSourceDirFile , ciphertextTargetDirFile , options );
553
- longFileNameProvider .persistCachedIfDeflated (ciphertextTargetDirFile );
558
+ longFileNameProvider .getCached (ciphertextTargetDirFile ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
554
559
} else if (ArrayUtils .contains (options , StandardCopyOption .ATOMIC_MOVE )) {
555
560
// replace atomically (impossible):
556
561
assert ArrayUtils .contains (options , StandardCopyOption .REPLACE_EXISTING );
@@ -569,7 +574,7 @@ private void moveDirectory(CryptoPath cleartextSource, CryptoPath cleartextTarge
569
574
Files .delete (ciphertextTargetDir );
570
575
}
571
576
Files .move (ciphertextSourceDirFile , ciphertextTargetDirFile , options );
572
- longFileNameProvider .persistCachedIfDeflated (ciphertextTargetDirFile );
577
+ longFileNameProvider .getCached (ciphertextTargetDirFile ). ifPresent ( LongFileNameProvider . DeflatedFileName :: persist );
573
578
}
574
579
dirIdProvider .move (ciphertextSourceDirFile , ciphertextTargetDirFile );
575
580
cryptoPathMapper .invalidatePathMapping (cleartextSource );
0 commit comments