Skip to content

Commit

Permalink
MXFileStore: Be more robust on commit interruption: The metadata file…
Browse files Browse the repository at this point in the history
… needs to by backuped too.

element-hq/element-ios#376
  • Loading branch information
manuroe committed Aug 12, 2016
1 parent 3cca806 commit afb3475
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
14 changes: 8 additions & 6 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@
L accountData
L receipts
+ ...
+ roomsBackup: This folder contains backup of room files that are modified
during the commit process. It is flushed when the commit completes.
L MXFileStore : Information about the stored data
+ backup : This folder contains backup of files that are modified during
the commit process. It is flushed when the commit completes.
This allows to rollback to previous data if the commit process was
interrupted.
+ {syncToken} : the token that corresponds to the backup data
+ {roomIdA}
+ {roomIdB}
+ ...
L MXFileStore : Information about the stored data
+ rooms
+ {roomIdA}
+ {roomIdB}
+ ...
L MXFileStore
*/
@interface MXFileStore : MXMemoryStore

Expand Down
36 changes: 31 additions & 5 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

NSString *const kMXFileStoreFolder = @"MXFileStore";
NSString *const kMXFileStoreMedaDataFile = @"MXFileStore";
NSString *const kMXFileStoreBackupFolder = @"backup";

NSString *const kMXFileStoreSavingMarker = @"savingMarker";

NSString *const kMXFileStoreRoomsFolder = @"rooms";
NSString *const kMXFileStoreRoomsBackupFolder = @"roomsBackup";
NSString *const kMXFileStoreRoomMessagesFile = @"messages";
NSString *const kMXFileStoreRoomStateFile = @"state";
NSString *const kMXFileStoreRoomAccountDataFile = @"accountData";
Expand All @@ -52,6 +52,9 @@ @interface MXFileStore ()
// The path of the MXFileStore folder
NSString *storePath;

// The path of the backup folder
NSString *storeBackupPath;

// The path of the temporary file created during saving process.
NSString *savingMarkerFile;

Expand Down Expand Up @@ -111,7 +114,9 @@ - (void)openWithCredentials:(MXCredentials*)someCredentials onComplete:(void (^)
storePath = [[cachePath stringByAppendingPathComponent:kMXFileStoreFolder] stringByAppendingPathComponent:credentials.userId];
savingMarkerFile = [storePath stringByAppendingPathComponent:kMXFileStoreSavingMarker];
storeRoomsPath = [storePath stringByAppendingPathComponent:kMXFileStoreRoomsFolder];
storeRoomsBackupPath = [storePath stringByAppendingPathComponent:kMXFileStoreRoomsBackupFolder];

storeBackupPath = [storePath stringByAppendingPathComponent:kMXFileStoreBackupFolder];
storeRoomsBackupPath = [storeBackupPath stringByAppendingPathComponent:kMXFileStoreRoomsFolder];

/*
Mount data corresponding to the account credentials.
Expand Down Expand Up @@ -541,6 +546,18 @@ - (NSString*)readReceiptsFileForRoom:(NSString*)roomId forBackup:(BOOL)backup
return [[self folderForRoom:roomId forBackup:backup] stringByAppendingPathComponent:kMXFileStoreRoomReadReceiptsFile];
}

- (NSString*)metaDataFileForBackup:(BOOL)backup
{
if (!backup)
{
return [storePath stringByAppendingPathComponent:kMXFileStoreMedaDataFile];
}
else
{
return [storeBackupPath stringByAppendingPathComponent:kMXFileStoreMedaDataFile];
}
}


#pragma mark - Storage validity
- (BOOL)checkStorageValidity
Expand Down Expand Up @@ -802,9 +819,18 @@ - (void)saveMetaData
MXFileStoreMetaData *metaData2 = [metaData copy];

dispatch_async(dispatchQueue, ^(void){

NSString *metaDataFile = [storePath stringByAppendingPathComponent:kMXFileStoreMedaDataFile];
[NSKeyedArchiver archiveRootObject:metaData2 toFile:metaDataFile];

NSString *file = [self metaDataFileForBackup:NO];
NSString *backupFile = [self metaDataFileForBackup:YES];

// Backup the file
if ([[NSFileManager defaultManager] fileExistsAtPath:file])
{
[[NSFileManager defaultManager] moveItemAtPath:file toPath:backupFile error:nil];
}

// Store new data
[NSKeyedArchiver archiveRootObject:metaData2 toFile:file];
});
}
}
Expand Down

0 comments on commit afb3475

Please sign in to comment.