From 7f0cd0f801d3c670fcc96350f2910757261d79be Mon Sep 17 00:00:00 2001 From: Miduo666 Date: Sun, 1 Mar 2026 01:18:50 +1100 Subject: [PATCH 1/2] resolve initialization failure when encryption directory exists --- lib/src/solid/utils/init_helper.dart | 51 ++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/src/solid/utils/init_helper.dart b/lib/src/solid/utils/init_helper.dart index 4e03b605..68f4aefa 100644 --- a/lib/src/solid/utils/init_helper.dart +++ b/lib/src/solid/utils/init_helper.dart @@ -189,12 +189,37 @@ Future initPod( dirUrls = [for (final d in defaultDirs) await getDirUrl(d)]; } - // Require the creation of the encryption directory and - // the encKeyFile and indKeyFile in it. + // Determine whether the encryption directory needs to be created. + // + // The encryption directory is essential for key management. It may or may + // not already be in [dirUrls] depending on whether [initialStructureTest] + // detected it as missing. We check the server to decide: + // - If it already exists on the server → skip creation, just set the key. + // - If it does NOT exist → ensure it is in the creation list + // and perform full key initialisation. final encDirUrl = await getDirUrl(await getEncDirPath()); - if (!dirUrls.contains(encDirUrl)) { - throw Exception('Can not initialise POD without creating $encDirUrl'); + var needsEncSetup = dirUrls.contains(encDirUrl); + + if (!needsEncSetup) { + // Encryption directory is not in the creation list. + // Check whether it actually exists on the server. + + final status = await checkResourceStatus(encDirUrl, isFile: false); + if (status == ResourceStatus.notExist) { + // The directory is missing on the server AND was not in the creation + // list (e.g. because initialStructureTest returned forbidden/unknown, + // or because the caller only passed partial dirUrls). Add it so that + // it gets created below together with the other directories. + + dirUrls.add(encDirUrl); + needsEncSetup = true; + debugPrint( + 'initPod: encryption directory missing on server, ' + 'added to creation list', + ); + } + // else: directory exists on the server → needsEncSetup stays false. } // Create the required directories. @@ -220,10 +245,22 @@ Future initPod( } } - // Create the encKeyFile, indKeyFile and pubKeyFile - // and remove them from the fileUrls list. + // Handle encryption key setup. + + if (needsEncSetup) { + // First-time setup: create encryption key files from scratch. + + await KeyManager.initPodKeys(securityKey); + } else { + // Encryption already initialised: just verify and set the security key + // so that encrypted operations work in this session without overwriting + // existing keys. + + await KeyManager.setSecurityKey(securityKey); + } + + // Remove encryption key file URLs from the list (already handled above). - await KeyManager.initPodKeys(securityKey); fileUrls.remove(await getFileUrl(await getEncKeyPath())); fileUrls.remove(await getFileUrl(await getIndKeyPath())); fileUrls.remove(await getFileUrl(await getPubKeyPath())); From a030c6dc35c9f060f0565bb6b12eb3b7f67bd4a6 Mon Sep 17 00:00:00 2001 From: Miduo666 Date: Sun, 1 Mar 2026 02:03:11 +1100 Subject: [PATCH 2/2] clarify empty list vs Null --- lib/src/solid/utils/init_helper.dart | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/src/solid/utils/init_helper.dart b/lib/src/solid/utils/init_helper.dart index 68f4aefa..a34a1f0e 100644 --- a/lib/src/solid/utils/init_helper.dart +++ b/lib/src/solid/utils/init_helper.dart @@ -183,8 +183,10 @@ Future initPod( } // Check (and generate) the directory URLs. + // Only regenerate when the caller did not provide any list (null). + // An empty list means "no directories to create" and should be respected. - if (dirUrls == null || dirUrls.isEmpty) { + if (dirUrls == null) { final defaultDirs = await generateDefaultFolders(); dirUrls = [for (final d in defaultDirs) await getDirUrl(d)]; } @@ -233,14 +235,16 @@ Future initPod( } // Check (and generate) the file URLs. + // Only regenerate when the caller did not provide any list (null). + // An empty list means "no files to create" and should be respected. - if (fileUrls == null || fileUrls.isEmpty) { + if (fileUrls == null) { final defaultFiles = await generateDefaultFiles(); fileUrls = []; for (final entry in defaultFiles.entries) { final d = entry.key; for (final f in entry.value as List) { - fileUrls.add([d, f].join('/')); + fileUrls.add(await getFileUrl([d, f].join('/'))); } } }