Skip to content

Commit c5cfe59

Browse files
authored
Merge pull request swiftlang#183 from hughbe/stat-fixes
Fix Windows build with posix specific stat APIs
2 parents ebda2f0 + 436c4d8 commit c5cfe59

File tree

6 files changed

+91
-15
lines changed

6 files changed

+91
-15
lines changed

include/llbuild/Basic/PlatformUtility.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,19 @@
1919
#define LLBUILD_BASIC_PLATFORMUTILITY_H
2020

2121
#include <cstdio>
22-
#include <sys/stat.h>
2322

2423
namespace llbuild {
2524
namespace basic {
2625
namespace sys {
27-
#if defined(_WIN32)
28-
using StatStruct = struct ::_stat;
29-
#else
30-
using StatStruct = struct ::stat;
31-
#endif
3226

3327
bool chdir(const char *fileName);
3428
int close(int fileHandle);
35-
int lstat(const char *fileName, StatStruct *buf);
3629
bool mkdir(const char *fileName);
3730
int pclose(FILE *stream);
3831
int pipe(int ptHandles[2]);
3932
FILE *popen(const char *command, const char *mode);
4033
int read(int fileHandle, void *destinationBuffer, unsigned int maxCharCount);
4134
int rmdir(const char *path);
42-
int stat(const char *fileName, StatStruct *buf);
4335
int symlink(const char *source, const char *target);
4436
int unlink(const char *fileName);
4537
int write(int fileHandle, void *destinationBuffer, unsigned int maxCharCount);

include/llbuild/Basic/Stat.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===- Stat.h -------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLBUILD_BASIC_STAT_H
14+
#define LLBUILD_BASIC_STAT_H
15+
16+
#include <sys/stat.h>
17+
18+
#include "llvm/Support/FileSystem.h"
19+
20+
namespace llbuild {
21+
namespace basic {
22+
namespace sys {
23+
24+
#if !defined(S_IFBLK)
25+
#define S_IFBLK 0060000
26+
#endif
27+
#if !defined(S_IFIFO)
28+
#define S_IFIFO 0010000
29+
#endif
30+
#if !defined(S_IFSOCK)
31+
#define S_IFSOCK 00140000
32+
#endif
33+
#if !defined(S_IFLNK)
34+
#define S_IFLNK 0120000
35+
#endif
36+
37+
#if !defined(S_ISREG)
38+
#define S_ISREG(mode) ((mode) & _S_IFMT) == S_IFREG
39+
#endif
40+
41+
#if !defined(S_ISDIR)
42+
#define S_ISDIR(mode) ((mode) & _S_IFMT) == S_IFDIR
43+
#endif
44+
45+
#if !defined(S_ISBLK)
46+
#define S_ISBLK(mode) ((mode) & _S_IFMT) == S_IFBLK
47+
#endif
48+
49+
#if !defined(S_ISCHR)
50+
#define S_ISCHR(mode) ((mode) & _S_IFMT) == S_IFCHR
51+
#endif
52+
53+
#if !defined(S_ISFIFO)
54+
#define S_ISFIFO(mode) ((mode) & _S_IFMT) == S_IFIFO
55+
#endif
56+
57+
#if !defined(S_ISSOCK)
58+
#define S_ISSOCK(mode) ((mode) & _S_IFMT) == S_IFSOCK
59+
#endif
60+
61+
#if !defined(S_ISLNK)
62+
#define S_ISLNK(mode) ((mode) & _S_IFMT) == S_IFLNK
63+
#endif
64+
65+
#if defined(_WIN32)
66+
using StatStruct = struct ::_stat;
67+
#else
68+
using StatStruct = struct ::stat;
69+
#endif
70+
71+
int lstat(const char *fileName, StatStruct *buf);
72+
int stat(const char *fileName, StatStruct *buf);
73+
}
74+
}
75+
}
76+
77+
#endif // LLBUILD_BASIC_STAT_H

lib/Basic/FileInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "llbuild/Basic/FileInfo.h"
1414

15-
#include "llbuild/Basic/PlatformUtility.h"
15+
#include "llbuild/Basic/Stat.h"
1616

1717
#include <cassert>
1818
#include <cstring>

lib/Basic/FileSystem.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llbuild/Basic/FileSystem.h"
1414
#include "llbuild/Basic/PlatformUtility.h"
15+
#include "llbuild/Basic/Stat.h"
1516

1617
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/Support/FileSystem.h"
@@ -27,7 +28,7 @@ namespace {
2728
using namespace llvm;
2829
using namespace llvm::sys::fs;
2930

30-
static std::error_code fillStatus(int StatRet, const struct stat &Status,
31+
static std::error_code fillStatus(int StatRet, const llbuild::basic::sys::StatStruct &Status,
3132
file_status &Result) {
3233
if (StatRet != 0) {
3334
std::error_code ec(errno, std::generic_category());
@@ -55,10 +56,14 @@ namespace {
5556
else if (S_ISLNK(Status.st_mode))
5657
Type = file_type::symlink_file;
5758

59+
#if defined(_WIN32)
60+
Result = file_status(Type);
61+
#else
5862
perms Perms = static_cast<perms>(Status.st_mode);
5963
Result =
6064
file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime,
6165
Status.st_uid, Status.st_gid, Status.st_size);
66+
#endif
6267

6368
return std::error_code();
6469
}
@@ -67,8 +72,8 @@ namespace {
6772
SmallString<128> PathStorage;
6873
StringRef P = Path.toNullTerminatedStringRef(PathStorage);
6974

70-
struct stat Status;
71-
int StatRet = ::lstat(P.begin(), &Status);
75+
llbuild::basic::sys::StatStruct Status;
76+
int StatRet = llbuild::basic::sys::lstat(P.begin(), &Status);
7277
return fillStatus(StatRet, Status, Result);
7378
}
7479

@@ -167,7 +172,7 @@ class LocalFileSystem : public FileSystem {
167172
}
168173

169174
// Check if `path` is a directory.
170-
struct stat statbuf;
175+
llbuild::basic::sys::StatStruct statbuf;
171176
if (llbuild::basic::sys::lstat(path.c_str(), &statbuf) != 0) {
172177
return false;
173178
}

lib/Basic/PlatformUtility.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "llbuild/Basic/PlatformUtility.h"
11+
#include "llbuild/Basic/Stat.h"
1112

1213
#if defined(_WIN32)
1314
#include "LeanWindows.h"

unittests/Basic/FileSystemTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llbuild/Basic/FileSystem.h"
1616
#include "llbuild/Basic/LLVM.h"
1717
#include "llbuild/Basic/PlatformUtility.h"
18+
#include "llbuild/Basic/Stat.h"
1819

1920
#include "llvm/Support/FileSystem.h"
2021
#include "llvm/Support/MemoryBuffer.h"
@@ -94,7 +95,7 @@ TEST(FileSystemTest, testRecursiveRemoval) {
9495
bool result = fs->remove(tempDir.c_str());
9596
EXPECT_TRUE(result);
9697

97-
struct stat statbuf;
98+
sys::StatStruct statbuf;
9899
EXPECT_EQ(-1, sys::stat(tempDir.c_str(), &statbuf));
99100
EXPECT_EQ(ENOENT, errno);
100101
}
@@ -144,7 +145,7 @@ TEST(FileSystemTest, testRecursiveRemovalDoesNotFollowSymlinks) {
144145
bool result = fs->remove(tempDir.c_str());
145146
EXPECT_TRUE(result);
146147

147-
struct stat statbuf;
148+
sys::StatStruct statbuf;
148149
EXPECT_EQ(-1, sys::stat(tempDir.c_str(), &statbuf));
149150
EXPECT_EQ(ENOENT, errno);
150151
// Verify that the symlink target still exists.

0 commit comments

Comments
 (0)