Skip to content

Commit e2b5073

Browse files
committed
Delete temporary files when they are deallocated
This will remove a lot of the stray files in the temporary dir. However, they won't be deleted on exit of the program. What to do about this?
1 parent b18b957 commit e2b5073

File tree

10 files changed

+470
-344
lines changed

10 files changed

+470
-344
lines changed

English.lproj/MainMenu.xib

Lines changed: 320 additions & 320 deletions
Large diffs are not rendered by default.

GitX.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
F58A8F280E043698007E3FC0 /* commits.css in Resources */ = {isa = PBXBuildFile; fileRef = F58A8F270E043698007E3FC0 /* commits.css */; };
3030
F5945E170E02B0C200706420 /* PBGitRepository.m in Sources */ = {isa = PBXBuildFile; fileRef = F5945E160E02B0C200706420 /* PBGitRepository.m */; };
3131
F5B721C40E05CF7E00AF29DC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F5B721C20E05CF7E00AF29DC /* MainMenu.xib */; };
32+
F5DFFA6C0E075D8800617813 /* PBEasyFS.m in Sources */ = {isa = PBXBuildFile; fileRef = F5DFFA6B0E075D8800617813 /* PBEasyFS.m */; };
3233
/* End PBXBuildFile section */
3334

3435
/* Begin PBXFileReference section */
@@ -70,6 +71,8 @@
7071
F5945E150E02B0C200706420 /* PBGitRepository.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepository.h; sourceTree = "<group>"; };
7172
F5945E160E02B0C200706420 /* PBGitRepository.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRepository.m; sourceTree = "<group>"; };
7273
F5B721C30E05CF7E00AF29DC /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
74+
F5DFFA6A0E075D8800617813 /* PBEasyFS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyFS.h; sourceTree = "<group>"; };
75+
F5DFFA6B0E075D8800617813 /* PBEasyFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyFS.m; sourceTree = "<group>"; };
7376
/* End PBXFileReference section */
7477

