ensureEncryptedPlacesDir function

Future<(bool, bool)> ensureEncryptedPlacesDir(
  1. bool directoryVerified
)

Ensure the encrypted places directory exists. Uses persistent caching flag to avoid repeated server checks. Returns (success, dirCreated) tuple.

  • success: true if directory exists or was created successfully
  • dirCreated: true if directory was newly created (false if already existed)

Optimization: If directoryVerified is true (from persistent storage), skips all network checks - the directory is assumed to exist.

IMPORTANT: API parameter types in solidpod:

  • checkResourceStatus() expects a full Pod URL
  • setInheritKeyDir() expects a full Pod URL
  • readPod/writePod expect relative paths (e.g., "encrypted_data/places")

Network calls breakdown (only when directoryVerified=false):

  1. checkResourceStatus(dirUrl) - check if directory exists URL-based
  2. If not exist, setInheritKeyDir(dirUrl) creates it URL-based, which internally:
    • Calls checkResourceStatus(dirUrl) again (solidpod internal, redundant)
    • Checks ACL file status via checkResourceStatus(aclUrl)
    • Creates directory and ACL resources if needed This is why persistent caching is critical - it saves 3+ URL-based network calls!

Reliability note: If the directory is deleted on the server side (by manual cleanup or another client), the persistent flag won't detect it until a write operation fails. The app handles this by clearing the flag on write failures, forcing re-verification on the next attempt (see encrypted_places_service.dart).

Implementation

Future<(bool success, bool dirCreated)> ensureEncryptedPlacesDir(
  bool directoryVerified,
) async {
  // Skip all checks if already verified (from persistent storage or session)
  // This optimization eliminates 3+ network calls per operation!
  if (directoryVerified) {
    return (true, false);
  }

  try {
    // Only check directory if not yet verified
    // This network call happens only once per installation.
    final fullDirPath = await getFullEncryptedPlacesDirPath();
    final dirUrl = await getDirUrl(fullDirPath);

    final status = await checkResourceStatus(dirUrl, isFile: false);
    if (status == ResourceStatus.notExist) {
      // Create the directory with encryption key inheritance
      // WARNING: setInheritKeyDir internally calls checkResourceStatus again
      // (solidpod limitation - can't skip this redundant check)
      await setInheritKeyDir(dirUrl, createAcl: true);
      return (true, true); // Directory was created
    }

    // Directory exists, return success without creation
    return (true, false);
  } catch (e) {
    debugPrint('Failed to ensure encrypted places directory: $e');
    return (false, false);
  }
}