Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit c46a2dd

Browse files
committed
Ensure directories are always listed in deterministic order
macOS APIs have recently changed and do not return sorted entries for directories.
1 parent 71e9720 commit c46a2dd

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

GCDWebDAVServer/GCDWebDAVServer.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ - (GCDWebServerResponse*)performPROPFIND:(GCDWebServerDataRequest*)request {
546546
NSArray* items = nil;
547547
if (isDirectory) {
548548
NSError* error = nil;
549-
items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:absolutePath error:&error];
549+
items = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:absolutePath error:&error] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
550550
if (items == nil) {
551551
return [GCDWebServerErrorResponse responseWithServerError:kGCDWebServerHTTPStatusCode_InternalServerError underlyingError:error message:@"Failed listing directory \"%@\"", relativePath];
552552
}

GCDWebServer/Core/GCDWebServer.m

+10-10
Original file line numberDiff line numberDiff line change
@@ -981,29 +981,29 @@ - (void)addGETHandlerForPath:(NSString*)path filePath:(NSString*)filePath isAtta
981981
}
982982

983983
- (GCDWebServerResponse*)_responseWithContentsOfDirectory:(NSString*)path {
984-
NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
985-
if (enumerator == nil) {
984+
NSArray* contents = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
985+
if (contents == nil) {
986986
return nil;
987987
}
988988
NSMutableString* html = [NSMutableString string];
989989
[html appendString:@"<!DOCTYPE html>\n"];
990990
[html appendString:@"<html><head><meta charset=\"utf-8\"></head><body>\n"];
991991
[html appendString:@"<ul>\n"];
992-
for (NSString* file in enumerator) {
993-
if (![file hasPrefix:@"."]) {
994-
NSString* type = [[enumerator fileAttributes] objectForKey:NSFileType];
992+
for (NSString* entry in contents) {
993+
if (![entry hasPrefix:@"."]) {
994+
NSString* type = [[[NSFileManager defaultManager] attributesOfItemAtPath:[path stringByAppendingPathComponent:entry] error:NULL] objectForKey:NSFileType];
995+
GWS_DCHECK(type);
995996
#pragma clang diagnostic push
996997
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
997-
NSString* escapedFile = [file stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
998+
NSString* escapedFile = [entry stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
998999
#pragma clang diagnostic pop
9991000
GWS_DCHECK(escapedFile);
10001001
if ([type isEqualToString:NSFileTypeRegular]) {
1001-
[html appendFormat:@"<li><a href=\"%@\">%@</a></li>\n", escapedFile, file];
1002+
[html appendFormat:@"<li><a href=\"%@\">%@</a></li>\n", escapedFile, entry];
10021003
} else if ([type isEqualToString:NSFileTypeDirectory]) {
1003-
[html appendFormat:@"<li><a href=\"%@/\">%@/</a></li>\n", escapedFile, file];
1004+
[html appendFormat:@"<li><a href=\"%@/\">%@/</a></li>\n", escapedFile, entry];
10041005
}
10051006
}
1006-
[enumerator skipDescendents];
10071007
}
10081008
[html appendString:@"</ul>\n"];
10091009
[html appendString:@"</body></html>\n"];
@@ -1176,7 +1176,7 @@ - (NSInteger)runTestsWithOptions:(NSDictionary<NSString*, id>*)options inDirecto
11761176
_ExecuteMainThreadRunLoopSources();
11771177

11781178
result = 0;
1179-
NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
1179+
NSArray* files = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
11801180
for (NSString* requestFile in files) {
11811181
if (![requestFile hasSuffix:@".request"]) {
11821182
continue;

GCDWebUploader/GCDWebUploader.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ - (GCDWebServerResponse*)listDirectory:(GCDWebServerRequest*)request {
239239
}
240240

241241
NSError* error = nil;
242-
NSArray* contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:absolutePath error:&error];
242+
NSArray* contents = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:absolutePath error:&error] sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
243243
if (contents == nil) {
244244
return [GCDWebServerErrorResponse responseWithServerError:kGCDWebServerHTTPStatusCode_InternalServerError underlyingError:error message:@"Failed listing directory \"%@\"", relativePath];
245245
}

0 commit comments

Comments
 (0)