7578
/* Begin PBXFrameworksBuildPhase section */
@@ -206,6 +209,8 @@
206209
F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */,
207210
F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */,
208211
F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */,
212+
F5DFFA6A0E075D8800617813 /* PBEasyFS.h */,
213+
F5DFFA6B0E075D8800617813 /* PBEasyFS.m */,
209214
F53EE3590E06BBA00022B925 /* CWQuickLook.h */,
210215
F51308590E0740F2000C8BCD /* PBQLOutlineView.h */,
211216
F513085A0E0740F2000C8BCD /* PBQLOutlineView.m */,
@@ -299,6 +304,7 @@
299304
F57CC3910E05DDF2000472E2 /* PBEasyPipe.m in Sources */,
300305
F57CC4410E05E496000472E2 /* PBDetailController.m in Sources */,
301306
F513085B0E0740F2000C8BCD /* PBQLOutlineView.m in Sources */,
307+
F5DFFA6C0E075D8800617813 /* PBEasyFS.m in Sources */,
302308
);
303309
runOnlyForDeploymentPostprocessing = 0;
304310
};
@@ -361,6 +367,7 @@
361367
26FC0A890875C7B200E6366F /* Debug */ = {
362368
isa = XCBuildConfiguration;
363369
buildSettings = {
370+
GCC_ENABLE_OBJC_GC = required;
364371
GCC_WARN_ABOUT_RETURN_TYPE = YES;
365372
GCC_WARN_UNUSED_VARIABLE = YES;
366373
PREBINDING = NO;
@@ -375,6 +382,7 @@
375382
ppc,
376383
i386,
377384
);
385+
GCC_ENABLE_OBJC_GC = required;
378386
GCC_WARN_ABOUT_RETURN_TYPE = YES;
379387
GCC_WARN_UNUSED_VARIABLE = YES;
380388
PREBINDING = NO;

PBDetailController.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ @implementation PBDetailController
2929
- (void) updateKeys
3030
{
3131
NSArray* selection = [commitController selectedObjects];
32+
33+
// Remove any references in the QLPanel
34+
[[QLPreviewPanel sharedPreviewPanel] setURLs:[NSArray array] currentIndex:0 preservingDisplayState:YES];
35+
// We have to do this manually, as NSTreeController leaks memory?
36+
[treeController setSelectionIndexPaths:[NSArray array]];
37+
3238
if ([selection count] > 0)
3339
realCommit = [selection objectAtIndex:0];
3440
else
@@ -38,6 +44,7 @@ - (void) updateKeys
3844
self.rawCommit = nil;
3945
self.gitTree = nil;
4046

47+
4148
int num = [self.selectedTab intValue];
4249

4350
if (num == 0) // Detailed view

PBEasyFS.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// PBEasyFS.h
3+
// GitX
4+
//
5+
// Created by Pieter de Bie on 6/17/08.
6+
// Copyright 2008 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
12+
@interface PBEasyFS : NSObject {
13+
14+
}
15+
+ (NSString*) tmpNameWithSuffix: (NSString*) path;
16+
+ (NSString*) tmpDirWithPrefix: (NSString*) path;
17+
18+
@end

PBEasyFS.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// PBEasyFS.m
3+
// GitX
4+
//
5+
// Created by Pieter de Bie on 6/17/08.
6+
// Copyright 2008 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "PBEasyFS.h"
10+
11+
12+
@implementation PBEasyFS
13+
14+
+ (NSString*) tmpNameWithSuffix: (NSString*) path
15+
{
16+
NSString* newName = [NSString stringWithFormat: @"%@/XXXXXX%@", NSTemporaryDirectory(), path];
17+
char *template = (char*) [newName fileSystemRepresentation];
18+
int fd = mkstemps(template, [path length]);
19+
close(fd);
20+
return [NSString stringWithUTF8String:template];
21+
}
22+
23+
+ (NSString*) tmpDirWithPrefix: (NSString*) path
24+
{
25+
NSString* newName = [NSString stringWithFormat: @"%@%@.XXXXXX", NSTemporaryDirectory(), path];
26+
char *template = (char*) [newName fileSystemRepresentation];
27+
template = mkdtemp(template);
28+
return [NSString stringWithUTF8String:template];
29+
}
30+
31+
@end

PBEasyPipe.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@
1818

1919
+ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args;
2020
+ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir;
21-
22-
+ (NSString*) writeData:(NSData*) data toTempFileWithName: (NSString *) fileName;
2321
@end

PBEasyPipe.m

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,4 @@ + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
5151
return [self outputForCommand:cmd withArgs:args inDir:nil];
5252
}
5353

54-
+ (NSString*) writeData:(NSData*) data toTempFileWithName: (NSString *) fileName
55-
{
56-
NSString* newName = [NSString stringWithFormat: @"%@/XXXXXX%@", NSTemporaryDirectory(), fileName];
57-
char *template = (char*) [newName fileSystemRepresentation];
58-
int fd = mkstemps(template, [fileName length]);
59-
NSFileHandle* handle = [[NSFileHandle alloc] initWithFileDescriptor:fd];
60-
[handle writeData: data];
61-
[handle closeFile];
62-
return [NSString stringWithUTF8String:template];
63-
}
64-
6554
@end

PBGitRepository.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ - (void) setCommits:(NSArray*) obj
7373

7474
- (void) initializeCommits
7575
{
76-
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
76+
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
7777

7878
NSMutableArray * newArray = [NSMutableArray array];
7979
NSDate* start = [NSDate date];
@@ -96,7 +96,7 @@ - (void) initializeCommits
9696
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
9797
NSLog(@"Loaded %i commits in %f seconds", num, duration);
9898

99-
[pool release];
99+
//[pool release];
100100
[NSThread exit];
101101
}
102102

PBGitTree.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
NSString* sha;
1414
NSString* path;
1515
PBGitRepository* repository;
16-
PBGitTree* parent;
16+
__weak PBGitTree* parent;
1717
NSArray* children;
1818
BOOL leaf;
19+
20+
NSString* localFileName;
21+
NSDate* localMtime;
1922
}
2023

2124
+ (PBGitTree*) rootForCommit: (id) commit;
@@ -27,7 +30,7 @@
2730
@property(copy) NSString* path;
2831
@property(assign) BOOL leaf;
2932
@property(retain) PBGitRepository* repository;
30-
@property(assign) PBGitTree* parent;
33+
@property(assign) __weak PBGitTree* parent;
3134

