diff --git a/native/android/app/src/main/java/app/comm/android/fbjni/PlatformSpecificTools.java b/native/android/app/src/main/java/app/comm/android/fbjni/PlatformSpecificTools.java index f3badb639..729d88901 100644 --- a/native/android/app/src/main/java/app/comm/android/fbjni/PlatformSpecificTools.java +++ b/native/android/app/src/main/java/app/comm/android/fbjni/PlatformSpecificTools.java @@ -1,101 +1,101 @@ package app.comm.android.fbjni; import android.content.Context; import android.util.Log; import app.comm.android.MainApplication; import java.io.File; import java.lang.SecurityException; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class PlatformSpecificTools { static SecureRandom secureRandom = new SecureRandom(); public static byte[] generateSecureRandomBytes(int size) { byte[] buffer = new byte[size]; secureRandom.nextBytes(buffer); return buffer; } public static String getNotificationsCryptoAccountPath() { Context mainApplicationContext = MainApplication.getMainApplicationContext(); if (mainApplicationContext == null) { throw new RuntimeException( "Failed to resolve notifications crypto account path - main application context not initialized."); } return mainApplicationContext .getFileStreamPath("comm_notifications_crypto_account") .getPath(); } public static String getBackupDirectoryPath() { Context mainApplicationContext = MainApplication.getMainApplicationContext(); if (mainApplicationContext == null) { throw new RuntimeException( "Failed to resolve backup path - main application context not initialized."); } String filesDirPath = mainApplicationContext.getFilesDir().getPath(); String backupDirPath = String.join(File.separator, filesDirPath, "backup"); try { File backupDirectory = new File(backupDirPath); if (!backupDirectory.exists() && !backupDirectory.mkdirs()) { throw new RuntimeException("Failed to create backup directory."); } return backupDirPath; } catch (SecurityException | NullPointerException e) { throw new RuntimeException( "Failed to check if backup directory exists or to attempt its creation. Details: " + e.getMessage()); } } public static String getBackupFilePath(String backupID, boolean isAttachments) { String backupDirPath = PlatformSpecificTools.getBackupDirectoryPath(); String filename; if (isAttachments) { - filename = String.join("_", "backup", backupID, "attachments"); + filename = String.join("-", "backup", backupID, "attachments"); } else { - filename = String.join("_", "backup", backupID); + filename = String.join("-", "backup", backupID); } return String.join(File.separator, backupDirPath, filename); } public static void removeBackupDirectory() { String backupDirPath = PlatformSpecificTools.getBackupDirectoryPath(); try { File backupDirectory = new File(backupDirPath); if (!backupDirectory.exists()) { return; } File[] files = backupDirectory.listFiles(); if (files == null && !backupDirectory.delete()) { throw new RuntimeException("Failed to remove backup directory."); } else if (files == null) { return; } // Backup directory structure is supposed to be flat. for (File file : files) { if (!file.delete()) { throw new RuntimeException( "Failed to remove backup file at path: " + file.getPath()); } } if (!backupDirectory.delete()) { throw new RuntimeException("Failed to remove backup directory."); } } catch (NullPointerException | SecurityException e) { throw new RuntimeException( "Failed to remove backup directory. Details: " + e.getMessage()); } } } diff --git a/native/ios/Comm/PlatformSpecificTools.mm b/native/ios/Comm/PlatformSpecificTools.mm index 6efb32f52..42e66d99d 100644 --- a/native/ios/Comm/PlatformSpecificTools.mm +++ b/native/ios/Comm/PlatformSpecificTools.mm @@ -1,110 +1,110 @@ #import "PlatformSpecificTools.h" #import #import namespace comm { void PlatformSpecificTools::generateSecureRandomBytes( crypto::OlmBuffer &buffer, size_t size) { uint8_t randomBytes[size]; const int status = SecRandomCopyBytes(kSecRandomDefault, size, randomBytes); if (status == errSecSuccess) { buffer = crypto::OlmBuffer(randomBytes, randomBytes + size); } else { throw std::runtime_error( "SecRandomCopyBytes failed for some reason, error code: " + std::to_string(status)); } } std::string PlatformSpecificTools::getDeviceOS() { return std::string{"ios"}; } std::string PlatformSpecificTools::getNotificationsCryptoAccountPath() { NSURL *groupUrl = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:@"group.app.comm"]; if (groupUrl == nil) { throw std::runtime_error( "Failed to resolve notifications crypto account path - could not find " "groupUrl"); } return std::string( [[groupUrl URLByAppendingPathComponent:@"comm_notifications_crypto_account"] .path UTF8String]); } NSURL *getBackupDirAsURL() { NSError *err = nil; NSURL *documentsUrl = [NSFileManager.defaultManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:false error:&err]; if (err) { NSLog(@"Error: %@", err); throw std::runtime_error( "Failed to resolve backup path - could not find documentsUrl. " "Details: " + std::string([err.localizedDescription UTF8String])); } NSURL *backupDir = [documentsUrl URLByAppendingPathComponent:@"backup"]; NSError *backupDirCreateError = nil; if (![NSFileManager.defaultManager fileExistsAtPath:backupDir.path]) { [NSFileManager.defaultManager createDirectoryAtURL:backupDir withIntermediateDirectories:YES attributes:nil error:&backupDirCreateError]; } if (backupDirCreateError) { throw std::runtime_error( "Failed to create backup directory. Details: " + std::string([backupDirCreateError.localizedDescription UTF8String])); } return backupDir; } std::string PlatformSpecificTools::getBackupDirectoryPath() { return [getBackupDirAsURL().path UTF8String]; } std::string PlatformSpecificTools::getBackupFilePath( std::string backupID, bool isAttachments) { NSURL *backupDir = getBackupDirAsURL(); NSString *backupIDObjC = [NSString stringWithCString:backupID.c_str() encoding:NSUTF8StringEncoding]; NSString *filename; if (isAttachments) { filename = [@[ @"backup", backupIDObjC, @"attachments" ] - componentsJoinedByString:@"_"]; + componentsJoinedByString:@"-"]; } else { - filename = [@[ @"backup", backupIDObjC ] componentsJoinedByString:@"_"]; + filename = [@[ @"backup", backupIDObjC ] componentsJoinedByString:@"-"]; } return [[backupDir URLByAppendingPathComponent:filename].path UTF8String]; } void PlatformSpecificTools::removeBackupDirectory() { NSURL *backupDir = getBackupDirAsURL(); if (![NSFileManager.defaultManager fileExistsAtPath:backupDir.path]) { return; } NSError *backupDirRemovalError = nil; [NSFileManager.defaultManager removeItemAtURL:backupDir error:&backupDirRemovalError]; if (backupDirRemovalError) { throw std::runtime_error( "Failed to remove backup directory. Details: " + std::string([backupDirRemovalError.localizedDescription UTF8String])); } } }; // namespace comm