Clear history older than certain date working
This commit is contained in:
parent
c2da77f0a0
commit
d052dc29ba
|
@ -233,7 +233,7 @@ static NSMenuItem *menuItemForItem(id item) {
|
||||||
-(void)clearBrowsingHistory {
|
-(void)clearBrowsingHistory {
|
||||||
NSUInteger days = [[Preferences defaultPreferences] browsingHistoryDays];
|
NSUInteger days = [[Preferences defaultPreferences] browsingHistoryDays];
|
||||||
if (days > 0) {
|
if (days > 0) {
|
||||||
NSLog(@"Clearing history older thna %u", days);
|
NSLog(@"Clearing history older than %u days", days);
|
||||||
[Website deleteHistoryOlderThanDays: days];
|
[Website deleteHistoryOlderThanDays: days];
|
||||||
} else {
|
} else {
|
||||||
NSLog(@"browsingHistoryDays preference set to 0, won't clear history");
|
NSLog(@"browsingHistoryDays preference set to 0, won't clear history");
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
struct website_data {
|
struct website_data {
|
||||||
int len_name;
|
int len_name;
|
||||||
// A 0 value of len_url implies that the data has been 'cleared', and should be
|
// A 0 value of len_url implies that the data has been 'cleared', and should be
|
||||||
// ignored. In this case, len_name will be the entire length of the structure.
|
// ignored. In this case, len_name will be the entire length of 'data'.
|
||||||
// See HistoryWindowController for the impl of this.
|
// See HistoryWindowController for the impl of this.
|
||||||
int len_url;
|
int len_url;
|
||||||
NSTimeInterval timeIntervalSinceReferenceDate;
|
NSTimeInterval timeIntervalSinceReferenceDate;
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#import "AppDelegate.h"
|
#import "AppDelegate.h"
|
||||||
|
|
||||||
@interface Website(Private)
|
@interface Website(Private)
|
||||||
+(void)truncateHistoryFileAtPath: (NSString*)path olderThanDate: (NSDate*)date;
|
+(void)zeroHistoryFileAtPath: (NSString*)path olderThanDate: (NSDate*)date;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation Website
|
@implementation Website
|
||||||
|
@ -161,6 +161,7 @@
|
||||||
NSLog(@"Error opening file: %@", path);
|
NSLog(@"Error opening file: %@", path);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
NSLog(@"Parsing file: %@", path);
|
||||||
fileoff = 0;
|
fileoff = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
if ((nread = fread(lens, sizeof (int), 2, f)) < 2) {
|
if ((nread = fread(lens, sizeof (int), 2, f)) < 2) {
|
||||||
|
@ -175,6 +176,10 @@
|
||||||
// Else it's valid, rewind and read the whole structure in.
|
// Else it's valid, rewind and read the whole structure in.
|
||||||
fseek(f, -nread * sizeof (int), SEEK_CUR);
|
fseek(f, -nread * sizeof (int), SEEK_CUR);
|
||||||
wdata = malloc(wsize);
|
wdata = malloc(wsize);
|
||||||
|
if (wdata == NULL) {
|
||||||
|
perror("NULL MALLOC?!");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
fread(wdata, wsize, 1, f);
|
fread(wdata, wsize, 1, f);
|
||||||
website = [[[Website alloc] initWithData: wdata atFileOffset: fileoff]
|
website = [[[Website alloc] initWithData: wdata atFileOffset: fileoff]
|
||||||
autorelease];
|
autorelease];
|
||||||
|
@ -203,7 +208,6 @@
|
||||||
}
|
}
|
||||||
[files filterUsingPredicate: historyPredicate];
|
[files filterUsingPredicate: historyPredicate];
|
||||||
[files sortUsingSelector: @selector(caseInsensitiveCompare:)];
|
[files sortUsingSelector: @selector(caseInsensitiveCompare:)];
|
||||||
NSLog(@"%@", files);
|
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,52 +261,100 @@
|
||||||
if (fileYear == targetYear || fileMonth == targetMonth) {
|
if (fileYear == targetYear || fileMonth == targetMonth) {
|
||||||
fullPath = [NSString stringWithFormat: @"%@/%@", historyPath,
|
fullPath = [NSString stringWithFormat: @"%@/%@", historyPath,
|
||||||
currentMonth];
|
currentMonth];
|
||||||
[Website truncateHistoryFileAtPath: fullPath olderThanDate: date];
|
char *cTmpPath = tempnam(NULL, NULL);
|
||||||
|
NSString *tmpPath = [NSString stringWithCString: cTmpPath];
|
||||||
|
NSLog(@"Copying file from '%@', to '%@'", fullPath, tmpPath);
|
||||||
|
err = nil;
|
||||||
|
[[NSFileManager defaultManager] copyItemAtPath: fullPath toPath: tmpPath
|
||||||
|
error: &err];
|
||||||
|
if (err != nil) {
|
||||||
|
NSLog(@"Error copying history to temp file at path: %@ (%@)", tmpPath, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[Website zeroHistoryFileAtPath: tmpPath olderThanDate: date];
|
||||||
|
err = nil;
|
||||||
|
[[NSFileManager defaultManager] removeItemAtPath: fullPath error: &err];
|
||||||
|
if (err != nil) {
|
||||||
|
NSLog(@"Failed to remove existing history file at path: %@", fullPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[[NSFileManager defaultManager] moveItemAtPath: tmpPath toPath: fullPath
|
||||||
|
error: &err];
|
||||||
|
if (err != nil) {
|
||||||
|
NSLog(@"Failed to copy back the tmp file at %@, to path %@", tmpPath,
|
||||||
|
fullPath);
|
||||||
|
} else {
|
||||||
|
NSLog(@"Copied history file back in place");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+(void)truncateHistoryFileAtPath: (NSString*)path olderThanDate: (NSDate*)date {
|
+(void)zeroHistoryFileAtPath: (NSString*)path olderThanDate: (NSDate*)date {
|
||||||
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath: path];
|
NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath: path];
|
||||||
if (handle == nil) {
|
if (handle == nil) {
|
||||||
NSLog(@"Failed to open history file for updating at path: %@", path);
|
NSLog(@"Failed to open history file for updating at path: %@", path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NSTimeInterval ival = [date timeIntervalSinceReferenceDate];
|
NSTimeInterval ival = [date timeIntervalSinceReferenceDate];
|
||||||
struct website_data buf[10];
|
NSLog(@"clear history older than: %f", ival);
|
||||||
NSUInteger nread, i;
|
|
||||||
NSData *data;
|
NSData *data;
|
||||||
|
NSError *err = nil;
|
||||||
|
BOOL foundEnd = NO;
|
||||||
|
int totalSize, largestSize;
|
||||||
|
largestSize = 100;
|
||||||
|
struct website_data *wdata = malloc(largestSize);
|
||||||
do {
|
do {
|
||||||
data = [handle readDataUpToLength: sizeof(buf)];
|
data = [handle readDataOfLength: offsetof(struct website_data, data)];
|
||||||
if (data == nil) {
|
if (data == nil || [data length] == 0) {
|
||||||
NSLog(@"readDataUpToLength failed");
|
NSLog(@"readDataUpToLength 1 failed (EOF?)");
|
||||||
return;
|
|
||||||
}
|
|
||||||
nread = [data length] / sizeof (struct website_data);
|
|
||||||
[data getBytes: buf length: nread * sizeof (struct website_data)];
|
|
||||||
for (i = 0; i < nread; i++) {
|
|
||||||
if (buf[i].timeIntervalSinceReferenceDate < ival)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (nread == 10);
|
[data getBytes: wdata length: offsetof(struct website_data, data)];
|
||||||
// If we didn't iterate to the end, must thave found a point with older time.
|
|
||||||
if (i == nread) {
|
data = [handle readDataOfLength: wdata->len_name + wdata->len_url];
|
||||||
NSLog(@"Reached the end of history file without finding older entries");
|
if (data == nil || [data length] == 0) {
|
||||||
|
NSLog(@"readDataUpToLength 2 failed (EOF?)");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
totalSize = sizeof (struct website_data) + wdata->len_name + wdata->len_url;
|
||||||
|
if (totalSize > largestSize) {
|
||||||
|
wdata = realloc(wdata, totalSize);
|
||||||
|
largestSize = totalSize;
|
||||||
|
}
|
||||||
|
[data getBytes: &(wdata->data) length: wdata->len_name + wdata->len_url];
|
||||||
|
if (wdata->len_url == 0) {
|
||||||
|
// Deleted entry, skip.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (wdata->timeIntervalSinceReferenceDate > ival) {
|
||||||
|
NSLog(@"End found");
|
||||||
|
foundEnd = YES;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (!foundEnd);
|
||||||
|
free(wdata);
|
||||||
|
|
||||||
|
if (!foundEnd) {
|
||||||
|
NSLog(@"Reached the end of history file without finding newer entries. Clear entire file.");
|
||||||
|
[handle truncateFileAtOffset: 0];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Rewind the file back to the start of that point.
|
unsigned long long offset = [handle offsetInFile];
|
||||||
unsigned long long offset;
|
|
||||||
long long rwnd = (long long)((i - nread) * sizeof (struct website_data));
|
|
||||||
NSError *err = nil;
|
|
||||||
[handle getOffset: &offset error: &err];
|
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
NSLog(@"Failed to get file offset");
|
NSLog(@"Failed to get file offset");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
offset += rwnd;
|
offset -= (unsigned long long)totalSize;
|
||||||
[handle truncateAtOffset: offset error: &err];
|
if (offset == 0) {
|
||||||
if (err != nil) {
|
NSLog(@"No entries need clearing.");
|
||||||
NSLog(@"Failed to truncate history file");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
wdata = calloc(1, offset);
|
||||||
|
wdata->len_name = offset - sizeof (struct website_data);
|
||||||
|
wdata->len_url = 0;
|
||||||
|
[handle seekToFileOffset: 0];
|
||||||
|
[handle writeData: [NSData dataWithBytesNoCopy: wdata length: offset]];
|
||||||
|
[handle closeFile];
|
||||||
|
NSLog(@"Zero'd %@ to %llu", path, offset);
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue