Skip to content

Commit

Permalink
[native] Filepath generation for log and userkeys data
Browse files Browse the repository at this point in the history
Summary:
[ENG-6216 : Add rust code for getting filepath with compations/logs](https://linear.app/comm/issue/ENG-6216/add-rust-code-for-getting-filepath-with-compationslogs)
Adds two new functions for files used for backup data storage on the device (while they are waiting to be confirmed by backup service): one for logs (with optional attachments file) and the second one for userkeys file.

Additionally changes the functions to take `&str` (`rust::Str`) instead of `String` (`rust::String`) as we are making a copy of them to std::string immediately and don't need to pass an owned string.

Depends on D10604

Test Plan:
Called these functions from rust and got these results:
```
backup-123-userkeys
backup-123-log-456
backup-123-log-456-attachments
```

Reviewers: marcin, kamil

Reviewed By: marcin

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D10605
  • Loading branch information
MichalGniadek committed Jan 16, 2024
1 parent b185d93 commit b096784
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 3 deletions.
40 changes: 40 additions & 0 deletions native/android/app/src/cpp/PlatformSpecificTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ class PlatformSpecificToolsJavaClass
return method(cls, backupID, isAttachments)->toStdString();
}

static std::string getBackupLogFilePath(
std::string backupID,
std::string logID,
bool isAttachments) {
static const auto cls = javaClassStatic();
static auto method =
cls->getStaticMethod<JString(std::string, std::string, bool)>(
"getBackupLogFilePath");
return method(cls, backupID, logID, isAttachments)->toStdString();
}

static std::string getBackupUserKeysFilePath(std::string backupID) {
static const auto cls = javaClassStatic();
static auto method =
cls->getStaticMethod<JString(std::string)>("getBackupUserKeysFilePath");
return method(cls, backupID)->toStdString();
}

