Changeset View
Changeset View
Standalone View
Standalone View
native/ios/Comm/TemporaryMessageStorage/TemporaryMessageStorage.mm
#import "TemporaryMessageStorage.h" | #import "TemporaryMessageStorage.h" | ||||
#import "EncryptedFileUtils.h" | #import "EncryptedFileUtils.h" | ||||
#import "Logger.h" | #import "Logger.h" | ||||
#import "NonBlockingLock.h" | #import "NonBlockingLock.h" | ||||
#import <string> | #import <string> | ||||
@interface TemporaryMessageStorage () | @interface TemporaryMessageStorage () | ||||
- (NSString *)_createNewStorage; | - (NSString *)_createNewStorage; | ||||
- (NSString *)_getLockName:(NSString *)fileName; | - (NSString *)_getLockName:(NSString *)fileName; | ||||
- (NSString *)_getPath:(NSString *)fileName; | - (NSString *)_getPath:(NSString *)fileName; | ||||
@end | @end | ||||
@implementation TemporaryMessageStorage | @implementation TemporaryMessageStorage | ||||
- (instancetype)init { | - (instancetype)init { | ||||
self = [self initAtDirectory:@"TemporaryMessageStorage"]; | self = [self _initAtDirectory:@"TemporaryMessageStorage" | ||||
withFilePrefix:@"msg"]; | |||||
return self; | return self; | ||||
} | } | ||||
- (instancetype)initAtDirectory:(NSString *)directoryName { | - (instancetype)initForRescinds { | ||||
self = [self _initAtDirectory:@"TemporaryMessageStorageRescinds" | |||||
withFilePrefix:@"rsc"]; | |||||
return self; | |||||
} | |||||
- (instancetype)_initAtDirectory:(NSString *)directoryName | |||||
withFilePrefix:(NSString *)filePrefix { | |||||
self = [super init]; | self = [super init]; | ||||
if (!self) { | if (!self) { | ||||
return self; | return self; | ||||
} | } | ||||
NSURL *groupURL = [NSFileManager.defaultManager | NSURL *groupURL = [NSFileManager.defaultManager | ||||
containerURLForSecurityApplicationGroupIdentifier:@"group.app.comm"]; | containerURLForSecurityApplicationGroupIdentifier:@"group.app.comm"]; | ||||
NSURL *directoryURL = [groupURL URLByAppendingPathComponent:directoryName]; | NSURL *directoryURL = [groupURL URLByAppendingPathComponent:directoryName]; | ||||
Show All 9 Lines | - (instancetype)_initAtDirectory:(NSString *)directoryName | ||||
if (err) { | if (err) { | ||||
comm::Logger::log( | comm::Logger::log( | ||||
"Failed to create directory at path: " + | "Failed to create directory at path: " + | ||||
std::string([directoryPath UTF8String]) + | std::string([directoryPath UTF8String]) + | ||||
". Details: " + std::string([err.localizedDescription UTF8String])); | ". Details: " + std::string([err.localizedDescription UTF8String])); | ||||
} | } | ||||
_directoryURL = directoryURL; | _directoryURL = directoryURL; | ||||
_directoryPath = directoryPath; | _directoryPath = directoryPath; | ||||
_filePrefix = filePrefix; | |||||
return self; | return self; | ||||
} | } | ||||
- (void)writeMessage:(NSString *)message { | - (void)writeMessage:(NSString *)message { | ||||
NSArray<NSString *> *storageContent = | NSArray<NSString *> *storageContent = | ||||
[NSFileManager.defaultManager contentsOfDirectoryAtPath:self.directoryPath | [NSFileManager.defaultManager contentsOfDirectoryAtPath:self.directoryPath | ||||
error:nil]; | error:nil]; | ||||
if (!storageContent) { | if (!storageContent) { | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | if (!fileMessages) { | ||||
[allMessages addObjectsFromArray:fileMessages]; | [allMessages addObjectsFromArray:fileMessages]; | ||||
} | } | ||||
} | } | ||||
return allMessages; | return allMessages; | ||||
} | } | ||||
- (NSString *)_createNewStorage { | - (NSString *)_createNewStorage { | ||||
int64_t timestamp = (int64_t)[NSDate date].timeIntervalSince1970; | int64_t timestamp = (int64_t)[NSDate date].timeIntervalSince1970; | ||||
NSString *newStorageName = [NSString stringWithFormat:@"msg_%lld", timestamp]; | NSString *newStorageName = | ||||
[NSString stringWithFormat:@"%@_%lld", self.filePrefix, timestamp]; | |||||
tomek: Why do we need different file prefixes? Shouldn't the content be distinguished by the directory… | |||||
marcinAuthorUnsubmitted Done Inline ActionsThe content is distinguished by the directory name, but the semaphore name isn't. Unfortunately POSIX semaphore impose restriction on how long semaphore name can be. I don't remember by heart what the exact limit was but I remember that using an absolute path to the file as the name would exceed it. marcin: The content is distinguished by the directory name, but the semaphore name isn't. Unfortunately… | |||||
NSString *newStoragePath = | NSString *newStoragePath = | ||||
[self.directoryURL URLByAppendingPathComponent:newStorageName].path; | [self.directoryURL URLByAppendingPathComponent:newStorageName].path; | ||||
const char *newStoragePathCstr = | const char *newStoragePathCstr = | ||||
[newStoragePath cStringUsingEncoding:NSUTF8StringEncoding]; | [newStoragePath cStringUsingEncoding:NSUTF8StringEncoding]; | ||||
FILE *newStorage = fopen(newStoragePathCstr, "a"); | FILE *newStorage = fopen(newStoragePathCstr, "a"); | ||||
if (!newStorage) { | if (!newStorage) { | ||||
return nil; | return nil; | ||||
} | } | ||||
Show All 16 Lines |
Why do we need different file prefixes? Shouldn't the content be distinguished by the directory name?