Skip to content

Commit

Permalink
MXFileStore: Be more robust on commit interruption: Start to backup f…
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Aug 12, 2016
1 parent b454673 commit 3cca806
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,19 @@ - (void)saveRoomsMessages
MXFileRoomStore *roomStore = roomStores[roomId];
if (roomStore)
{
NSString *file = [self messagesFileForRoom:roomId forBackup:NO];
NSString *backupFile = [self messagesFileForRoom:roomId forBackup:YES];

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

// Store new data
[self checkFolderExistenceForRoom:roomId forBackup:NO];
[NSKeyedArchiver archiveRootObject:roomStore toFile:[self messagesFileForRoom:roomId forBackup:NO]];
[NSKeyedArchiver archiveRootObject:roomStore toFile:file];
}
}

Expand Down Expand Up @@ -658,8 +669,19 @@ - (void)saveRoomsState
{
NSArray *stateEvents = roomsToCommit[roomId];

NSString *file = [self stateFileForRoom:roomId forBackup:NO];
NSString *backupFile = [self stateFileForRoom:roomId forBackup:YES];

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

// Store new data
[self checkFolderExistenceForRoom:roomId forBackup:NO];
[NSKeyedArchiver archiveRootObject:stateEvents toFile:[self stateFileForRoom:roomId forBackup:NO]];
[NSKeyedArchiver archiveRootObject:stateEvents toFile:file];
}
});
}
Expand Down Expand Up @@ -698,8 +720,19 @@ - (void)saveRoomsAccountData
{
MXRoomAccountData *roomAccountData = roomsToCommit[roomId];

NSString *file = [self accountDataFileForRoom:roomId forBackup:NO];
NSString *backupFile = [self accountDataFileForRoom:roomId forBackup:YES];

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

// Store new data
[self checkFolderExistenceForRoom:roomId forBackup:NO];
[NSKeyedArchiver archiveRootObject:roomAccountData toFile:[self accountDataFileForRoom:roomId forBackup:NO]];
[NSKeyedArchiver archiveRootObject:roomAccountData toFile:file];
}
});
}
Expand Down Expand Up @@ -878,12 +911,19 @@ - (void)saveReceipts
{
@synchronized (receiptsByUserId)
{
[self checkFolderExistenceForRoom:roomId forBackup:NO];
BOOL success = [NSKeyedArchiver archiveRootObject:receiptsByUserId toFile:[self readReceiptsFileForRoom:roomId forBackup:NO]];
if (!success)
NSString *file = [self readReceiptsFileForRoom:roomId forBackup:NO];
NSString *backupFile = [self readReceiptsFileForRoom:roomId forBackup:YES];

// Backup the file
if ([[NSFileManager defaultManager] fileExistsAtPath:file])
{
NSLog(@"[MXFileStore] Error: Failed to store read receipts for room %@", roomId);
[self checkFolderExistenceForRoom:roomId forBackup:YES];
[[NSFileManager defaultManager] moveItemAtPath:file toPath:backupFile error:nil];
}

// Store new data
[self checkFolderExistenceForRoom:roomId forBackup:NO];
[NSKeyedArchiver archiveRootObject:receiptsByUserId toFile:file];
}
}
}
Expand Down

0 comments on commit 3cca806

Please sign in to comment.