static void removeBackupDirectory() {
static const auto cls = javaClassStatic();
static auto method = cls->getStaticMethod<void()>("removeBackupDirectory");
Expand Down Expand Up @@ -95,6 +113,28 @@ std::string PlatformSpecificTools::getBackupFilePath(
return path;
}

std::string PlatformSpecificTools::getBackupLogFilePath(
std::string backupID,
std::string logID,
bool isAttachments) {
std::string path;
NativeAndroidAccessProvider::runTask(
[&path, backupID, logID, isAttachments]() {
path = PlatformSpecificToolsJavaClass::getBackupLogFilePath(
backupID, logID, isAttachments);
});
return path;
}

std::string
PlatformSpecificTools::getBackupUserKeysFilePath(std::string backupID) {
std::string path;
NativeAndroidAccessProvider::runTask([&path, backupID]() {
path = PlatformSpecificToolsJavaClass::getBackupUserKeysFilePath(backupID);
});
return path;
}

void PlatformSpecificTools::removeBackupDirectory() {
NativeAndroidAccessProvider::runTask(
[]() { PlatformSpecificToolsJavaClass::removeBackupDirectory(); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public static String getBackupDirectoryPath() {
return String.join(File.separator, backupDirPath, filename);
}

public static String
getBackupLogFilePath(String backupID, String logID, boolean isAttachments) {
String backupDirPath = PlatformSpecificTools.getBackupDirectoryPath();

String filename;
if (isAttachments) {
filename =
String.join("-", "backup", backupID, "log", logID, "attachments");
} else {
filename = String.join("-", "backup", backupID, "log", logID);
}
return String.join(File.separator, backupDirPath, filename);
}

public static String getBackupUserKeysFilePath(String backupID) {
String backupDirPath = PlatformSpecificTools.getBackupDirectoryPath();
String filename = String.join("-", "backup", backupID, "userkeys");
return String.join(File.separator, backupDirPath, filename);
}

public static void removeBackupDirectory() {
String backupDirPath = PlatformSpecificTools.getBackupDirectoryPath();
try {
Expand Down
5 changes: 5 additions & 0 deletions native/cpp/CommonCpp/Tools/PlatformSpecificTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class PlatformSpecificTools {
static std::string getBackupDirectoryPath();
static std::string
getBackupFilePath(std::string backupID, bool isAttachments);
static std::string getBackupLogFilePath(
std::string backupID,
std::string logID,
bool isAttachments);
static std::string getBackupUserKeysFilePath(std::string backupID);
static void removeBackupDirectory();
};

Expand Down
32 changes: 32 additions & 0 deletions native/ios/Comm/PlatformSpecificTools.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@
return [[backupDir URLByAppendingPathComponent:filename].path UTF8String];
}

std::string PlatformSpecificTools::getBackupLogFilePath(
std::string backupID,
std::string logID,
bool isAttachments) {

NSURL *backupDir = getBackupDirAsURL();
NSString *backupIDObjC = [NSString stringWithCString:backupID.c_str()
encoding:NSUTF8StringEncoding];
NSString *logIDObjC = [NSString stringWithCString:logID.c_str()
encoding:NSUTF8StringEncoding];
NSString *filename;
if (isAttachments) {
filename = [@[ @"backup", backupIDObjC, @"log", logIDObjC, @"attachments" ]
componentsJoinedByString:@"-"];
} else {
filename = [@[ @"backup", backupIDObjC, @"log", logIDObjC ]
componentsJoinedByString:@"-"];
}
return [[backupDir URLByAppendingPathComponent:filename].path UTF8String];
}

std::string
PlatformSpecificTools::getBackupUserKeysFilePath(std::string backupID) {

NSURL *backupDir = getBackupDirAsURL();
NSString *backupIDObjC = [NSString stringWithCString:backupID.c_str()
encoding:NSUTF8StringEncoding];
NSString *filename =
[@[ @"backup", backupIDObjC, @"userkeys" ] componentsJoinedByString:@"-"];
return [[backupDir URLByAppendingPathComponent:filename].path UTF8String];
}

void PlatformSpecificTools::removeBackupDirectory() {
NSURL *backupDir = getBackupDirAsURL();
if (![NSFileManager.defaultManager fileExistsAtPath:backupDir.path]) {
Expand Down
13 changes: 12 additions & 1 deletion native/native_rust_library/RustBackupExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ rust::String getBackupDirectoryPath() {
return rust::String(PlatformSpecificTools::getBackupDirectoryPath());
}

rust::String getBackupFilePath(rust::String backupID, bool isAttachments) {
rust::String getBackupFilePath(rust::Str backupID, bool isAttachments) {
return rust::String(PlatformSpecificTools::getBackupFilePath(
std::string(backupID), isAttachments));
}

rust::String
getBackupLogFilePath(rust::Str backupID, rust::Str logID, bool isAttachments) {
return rust::String(PlatformSpecificTools::getBackupLogFilePath(
std::string(backupID), std::string(logID), isAttachments));
}

rust::String getBackupUserKeysFilePath(rust::Str backupID) {
return rust::String(
PlatformSpecificTools::getBackupUserKeysFilePath(std::string(backupID)));
}
} // namespace comm
5 changes: 4 additions & 1 deletion native/native_rust_library/RustBackupExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace comm {

rust::String getBackupDirectoryPath();
rust::String getBackupFilePath(rust::String backupID, bool isAttachments);
rust::String getBackupFilePath(rust::Str backupID, bool isAttachments);
rust::String
getBackupLogFilePath(rust::Str backupID, rust::Str logID, bool isAttachments);
rust::String getBackupUserKeysFilePath(rust::Str backupID);

} // namespace comm
14 changes: 13 additions & 1 deletion native/native_rust_library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,21 @@ mod ffi {
#[allow(unused)]
#[cxx_name = "getBackupFilePath"]
fn get_backup_file_path(
backup_id: String,
backup_id: &str,
is_attachments: bool,
) -> Result<String>;

#[allow(unused)]
#[cxx_name = "getBackupLogFilePath"]
fn get_backup_log_file_path(
backup_id: &str,
log_id: &str,
is_attachments: bool,
) -> Result<String>;

#[allow(unused)]
#[cxx_name = "getBackupUserKeysFilePath"]
fn get_backup_user_keys_file_path(backup_id: &str) -> Result<String>;
}
}

Expand Down

0 comments on commit b096784

Please sign in to comment.