Oolite
Loading...
Searching...
No Matches
NSFileManagerOOExtensions.m
Go to the documentation of this file.
1/*
2
3NSFileManagerOOExtensions.m
4
5This extends NSFileManager and adds some methods to insulate the
6main oolite code from the gory details of creating/chdiring to the
7commander save directory.
8
9Oolite
10Copyright (C) 2004-2013 Giles C Williams and contributors
11
12This program is free software; you can redistribute it and/or
13modify it under the terms of the GNU General Public License
14as published by the Free Software Foundation; either version 2
15of the License, or (at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program; if not, write to the Free Software
24Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25MA 02110-1301, USA.
26
27*/
28
30#import "GameController.h"
31#import "OOPListParsing.h"
32#import "ResourceManager.h"
33#import "unzip.h"
34#include <SDL3/SDL_stdinc.h>
35#include <stdlib.h>
36
37@implementation NSFileManager (OOExtensions)
38
39- (NSArray*)commanderContentsOfPath:(NSString*)savePath
40{
41 BOOL pathIsDirectory = NO;
42 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath isDirectory:&pathIsDirectory] && pathIsDirectory) {
43 NSMutableArray* contents = [NSMutableArray arrayWithArray:[self oo_directoryContentsAtPath:savePath]];
44
45 // at this point we should strip out any files not loadable as Oolite saved games
46 unsigned i;
47 for (i = 0; i < [contents count]; i++) {
48 NSString* path = [savePath stringByAppendingPathComponent:(NSString*)[contents objectAtIndex:i]];
49
50 // ensure it's not a directory
51 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&pathIsDirectory] && pathIsDirectory) {
52
53 // check file extension
54 if (![[path pathExtension] isEqual:@"oolite-save"]) {
55 [contents removeObjectAtIndex:i--];
56 continue;
57 }
58
59 // check to see if we can parse the file okay
60 NSDictionary* cdr = OODictionaryFromFile(path);
61 if (!cdr) {
62 OOLog(@"savedGame.read.fail.notDictionary", @">>>> %@ could not be parsed as a saved game.", path);
63 [contents removeObjectAtIndex:i--];
64 continue;
65 }
66 }
67
68 // all okay - we can use this path!
69 [contents replaceObjectAtIndex:i withObject:path];
70 }
71
72 return contents;
73 } else {
74 OOLogERR(@"savedGame.read.fail.fileNotFound", @"File at path '%@' could not be found.", savePath);
75 return nil;
76 }
77}
78
80{
81 const char* savedirEnv = SDL_getenv("OO_SAVEDIR");
82 NSString* savedir;
83
84 if (savedirEnv) {
85 savedir = [NSString stringWithUTF8String:savedirEnv];
86 } else {
87 savedir = [NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR];
88 }
89 BOOL pathIsDirectory = NO;
90
91 // does it exist?
92 if (![[NSFileManager defaultManager] fileExistsAtPath:savedir isDirectory:&pathIsDirectory]) {
93 // it doesn't exist.
94 if ([self oo_createDirectoryAtPath:savedir attributes:nil]) {
95 return savedir;
96 } else {
97 OOLogERR(@"savedGame.defaultPath.create.failed", @"Unable to create '%@'. Saved games will go to the home directory.", savedir);
98 return NSHomeDirectory();
99 }
100 }
101
102 // is it a directory?
103 if (!pathIsDirectory) {
104 OOLogERR(@"savedGame.defaultPath.notDirectory", @"'%@' is not a directory, saved games will go to the home directory.", savedir);
105 return NSHomeDirectory();
106 }
107
108 return savedir;
109}
110
111#if OOLITE_MAC_OS_X
112
113- (NSArray*)oo_directoryContentsAtPath:(NSString*)path
114{
115 return [self contentsOfDirectoryAtPath:path error:NULL];
116}
117
118- (BOOL)oo_createDirectoryAtPath:(NSString*)path attributes:(NSDictionary*)attributes
119{
120 return [self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
121}
122
123- (NSDictionary*)oo_fileAttributesAtPath:(NSString*)path traverseLink:(BOOL)traverseLink
124{
125 if (traverseLink) {
126 NSString* linkDest = nil;
127 do {
128 linkDest = [self destinationOfSymbolicLinkAtPath:path error:NULL];
129 if (linkDest != nil)
130 path = linkDest;
131 } while (linkDest != nil);
132 }
133
134 return [self attributesOfItemAtPath:path error:NULL];
135}
136
137- (NSDictionary*)oo_fileSystemAttributesAtPath:(NSString*)path
138{
139 return [self attributesOfFileSystemForPath:path error:NULL];
140}
141
142- (BOOL)oo_removeItemAtPath:(NSString*)path
143{
144 return [self removeItemAtPath:path error:NULL];
145}
146
147- (BOOL)oo_moveItemAtPath:(NSString*)src toPath:(NSString*)dest
148{
149 return [self moveItemAtPath:src toPath:dest error:NULL];
150}
151
152#else
153
154- (NSArray*)oo_directoryContentsAtPath:(NSString*)path
155{
156 return [self directoryContentsAtPath:path];
157}
158
159- (BOOL)oo_createDirectoryAtPath:(NSString*)path attributes:(NSDictionary*)attributes
160{
161 return [self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
162}
163
164- (NSDictionary*)oo_fileAttributesAtPath:(NSString*)path traverseLink:(BOOL)yorn
165{
166 return [self fileAttributesAtPath:path traverseLink:yorn];
167}
168
169- (NSDictionary*)oo_fileSystemAttributesAtPath:(NSString*)path
170{
171 return [self fileSystemAttributesAtPath:path];
172}
173
174- (BOOL)oo_removeItemAtPath:(NSString*)path
175{
176 return [self removeFileAtPath:path handler:nil];
177}
178
179- (BOOL)oo_moveItemAtPath:(NSString*)src toPath:(NSString*)dest
180{
181 return [self movePath:src toPath:dest handler:nil];
182}
183
184#endif
185
186#if OOLITE_SDL
187- (BOOL)chdirToSnapshotPath
188{
189 const char* snapshotEnv = SDL_getenv("OO_SNAPSHOTSDIR");
190 NSString* savedir;
191
192 if (snapshotEnv) {
193 savedir = [NSString stringWithUTF8String:snapshotEnv];
194 } else {
195 // SDL: the default path for snapshots is oolite.app/oolite-saves/snapshots
196 savedir = [[NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
197 }
198
199 if (![self changeCurrentDirectoryPath:savedir]) {
200 // it probably doesn't exist.
201 if (![self oo_createDirectoryAtPath:savedir attributes:nil]) {
202 OOLog(@"savedSnapshot.defaultPath.create.failed", @"Unable to create directory %@", savedir);
203 return NO;
204 }
205 if (![self changeCurrentDirectoryPath:savedir]) {
206 OOLog(@"savedSnapshot.defaultPath.chdir.failed", @"Created %@ but couldn't make it the current directory.", savedir);
207 return NO;
208 }
209 }
210
211 return YES;
212}
213#endif
214
215- (BOOL)oo_oxzFileExistsAtPath:(NSString*)path
216{
217 NSUInteger i, cl;
218 NSArray* components = [path pathComponents];
219 cl = [components count];
220 for (i = 0; i < cl; i++) {
221 NSString* component = [components objectAtIndex:i];
222 if ([[[component pathExtension] lowercaseString] isEqualToString:@"oxz"]) {
223 break;
224 }
225 }
226 // if i == cl then the path is entirely uncompressed
227 if (i == cl) {
228 BOOL directory = NO;
229 BOOL result = [self fileExistsAtPath:path isDirectory:&directory];
230 if (directory) {
231 return NO;
232 }
233 return result;
234 }
235
236 NSRange range;
237 range.location = 0;
238 range.length = i + 1;
239 NSString* zipFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
240 range.location = i + 1;
241 range.length = cl - (i + 1);
242 NSString* containedFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
243
244 unzFile uf = NULL;
245 const char* zipname = [zipFile cStringUsingEncoding:NSUTF8StringEncoding];
246 if (zipname != NULL) {
247 uf = unzOpen64(zipname);
248 }
249 if (uf == NULL) {
250 // no such zip file
251 return NO;
252 }
253 const char* filename = [containedFile cStringUsingEncoding:NSUTF8StringEncoding];
254 // unzLocateFile(*, *, 1) = case-sensitive extract
255 BOOL result = YES;
256 if (unzLocateFile(uf, filename, 1) != UNZ_OK) {
257 result = NO;
258 } else {
259 int err = UNZ_OK;
260 unz_file_info64 file_info = { 0 };
261 err = unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
262 if (err != UNZ_OK) {
263 result = NO;
264 } else {
265 }
266 }
267 unzClose(uf);
268 return result;
269}
270
271@end
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSDictionary * OODictionaryFromFile(NSString *path)
return nil
NSArray * oo_directoryContentsAtPath:(NSString *path)
const char * filename
Definition ioapi.h:125
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
Definition unzip.c:1161
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition unzip.c:1058
unzFile ZEXPORT unzOpen64(const void *path)
Definition unzip.c:758
int ZEXPORT unzClose(unzFile file)
Definition unzip.c:768
struct unz_file_info64_s unz_file_info64
voidp unzFile
Definition unzip.h:72
#define UNZ_OK
Definition unzip.h:75