77import org .slf4j .Logger ;
88import org .slf4j .LoggerFactory ;
99
10- import javax .inject .Inject ;
11- import javax .inject .Singleton ;
1210import java .io .IOException ;
1311import java .nio .file .DirectoryIteratorException ;
1412import java .nio .file .DirectoryStream ;
1715import java .nio .file .Files ;
1816import java .nio .file .Path ;
1917
20- @ Singleton
21- public class FileSystemCapabilityChecker {
18+ public final class FileSystemCapabilityChecker {
2219
2320 private static final Logger LOG = LoggerFactory .getLogger (FileSystemCapabilityChecker .class );
24- private static final int MAX_CIPHERTEXT_NAME_LENGTH = 220 ; // inclusive. calculations done in https://github.com/cryptomator/cryptofs/issues/60#issuecomment-523238303
25- private static final int MIN_CIPHERTEXT_NAME_LENGTH = 28 ; // base64(iv).c9r
26- private static final int MAX_ADDITIONAL_PATH_LENGTH = 48 ; // beginning at d/... see https://github.com/cryptomator/cryptofs/issues/77
2721
2822 public enum Capability {
2923 /**
@@ -41,8 +35,8 @@ public enum Capability {
4135 WRITE_ACCESS ,
4236 }
4337
44- @ Inject
45- public FileSystemCapabilityChecker () {
38+ private FileSystemCapabilityChecker () {
39+
4640 }
4741
4842 /**
@@ -53,7 +47,7 @@ public FileSystemCapabilityChecker() {
5347 * @implNote Only short-running tests with constant time are performed
5448 * @since 1.9.2
5549 */
56- public void assertAllCapabilities (Path pathToVault ) throws MissingCapabilityException {
50+ public static void assertAllCapabilities (Path pathToVault ) throws MissingCapabilityException {
5751 assertReadAccess (pathToVault );
5852 assertWriteAccess (pathToVault );
5953 }
@@ -65,7 +59,7 @@ public void assertAllCapabilities(Path pathToVault) throws MissingCapabilityExce
6559 * @throws MissingCapabilityException if the check fails
6660 * @since 1.9.3
6761 */
68- public void assertReadAccess (Path pathToVault ) throws MissingCapabilityException {
62+ public static void assertReadAccess (Path pathToVault ) throws MissingCapabilityException {
6963 try (DirectoryStream <Path > ds = Files .newDirectoryStream (pathToVault )) {
7064 assert ds != null ;
7165 } catch (IOException e ) {
@@ -80,7 +74,7 @@ public void assertReadAccess(Path pathToVault) throws MissingCapabilityException
8074 * @throws MissingCapabilityException if the check fails
8175 * @since 1.9.3
8276 */
83- public void assertWriteAccess (Path pathToVault ) throws MissingCapabilityException {
77+ public static void assertWriteAccess (Path pathToVault ) throws MissingCapabilityException {
8478 Path checkDir = pathToVault .resolve ("c" );
8579 try {
8680 Files .createDirectories (checkDir );
@@ -93,9 +87,9 @@ public void assertWriteAccess(Path pathToVault) throws MissingCapabilityExceptio
9387 }
9488 }
9589
96- public int determineSupportedCleartextFileNameLength (Path pathToVault ) throws IOException {
90+ public static int determineSupportedCleartextFileNameLength (Path pathToVault ) throws IOException {
9791 int maxCiphertextLen = determineSupportedCiphertextFileNameLength (pathToVault );
98- assert maxCiphertextLen >= MIN_CIPHERTEXT_NAME_LENGTH ;
92+ assert maxCiphertextLen >= Constants . MIN_CIPHER_NAME_LENGTH ;
9993 // math explained in https://github.com/cryptomator/cryptofs/issues/60#issuecomment-523238303;
10094 // subtract 4 for file extension, base64-decode, subtract 16 for IV
10195 return (maxCiphertextLen - 4 ) / 4 * 3 - 16 ;
@@ -108,22 +102,22 @@ public int determineSupportedCleartextFileNameLength(Path pathToVault) throws IO
108102 * @return Number of chars a .c9r file is allowed to have
109103 * @throws IOException If unable to perform this check
110104 */
111- public int determineSupportedCiphertextFileNameLength (Path pathToVault ) throws IOException {
112- int subPathLength = MAX_ADDITIONAL_PATH_LENGTH - 2 ; // subtract "c/"
113- return determineSupportedCiphertextFileNameLength (pathToVault .resolve ("c" ), subPathLength , MIN_CIPHERTEXT_NAME_LENGTH , MAX_CIPHERTEXT_NAME_LENGTH );
105+ public static int determineSupportedCiphertextFileNameLength (Path pathToVault ) throws IOException {
106+ int subPathLength = Constants . MAX_ADDITIONAL_PATH_LENGTH - 2 ; // subtract "c/"
107+ return determineSupportedCiphertextFileNameLength (pathToVault .resolve ("c" ), subPathLength , Constants . MIN_CIPHER_NAME_LENGTH , Constants . MAX_CIPHER_NAME_LENGTH );
114108 }
115109
116110 /**
117111 * Determines the number of chars a filename is allowed to have inside of subdirectories of <code>dir</code> by running an experiment.
118112 *
119- * @param dir Path to a directory where to conduct the experiment (e.g. <code>/path/to/vault/c</code>)
120- * @param subPathLength Defines the combined number of chars of the subdirectories inside <code>dir</code>, including slashes but excluding the leading slash. Must be a minimum of 6
113+ * @param dir Path to a directory where to conduct the experiment (e.g. <code>/path/to/vault/c</code>)
114+ * @param subPathLength Defines the combined number of chars of the subdirectories inside <code>dir</code>, including slashes but excluding the leading slash. Must be a minimum of 6
121115 * @param minFileNameLength The minimum filename length to check
122116 * @param maxFileNameLength The maximum filename length to check
123117 * @return The supported filename length inside a subdirectory of <code>dir</code> with <code>subPathLength</code> chars
124118 * @throws IOException If unable to perform this check
125119 */
126- public int determineSupportedCiphertextFileNameLength (Path dir , int subPathLength , int minFileNameLength , int maxFileNameLength ) throws IOException {
120+ public static int determineSupportedCiphertextFileNameLength (Path dir , int subPathLength , int minFileNameLength , int maxFileNameLength ) throws IOException {
127121 Preconditions .checkArgument (subPathLength >= 6 , "subPathLength must be larger than charcount(a/nnn/)" );
128122 Preconditions .checkArgument (minFileNameLength > 0 );
129123 Preconditions .checkArgument (maxFileNameLength <= 999 );
@@ -144,7 +138,7 @@ public int determineSupportedCiphertextFileNameLength(Path dir, int subPathLengt
144138 }
145139 }
146140
147- private int determineSupportedCiphertextFileNameLength (Path p , int lowerBoundIncl , int upperBoundExcl ) {
141+ private static int determineSupportedCiphertextFileNameLength (Path p , int lowerBoundIncl , int upperBoundExcl ) {
148142 assert lowerBoundIncl < upperBoundExcl ;
149143 int mid = (lowerBoundIncl + upperBoundExcl ) / 2 ;
150144 assert mid < upperBoundExcl ;
@@ -159,7 +153,7 @@ private int determineSupportedCiphertextFileNameLength(Path p, int lowerBoundInc
159153 }
160154 }
161155
162- private boolean canHandleFileNameLength (Path parent , int nameLength ) {
156+ private static boolean canHandleFileNameLength (Path parent , int nameLength ) {
163157 Path checkDir = parent .resolve (String .format ("%03d" , nameLength ));
164158 Path checkFile = checkDir .resolve (Strings .repeat ("a" , nameLength ));
165159 try {
@@ -178,7 +172,7 @@ private boolean canHandleFileNameLength(Path parent, int nameLength) {
178172 }
179173 }
180174
181- private boolean canListDir (Path dir ) {
175+ private static boolean canListDir (Path dir ) {
182176 try (DirectoryStream <Path > ds = Files .newDirectoryStream (dir )) {
183177 ds .iterator ().hasNext (); // throws DirectoryIteratorException on Windows if child path too long
184178 return true ;
@@ -187,15 +181,15 @@ private boolean canListDir(Path dir) {
187181 }
188182 }
189183
190- private void deleteSilently (Path path ) {
184+ private static void deleteSilently (Path path ) {
191185 try {
192186 Files .delete (path );
193187 } catch (IOException e ) {
194188 LOG .trace ("Failed to delete " + path , e );
195189 }
196190 }
197191
198- private void deleteRecursivelySilently (Path dir ) {
192+ private static void deleteRecursivelySilently (Path dir ) {
199193 try {
200194 if (Files .exists (dir )) {
201195 MoreFiles .deleteRecursively (dir , RecursiveDeleteOption .ALLOW_INSECURE );
0 commit comments