feat: use temporary folder for lldb if support is locked

This commit is contained in:
Serhiy Mytrovtsiy
2024-04-10 21:07:26 +02:00
parent 7c8baef330
commit a7cc898e16
2 changed files with 35 additions and 14 deletions

View File

@@ -27,19 +27,23 @@ using namespace std;
- (instancetype) init:(NSString *) name {
self = [super init];
if (self) {
[self createDB:name];
bool status = [self createDB:name];
if (!status) {
return nil;
}
}
return self;
}
-(void)createDB:(NSString *) path {
-(bool)createDB:(NSString *) path {
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, [path UTF8String], &self->db);
if (false == status.ok()) {
NSLog(@"ERROR: Unable to open/create database.");
std::cout << status.ToString();
NSLog(@"ERROR: Unable to open/create database: %s", status.ToString().c_str());
return false;
}
return true;
}
-(NSArray *)keys:(NSString *)key {

View File

@@ -31,19 +31,36 @@ public class DB {
}
init() {
var dbPath: URL
let fileManager = FileManager.default
let appSupportURL = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let folder = appSupportURL.appendingPathComponent("Stats")
do {
try fileManager.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
dbPath = folder.appendingPathComponent("lldb")
} catch {
dbPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Stats").appendingPathComponent("lldb")
let supportPath = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!.appendingPathComponent("Stats")
let tmpPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Stats")
try? fileManager.createDirectory(at: supportPath, withIntermediateDirectories: true, attributes: nil)
try? fileManager.createDirectory(at: tmpPath, withIntermediateDirectories: true, attributes: nil)
var dbURL: URL?
var tmpURL: URL?
if let values = try? supportPath.resourceValues(forKeys: [.isDirectoryKey]), values.isDirectory ?? false {
dbURL = supportPath.appendingPathComponent("lldb")
}
if let values = try? tmpPath.resourceValues(forKeys: [.isDirectoryKey]), values.isDirectory ?? false {
tmpURL = tmpPath.appendingPathComponent("lldb")
}
if dbURL == nil && tmpURL != nil {
dbURL = tmpURL
}
self.lldb = LLDB(dbPath.path)
if let url = dbURL, let lldb = LLDB(url.path) {
self.lldb = lldb
return
}
if let url = tmpURL, let lldb = LLDB(url.path) {
self.lldb = lldb
return
}
print("ERROR INITIALIZE DB")
}
deinit {