File: XADPlatformWindows.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (212 lines) | stat: -rw-r--r-- 5,641 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#import "XADPlatform.h"
#import "NSDateXAD.h"

#import <windows.h>
#import <sys/stat.h>




// TODO: Implement proper handling of Windows metadata.

@implementation XADPlatform

//
// Archive entry extraction.
//

+(XADError)extractResourceForkEntryWithDictionary:(NSDictionary *)dict
unarchiver:(XADUnarchiver *)unarchiver toPath:(NSString *)destpath
{
	return XADNotSupportedError;
}

+(XADError)updateFileAttributesAtPath:(NSString *)path
forEntryWithDictionary:(NSDictionary *)dict parser:(XADArchiveParser *)parser
preservePermissions:(BOOL)preservepermissions
{
	const wchar_t *wpath=[path fileSystemRepresentationW];

	// If the file is read-only, change this temporarily and remember to change back.
	BOOL changedattributes=NO;
	DWORD oldattributes=GetFileAttributesW(wpath);
	if(oldattributes!=INVALID_FILE_ATTRIBUTES&&(oldattributes&FILE_ATTRIBUTE_READONLY))
	{
		SetFileAttributesW(wpath,oldattributes&~INVALID_FILE_ATTRIBUTES);
		changedattributes=YES;
	}

	NSDate *modification=[dict objectForKey:XADLastModificationDateKey];
	NSDate *creation=[dict objectForKey:XADCreationDateKey];
	NSDate *access=[dict objectForKey:XADLastAccessDateKey];

	if(modification||creation||access)
	{
		HANDLE handle=CreateFileW(wpath,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
		if(handle==INVALID_HANDLE_VALUE) return XADUnknownError; // TODO: better error

		FILETIME creationtime,lastaccesstime,lastwritetime;

		if(creation) creationtime=[creation FILETIME];
		if(access) lastaccesstime=[access FILETIME];
		if(modification) lastwritetime=[modification FILETIME];

		if(!SetFileTime(handle,
		creation?&creationtime:NULL,
		access?&lastaccesstime:NULL,
		modification?&lastwritetime:NULL))
		{
			CloseHandle(handle);
			return XADUnknownError; // TODO: better error
		}

		CloseHandle(handle);
	}

	NSNumber *attributes=[dict objectForKey:XADWindowsFileAttributesKey];
	if(!attributes) attributes=[dict objectForKey:XADDOSFileAttributesKey];
	if(attributes||changedattributes)
	{
		DWORD newattributes=oldattributes;
		if(attributes) newattributes=[attributes intValue];
		SetFileAttributesW(wpath,newattributes);
	}

	return XADNoError;
}

+(XADError)createLinkAtPath:(NSString *)path withDestinationPath:(NSString *)link
{
	return XADNotSupportedError;
}




//
// Archive post-processing.
//

+(id)readCloneableMetadataFromPath:(NSString *)path { return nil; }
+(void)writeCloneableMetadata:(id)metadata toPath:(NSString *)path {}

+(BOOL)copyDateFromPath:(NSString *)src toPath:(NSString *)dest
{
	const wchar_t *wsrc=[src fileSystemRepresentationW];
    HANDLE srchandle=CreateFileW(wsrc,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE, 
	NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
	if(srchandle==INVALID_HANDLE_VALUE) return NO;

	FILETIME time;
	if(!GetFileTime(srchandle,NULL,NULL,&time)) { CloseHandle(srchandle); return NO; }

	CloseHandle(srchandle);

	const wchar_t *wdest=[dest fileSystemRepresentationW];
	HANDLE desthandle=CreateFileW(wdest,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
	NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
	if(desthandle==INVALID_HANDLE_VALUE) return NO;

	if(!SetFileTime(desthandle,NULL,NULL,&time)) { CloseHandle(desthandle); return NO; }

	CloseHandle(desthandle);

	return YES;
}

+(BOOL)resetDateAtPath:(NSString *)path
{
	SYSTEMTIME now;
	GetSystemTime(&now);

	FILETIME time;
	if(!SystemTimeToFileTime(&now,&time)) return NO;

	const wchar_t *wpath=[path fileSystemRepresentationW];
	HANDLE handle=CreateFileW(wpath,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
	NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
	if(handle==INVALID_HANDLE_VALUE) return NO;

	if(!SetFileTime(handle,NULL,NULL,&time)) { CloseHandle(handle); return NO; }

	CloseHandle(handle);

	return YES;
}




//
// Path functions.
//

+(NSString *)uniqueDirectoryPathWithParentDirectory:(NSString *)parent
{
	NSDate *now=[NSDate date];
	int64_t t=[now timeIntervalSinceReferenceDate]*1000000000;

	NSString *dirname=[NSString stringWithFormat:@"XADTemp%qd",t];

	if(parent) return [parent stringByAppendingPathComponent:dirname];
	else return dirname;
}

+(NSString *)sanitizedPathComponent:(NSString *)component
{
	static NSCharacterSet *charset=nil;
	if(!charset) charset=[[NSCharacterSet characterSetWithCharactersInString:
	@"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
	@"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
	@"\"*:<>?\\/|\000"] retain];

	static XADRegex *regex1=nil;
	if(!regex1) regex1=[[XADRegex regexWithPattern:
	@"^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$" options:REG_ICASE] retain];

	static XADRegex *regex2=nil;
	if(!regex2) regex2=[[XADRegex regexWithPattern:
	@"^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\\..*)$" options:REG_ICASE] retain];

	if([component rangeOfCharacterFromSet:charset].location!=NSNotFound)
	{
		NSMutableString *newstring=[NSMutableString stringWithString:component];
		int length=[newstring length];
		for(int i=0;i<length;i++)
		{
			unichar c=[newstring characterAtIndex:i];
			if([charset characterIsMember:c])
			[newstring replaceCharactersInRange:NSMakeRange(i,1) withString:@"_"];
		}
		component=newstring;
	}

	if([regex1 matchesString:component])
	{
		return [component stringByAppendingString:@"_"];
	}

	if([regex2 matchesString:component])
	{
		NSArray *matches=[regex2 capturedSubstringsOfString:component];
		return [NSString stringWithFormat:@"%@_%@",
		[matches objectAtIndex:1],[matches objectAtIndex:2]];
	}

	return component;
}




//
// Time functions.
//

+(double)currentTimeInSeconds
{
	return (double)timeGetTime()/1000.0;
}

@end