3235
@property(readonly) NSArray* children;
3336
@property(readonly) NSString* fullPath;

PBGitTree.m

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PBGitCommit.h"
1111
#import "NSFileHandleExt.h"
1212
#import "PBEasyPipe.h"
13+
#import "PBEasyFS.h"
1314

1415
@implementation PBGitTree
1516

@@ -40,6 +41,7 @@ + (PBGitTree*) treeForTree: (PBGitTree*) prev andPath: (NSString*) path;
4041
- init
4142
{
4243
children = nil;
44+
localFileName = nil;
4345
leaf = YES;
4446
return self;
4547
}
@@ -49,25 +51,89 @@ - (NSString*) refSpec
4951
return [NSString stringWithFormat:@"%@:%@", self.sha, self.fullPath];
5052
}
5153

54+
- (BOOL) isLocallyCached
55+
{
56+
NSFileManager* fs = [NSFileManager defaultManager];
57+
if (localFileName && [fs fileExistsAtPath:localFileName])
58+
{
59+
NSDate* mtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
60+
if ([mtime compare:localMtime] == 0)
61+
return YES;
62+
}
63+
return NO;
64+
}
65+
5266
- (NSString*) contents
5367
{
5468
if (!leaf)
5569
return [NSString stringWithFormat:@"This is a tree with path %@", self];
5670

57-
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
58-
NSData* data = [handle readDataToEndOfFile];
59-
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
60-
return string;
71+
NSData* data = nil;
72+
73+
if ([self isLocallyCached])
74+
data = [NSData dataWithContentsOfFile: localFileName];
75+
else {
76+
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
77+
data = [handle readDataToEndOfFile];
78+
}
79+
80+
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
6181
}
6282

83+
- (void) saveToFolder: (NSString *) dir
84+
{
85+
NSString* newName = [dir stringByAppendingPathComponent:path];
86+
87+
if (leaf) {
88+
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
89+
NSData* data = [handle readDataToEndOfFile];
90+
[data writeToFile:newName atomically:YES];
91+
} else { // Directory
92+
[[NSFileManager defaultManager] createDirectoryAtPath:newName attributes:nil];
93+
for (PBGitTree* child in children)
94+
[child saveToFolder: newName];
95+
}
96+
}
97+
98+
- (NSString*) tmpDirWithContents
99+
{
100+
if (leaf)
101+
return nil;
102+
103+
if (!localFileName)
104+
localFileName = [PBEasyFS tmpDirWithPrefix: path];
105+
106+
NSLog(@"Exporting children..");
107+
108+
for (PBGitTree* child in [self children]) {
109+
NSLog(@"Telling %@ to save to %@!", [child fullPath], localFileName);
110+
[child saveToFolder: localFileName];
111+
}
112+
113+
return localFileName;
114+
}
115+
116+
117+
63118
- (NSString*) tmpFileNameForContents
64119
{
65120
if (!leaf)
66-
return nil;
67-
NSLog(@"Getting tmp file");
121+
return [self tmpDirWithContents];
122+
123+
if ([self isLocallyCached])
124+
return localFileName;
125+
126+
if (!localFileName)
127+
localFileName = [PBEasyFS tmpNameWithSuffix: path];
128+
68129
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
69130
NSData* data = [handle readDataToEndOfFile];
70-
return [PBEasyPipe writeData:data toTempFileWithName:path];
131+
[data writeToFile:localFileName atomically:YES];
132+
133+
NSFileManager* fs = [NSFileManager defaultManager];
134+
localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
135+
136+
return localFileName;
71137
}
72138

73139
- (NSArray*) children
@@ -111,4 +177,10 @@ - (NSString*) fullPath
111177
return [parent.fullPath stringByAppendingPathComponent: self.path];
112178
}
113179

180+
- (void) finalize
181+
{
182+
if (localFileName)
183+
[[NSFileManager defaultManager] removeFileAtPath:localFileName handler:nil];
184+
[super finalize];
185+
}
114186
@end

0 commit comments

Comments
 (0)