diff --git a/Kit/lldb/lldb.m b/Kit/lldb/lldb.m index 1c242884..5cbd09e3 100644 --- a/Kit/lldb/lldb.m +++ b/Kit/lldb/lldb.m @@ -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 { diff --git a/Kit/plugins/DB.swift b/Kit/plugins/DB.swift index 16738c5e..716cb9e5 100644 --- a/Kit/plugins/DB.swift +++ b/Kit/plugins/DB.swift @@ -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 {