99package org .cryptomator .cryptofs ;
1010
1111import com .google .common .base .Strings ;
12- import org .cryptomator .cryptofs .common .Constants ;
1312import org .cryptomator .cryptolib .api .MasterkeyLoader ;
14- import org .cryptomator .cryptolib .api .MasterkeyLoadingFailedException ;
1513
1614import java .net .URI ;
1715import java .nio .file .FileSystems ;
1816import java .nio .file .Path ;
1917import java .util .AbstractMap ;
2018import java .util .Collection ;
2119import java .util .EnumSet ;
22- import java .util .HashSet ;
2320import java .util .Map ;
2421import java .util .Set ;
2522import java .util .function .Consumer ;
3835public class CryptoFileSystemProperties extends AbstractMap <String , Object > {
3936
4037 /**
41- * Maximum ciphertext path length.
38+ * Maximum cleartext filename length.
4239 *
43- * @since 1.9.8
44- */
45- public static final String PROPERTY_MAX_PATH_LENGTH = "maxPathLength" ;
46-
47- static final int DEFAULT_MAX_PATH_LENGTH = Constants .MAX_CIPHERTEXT_PATH_LENGTH ;
48-
49- /**
50- * Maximum filename length of .c9r files.
51- *
52- * @since 1.9.9
40+ * @since 2.0.0
5341 */
54- public static final String PROPERTY_MAX_NAME_LENGTH = "maxNameLength " ;
42+ public static final String PROPERTY_MAX_CLEARTEXT_NAME_LENGTH = "maxCleartextNameLength " ;
5543
56- static final int DEFAULT_MAX_NAME_LENGTH = Constants . MAX_CIPHERTEXT_NAME_LENGTH ;
44+ static final int DEFAULT_MAX_CLEARTEXT_NAME_LENGTH = LongFileNameProvider . MAX_FILENAME_BUFFER_SIZE ;
5745
5846 /**
5947 * Key identifying the key loader used during initialization.
6048 *
6149 * @since 2.0.0
6250 */
63- public static final String PROPERTY_KEYLOADERS = "keyLoaders" ;
64-
65- static final Collection <MasterkeyLoader > DEFAULT_KEYLOADERS = Set .of ();
51+ public static final String PROPERTY_KEYLOADER = "keyLoader" ;
6652
6753 /**
6854 * Key identifying the name of the vault config file located inside the vault directory.
@@ -111,34 +97,17 @@ public enum FileSystemFlags {
11197
11298 private CryptoFileSystemProperties (Builder builder ) {
11399 this .entries = Set .of ( //
114- Map .entry (PROPERTY_KEYLOADERS , builder .keyLoaders ), //
100+ Map .entry (PROPERTY_KEYLOADER , builder .keyLoader ), //
115101 Map .entry (PROPERTY_FILESYSTEM_FLAGS , builder .flags ), //
116102 Map .entry (PROPERTY_VAULTCONFIG_FILENAME , builder .vaultConfigFilename ), //
117103 Map .entry (PROPERTY_MASTERKEY_FILENAME , builder .masterkeyFilename ), //
118- Map .entry (PROPERTY_MAX_PATH_LENGTH , builder .maxPathLength ), //
119- Map .entry (PROPERTY_MAX_NAME_LENGTH , builder .maxNameLength ), //
104+ Map .entry (PROPERTY_MAX_CLEARTEXT_NAME_LENGTH , builder .maxCleartextNameLength ), //
120105 Map .entry (PROPERTY_CIPHER_COMBO , builder .cipherCombo ) //
121106 );
122107 }
123108
124- Collection <MasterkeyLoader > keyLoaders () {
125- return (Collection <MasterkeyLoader >) get (PROPERTY_KEYLOADERS );
126- }
127-
128- /**
129- * Selects the first applicable MasterkeyLoader that supports the given scheme.
130- *
131- * @param scheme An URI scheme used in key IDs
132- * @return A key loader
133- * @throws MasterkeyLoadingFailedException If the scheme is not supported by any key loader
134- */
135- MasterkeyLoader keyLoader (String scheme ) throws MasterkeyLoadingFailedException {
136- for (MasterkeyLoader loader : keyLoaders ()) {
137- if (loader .supportsScheme (scheme )) {
138- return loader ;
139- }
140- }
141- throw new MasterkeyLoadingFailedException ("No key loader for key type: " + scheme );
109+ MasterkeyLoader keyLoader () {
110+ return (MasterkeyLoader ) get (PROPERTY_KEYLOADER );
142111 }
143112
144113 public VaultCipherCombo cipherCombo () {
@@ -162,12 +131,8 @@ String masterkeyFilename() {
162131 return (String ) get (PROPERTY_MASTERKEY_FILENAME );
163132 }
164133
165- int maxPathLength () {
166- return (int ) get (PROPERTY_MAX_PATH_LENGTH );
167- }
168-
169- int maxNameLength () {
170- return (int ) get (PROPERTY_MAX_NAME_LENGTH );
134+ int maxCleartextNameLength () {
135+ return (int ) get (PROPERTY_MAX_CLEARTEXT_NAME_LENGTH );
171136 }
172137
173138 @ Override
@@ -219,23 +184,21 @@ public static CryptoFileSystemProperties wrap(Map<String, ?> properties) {
219184 public static class Builder {
220185
221186 public VaultCipherCombo cipherCombo = DEFAULT_CIPHER_COMBO ;
222- private Collection < MasterkeyLoader > keyLoaders = new HashSet <>( DEFAULT_KEYLOADERS ) ;
187+ private MasterkeyLoader keyLoader = null ;
223188 private final Set <FileSystemFlags > flags = EnumSet .copyOf (DEFAULT_FILESYSTEM_FLAGS );
224189 private String vaultConfigFilename = DEFAULT_VAULTCONFIG_FILENAME ;
225190 private String masterkeyFilename = DEFAULT_MASTERKEY_FILENAME ;
226- private int maxPathLength = DEFAULT_MAX_PATH_LENGTH ;
227- private int maxNameLength = DEFAULT_MAX_NAME_LENGTH ;
191+ private int maxCleartextNameLength = DEFAULT_MAX_CLEARTEXT_NAME_LENGTH ;
228192
229193 private Builder () {
230194 }
231195
232196 private Builder (Map <String , ?> properties ) {
233- checkedSet (Collection .class , PROPERTY_KEYLOADERS , properties , this ::withKeyLoaders );
197+ checkedSet (MasterkeyLoader .class , PROPERTY_KEYLOADER , properties , this ::withKeyLoader );
234198 checkedSet (String .class , PROPERTY_VAULTCONFIG_FILENAME , properties , this ::withVaultConfigFilename );
235199 checkedSet (String .class , PROPERTY_MASTERKEY_FILENAME , properties , this ::withMasterkeyFilename );
236200 checkedSet (Set .class , PROPERTY_FILESYSTEM_FLAGS , properties , this ::withFlags );
237- checkedSet (Integer .class , PROPERTY_MAX_PATH_LENGTH , properties , this ::withMaxPathLength );
238- checkedSet (Integer .class , PROPERTY_MAX_NAME_LENGTH , properties , this ::withMaxNameLength );
201+ checkedSet (Integer .class , PROPERTY_MAX_CLEARTEXT_NAME_LENGTH , properties , this ::withMaxCleartextNameLength );
239202 checkedSet (VaultCipherCombo .class , PROPERTY_CIPHER_COMBO , properties , this ::withCipherCombo );
240203 }
241204
@@ -250,28 +213,17 @@ private <T> void checkedSet(Class<T> type, String key, Map<String, ?> properties
250213 }
251214 }
252215
253-
254- /**
255- * Sets the maximum ciphertext path length for a CryptoFileSystem.
256- *
257- * @param maxPathLength The maximum ciphertext path length allowed
258- * @return this
259- * @since 1.9.8
260- */
261- public Builder withMaxPathLength (int maxPathLength ) {
262- this .maxPathLength = maxPathLength ;
263- return this ;
264- }
265-
266216 /**
267- * Sets the maximum ciphertext filename length for a CryptoFileSystem.
217+ * Sets the maximum cleartext filename length for a CryptoFileSystem. This value is checked during write
218+ * operations. Read access to nodes with longer names should be unaffected. Setting this value to {@code 0} or
219+ * a negative value effectively disables write access.
268220 *
269- * @param maxNameLength The maximum ciphertext filename length allowed
221+ * @param maxCleartextNameLength The maximum cleartext filename length allowed
270222 * @return this
271- * @since 1.9.9
223+ * @since 2.0.0
272224 */
273- public Builder withMaxNameLength (int maxNameLength ) {
274- this .maxNameLength = maxNameLength ;
225+ public Builder withMaxCleartextNameLength (int maxCleartextNameLength ) {
226+ this .maxCleartextNameLength = maxCleartextNameLength ;
275227 return this ;
276228 }
277229
@@ -289,26 +241,14 @@ public Builder withCipherCombo(VaultCipherCombo cipherCombo) {
289241 }
290242
291243 /**
292- * Sets the keyLoaders for a CryptoFileSystem.
293- *
294- * @param keyLoaders A set of keyLoaders to load the key configured in the vault configuration
295- * @return this
296- * @since 2.0.0
297- */
298- public Builder withKeyLoaders (MasterkeyLoader ... keyLoaders ) {
299- return withKeyLoaders (asList (keyLoaders ));
300- }
301-
302- /**
303- * Sets the keyLoaders for a CryptoFileSystem.
244+ * Sets the keyloader for a CryptoFileSystem.
304245 *
305- * @param keyLoaders A set of keyLoaders to load the key configured in the vault configuration
246+ * @param keyLoader A factory creating a {@link MasterkeyLoader} capable of handling the given {@code scheme}.
306247 * @return this
307248 * @since 2.0.0
308249 */
309- public Builder withKeyLoaders (Collection <MasterkeyLoader > keyLoaders ) {
310- this .keyLoaders .clear ();
311- this .keyLoaders .addAll (keyLoaders );
250+ public Builder withKeyLoader (MasterkeyLoader keyLoader ) {
251+ this .keyLoader = keyLoader ;
312252 return this ;
313253 }
314254
@@ -372,8 +312,8 @@ public CryptoFileSystemProperties build() {
372312 }
373313
374314 private void validate () {
375- if (keyLoaders . isEmpty () ) {
376- throw new IllegalStateException ("at least one keyloader is required" );
315+ if (keyLoader == null ) {
316+ throw new IllegalStateException ("keyLoader is required" );
377317 }
378318 if (Strings .nullToEmpty (masterkeyFilename ).trim ().isEmpty ()) {
379319 throw new IllegalStateException ("masterkeyFilename is required" );
0 commit comments