88import org .slf4j .LoggerFactory ;
99
1010import java .io .IOException ;
11+ import java .nio .file .DirectoryStream ;
1112import java .nio .file .FileSystemException ;
1213import java .nio .file .Files ;
1314import java .nio .file .Path ;
@@ -17,6 +18,12 @@ public class FileSystemCapabilityChecker {
1718 private static final Logger LOG = LoggerFactory .getLogger (FileSystemCapabilityChecker .class );
1819
1920 public enum Capability {
21+ /**
22+ * File system allows read access
23+ * @since 1.9.3
24+ */
25+ READ_ACCESS ,
26+
2027 /**
2128 * File system allows write access
2229 * @since 1.9.3
@@ -37,67 +44,85 @@ public enum Capability {
3744 }
3845
3946 /**
40- * Checks whether the underlying filesystem has all required capabilities for readonly access.
47+ * Checks whether the underlying filesystem has all required capabilities.
48+ *
4149 * @param pathToVault Path to a vault's storage location
4250 * @throws MissingCapabilityException if any check fails
4351 * @implNote Only short-running tests with constant time are performed
44- * @since 1.9.3
52+ * @since 1.9.2
4553 */
46- public void assertReadOnlyCapabilities (Path pathToVault ) throws MissingCapabilityException {
47- // no-op
54+ public void assertAllCapabilities (Path pathToVault ) throws MissingCapabilityException {
55+ assertReadAccess (pathToVault );
56+ assertWriteAccess (pathToVault );
57+ assertLongFilenameSupport (pathToVault );
58+ assertLongFilePathSupport (pathToVault );
4859 }
4960
5061 /**
51- * Checks whether the underlying filesystem has all required capabilities.
52- *
62+ * Checks whether the underlying filesystem allows reading the given dir.
5363 * @param pathToVault Path to a vault's storage location
54- * @throws MissingCapabilityException if any check fails
55- * @implNote Only short-running tests with constant time are performed
56- * @since 1.9.2
64+ * @throws MissingCapabilityException if the check fails
65+ * @since 1.9.3
5766 */
58- public void assertReadWriteCapabilities (Path pathToVault ) throws MissingCapabilityException {
59- Path checkDir = pathToVault .resolve ("c" );
60- try {
61- checkWriteAccess (checkDir );
62- checkLongFilenames (checkDir );
63- checkLongFilePaths (checkDir );
64- } finally {
65- try {
66- if (Files .exists (checkDir )) {
67- MoreFiles .deleteRecursively (checkDir , RecursiveDeleteOption .ALLOW_INSECURE );
68- }
69- } catch (IOException e ) {
70- LOG .warn ("Failed to clean up " + checkDir , e );
71- }
67+ public void assertReadAccess (Path pathToVault ) throws MissingCapabilityException {
68+ try (DirectoryStream ds = Files .newDirectoryStream (pathToVault )) {
69+ assert ds != null ;
70+ } catch (IOException e ) {
71+ throw new MissingCapabilityException (pathToVault , Capability .READ_ACCESS );
7272 }
7373 }
74-
75- private void checkWriteAccess (Path checkDir ) throws MissingCapabilityException {
74+
75+ /**
76+ * Checks whether the underlying filesystem allows writing to the given dir.
77+ * @param pathToVault Path to a vault's storage location
78+ * @throws MissingCapabilityException if the check fails
79+ * @since 1.9.3
80+ */
81+ public void assertWriteAccess (Path pathToVault ) throws MissingCapabilityException {
82+ Path checkDir = pathToVault .resolve ("c" );
7683 try {
77- Files .createDirectories (checkDir );
84+ Files .createDirectory (checkDir );
7885 } catch (IOException e ) {
7986 throw new MissingCapabilityException (checkDir , Capability .WRITE_ACCESS );
87+ } finally {
88+ deleteSilently (checkDir );
8089 }
8190 }
8291
83- private void checkLongFilenames (Path checkDir ) throws MissingCapabilityException {
92+ public void assertLongFilenameSupport (Path pathToVault ) throws MissingCapabilityException {
8493 String longFileName = Strings .repeat ("a" , 226 ) + ".c9r" ;
94+ Path checkDir = pathToVault .resolve ("c" );
8595 Path p = checkDir .resolve (longFileName );
8696 try {
8797 Files .createDirectories (p );
8898 } catch (IOException e ) {
8999 throw new MissingCapabilityException (p , Capability .LONG_FILENAMES );
100+ } finally {
101+ deleteSilently (checkDir );
90102 }
91103 }
92104
93- private void checkLongFilePaths (Path checkDir ) throws MissingCapabilityException {
105+ public void assertLongFilePathSupport (Path pathToVault ) throws MissingCapabilityException {
94106 String longFileName = Strings .repeat ("a" , 96 ) + ".c9r" ;
95107 String longPath = Joiner .on ('/' ).join (longFileName , longFileName , longFileName , longFileName );
108+ Path checkDir = pathToVault .resolve ("c" );
96109 Path p = checkDir .resolve (longPath );
97110 try {
98111 Files .createDirectories (p );
99112 } catch (IOException e ) {
100113 throw new MissingCapabilityException (p , Capability .LONG_PATHS );
114+ } finally {
115+ deleteSilently (checkDir );
116+ }
117+ }
118+
119+ private void deleteSilently (Path dir ) {
120+ try {
121+ if (Files .exists (dir )) {
122+ MoreFiles .deleteRecursively (dir , RecursiveDeleteOption .ALLOW_INSECURE );
123+ }
124+ } catch (IOException e ) {
125+ LOG .warn ("Failed to clean up " + dir , e );
101126 }
102127 }
103128
0 commit comments