Skip to content

Commit 4272bf9

Browse files
committed
Use FileManager API to get current working directory
- FileManager handles long file name prefixes on Windows correctly. This keeps any UNC long file prefix (\\?\) out of URL that were relative and then resolved to absolute urls.
1 parent bcf2cca commit 4272bf9

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

Sources/CoreFoundation/CFPlatform.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#define SECURITY_WIN32
4343
#include <Security.h>
4444

45-
#define getcwd _NS_getcwd
4645
#define open _NS_open
4746

4847
#endif
@@ -69,11 +68,6 @@ char **_CFArgv(void) { return *_NSGetArgv(); }
6968
int _CFArgc(void) { return *_NSGetArgc(); }
7069
#endif
7170

72-
73-
CF_PRIVATE Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
74-
return getcwd(path, maxlen) != NULL;
75-
}
76-
7771
#if TARGET_OS_WIN32
7872
// Returns the path to the CF DLL, which we can then use to find resources like char sets
7973
bool bDllPathCached = false;
@@ -1131,24 +1125,6 @@ CF_EXPORT int _NS_unlink(const char *name) {
11311125
return res;
11321126
}
11331127

1134-
// Warning: this doesn't support dstbuf as null even though 'getcwd' does
1135-
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size) {
1136-
if (!dstbuf) {
1137-
CFLog(kCFLogLevelWarning, CFSTR("CFPlatform: getcwd called with null buffer"));
1138-
return 0;
1139-
}
1140-
1141-
wchar_t *buf = _wgetcwd(NULL, 0);
1142-
if (!buf) {
1143-
return NULL;
1144-
}
1145-
1146-
// Convert result to UTF8
1147-
copyToNarrowFileSystemRepresentation(buf, (CFIndex)size, dstbuf);
1148-
free(buf);
1149-
return dstbuf;
1150-
}
1151-
11521128
CF_EXPORT char *_NS_getenv(const char *name) {
11531129
// todo: wide getenv
11541130
// We have to be careful what happens here, because getenv is called during cf initialization, and things like cfstring may not be working yet

Sources/CoreFoundation/include/ForFoundationOnly.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ CF_EXPORT int _NS_mkdir(const char *name);
527527
CF_EXPORT int _NS_rmdir(const char *name);
528528
CF_EXPORT int _NS_chmod(const char *name, int mode);
529529
CF_EXPORT int _NS_unlink(const char *name);
530-
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
531530
CF_EXPORT char *_NS_getenv(const char *name);
532531
CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
533532
CF_EXPORT int _NS_open(const char *name, int oflag, int pmode);

Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ CF_EXPORT int _NS_mkdir(const char *name);
374374
CF_EXPORT int _NS_rmdir(const char *name);
375375
CF_EXPORT int _NS_chmod(const char *name, int mode);
376376
CF_EXPORT int _NS_unlink(const char *name);
377-
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
378377
CF_EXPORT char *_NS_getenv(const char *name);
379378
CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
380379
CF_EXPORT int _NS_open(const char *name, int oflag, int pmode);

Sources/Foundation/FileManager.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,28 @@ extension FileManager {
681681
}
682682
}
683683
}
684+
685+
// USed by NSURL
686+
@_cdecl("_CFGetCurrentDirectory")
687+
internal func _CFGetCurrentDirectory(_ buffer: UnsafeMutablePointer<CChar>?, _ size: Int) -> Bool {
688+
guard let buffer = buffer, size > 0 else {
689+
return false // Invalid parameters
690+
}
691+
692+
let currentPath = FileManager.default.currentDirectoryPath
693+
694+
// Convert to C string representation
695+
let cString = currentPath.utf8CString
696+
let requiredSize = cString.count // includes null terminator
697+
698+
if requiredSize > size {
699+
return false // Buffer too small
700+
}
701+
702+
// Copy the string to the buffer
703+
cString.withUnsafeBufferPointer { sourceBuffer in
704+
buffer.initialize(from: sourceBuffer.baseAddress!, count: requiredSize)
705+
}
706+
707+
return true // Success
708+
}

0 commit comments

Comments
 (0)