Skip to content

Commit 5e0f7e7

Browse files
ljhljh
ljh
authored and
ljh
committed
Try fix: Disk I/O error when device is locked
1 parent c952165 commit 5e0f7e7

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,49 @@ - (void)setDBPath:(NSString *)filePath
158158
if (self.bindingQueue && [self.dbPath isEqualToString:filePath]) {
159159
return;
160160
}
161-
161+
NSFileManager* fileManager = [NSFileManager defaultManager];
162162
// 创建数据库目录
163163
NSRange lastComponent = [filePath rangeOfString:@"/" options:NSBackwardsSearch];
164164

165165
if (lastComponent.length > 0) {
166166
NSString *dirPath = [filePath substringToIndex:lastComponent.location];
167167
BOOL isDir = NO;
168-
BOOL isCreated = [[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDir];
168+
BOOL isCreated = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
169169

170170
if ((isCreated == NO) || (isDir == NO)) {
171171
NSError *error = nil;
172-
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
172+
BOOL success = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES
173+
attributes:@{NSFileProtectionKey:NSFileProtectionNone} error:&error];
173174

174175
if (success == NO) {
175-
NSLog(@"create dir error: %@", error.debugDescription);
176+
LKErrorLog(@"create dir error: %@", error.debugDescription);
176177
}
177178
}
179+
else
180+
{
181+
/**
182+
* @brief Disk I/O error when device is locked
183+
* https://github.com/ccgus/fmdb/issues/262
184+
*/
185+
[fileManager setAttributes:@{NSFileProtectionKey:NSFileProtectionNone} ofItemAtPath:dirPath error:nil];
186+
}
178187
}
179-
188+
180189
self.dbPath = filePath;
181190
[self.bindingQueue close];
182-
self.bindingQueue = [[FMDatabaseQueue alloc]initWithPath:filePath];
191+
192+
#ifndef SQLITE_OPEN_FILEPROTECTION_NONE
193+
#define SQLITE_OPEN_FILEPROTECTION_NONE 0x00400000
194+
#endif
195+
196+
self.bindingQueue = [[FMDatabaseQueue alloc]initWithPath:filePath
197+
flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FILEPROTECTION_NONE];
198+
199+
if([fileManager fileExistsAtPath:filePath])
200+
{
201+
[fileManager setAttributes:@{NSFileProtectionKey:NSFileProtectionNone} ofItemAtPath:filePath error:nil];
202+
}
203+
183204
_encryptionKey = nil;
184205

185206
#ifdef DEBUG

0 commit comments

Comments
 (0)