File: FileSystem.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (251 lines) | stat: -rw-r--r-- 7,327 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//===-- FileSystem.cpp ----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "llbuild/Basic/FileSystem.h"
#include "llbuild/Basic/PlatformUtility.h"
#include "llbuild/Basic/Stat.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/MemoryBuffer.h"

#include <cassert>
#include <cstring>

// Cribbed from llvm, where it's been since removed.
namespace {
  using namespace std;
  using namespace llvm;
  using namespace llvm::sys::fs;

  static std::error_code fillStatus(int StatRet, const llbuild::basic::sys::StatStruct &Status,
                                    file_status &Result) {
    if (StatRet != 0) {
      std::error_code ec(errno, std::generic_category());
      if (ec == errc::no_such_file_or_directory)
        Result = file_status(file_type::file_not_found);
      else
        Result = file_status(file_type::status_error);
      return ec;
    }

    file_type Type = file_type::type_unknown;

    if (S_ISDIR(Status.st_mode))
      Type = file_type::directory_file;
    else if (S_ISREG(Status.st_mode))
      Type = file_type::regular_file;
    else if (S_ISBLK(Status.st_mode))
      Type = file_type::block_file;
    else if (S_ISCHR(Status.st_mode))
      Type = file_type::character_file;
    else if (S_ISFIFO(Status.st_mode))
      Type = file_type::fifo_file;
    else if (S_ISSOCK(Status.st_mode))
      Type = file_type::socket_file;
    else if (S_ISLNK(Status.st_mode))
      Type = file_type::symlink_file;

#if defined(_WIN32)
    Result = file_status(Type);
#else
    perms Perms = static_cast<perms>(Status.st_mode);
    Result =
      file_status(Type, Perms, Status.st_dev, Status.st_nlink, Status.st_ino, Status.st_atime, 0, Status.st_mtime, 0,
                Status.st_uid, Status.st_gid, Status.st_size);
#endif

    return std::error_code();
  }

  std::error_code link_status(const Twine &Path, file_status &Result) {
    SmallString<128> PathStorage;
    StringRef P = Path.toNullTerminatedStringRef(PathStorage);

    llbuild::basic::sys::StatStruct Status;
    int StatRet = llbuild::basic::sys::lstat(P.begin(), &Status);
    return fillStatus(StatRet, Status, Result);
  }

  error_code _remove_all_r(StringRef path, file_type ft, uint32_t &count) {
    if (ft == file_type::directory_file) {
      error_code ec;
      // For removal purposes, we should not follow through symlinks. Rather,
      // we just want to remove the symlink itself.
      directory_iterator i(path, ec, /* FollowSymlinks */ false);
      if (ec)
        return ec;

      for (directory_iterator e; i != e; i.increment(ec)) {
        if (ec)
          return ec;

        file_status st;

        if (error_code ec = link_status(i->path(), st))
          return ec;

        if (error_code ec = _remove_all_r(i->path(), st.type(), count))
          return ec;
      }

      if (error_code ec = remove(path, false))
        return ec;

      ++count; // Include the directory itself in the items removed.
    } else {
      if (error_code ec = remove(path, false))
        return ec;

      ++count;
    }

    return error_code();
  }
}

using namespace llbuild;
using namespace llbuild::basic;

FileSystem::~FileSystem() {}

bool FileSystem::createDirectories(const std::string& path) {
  // Attempt to create the final directory first, to optimize for the common
  // case where we don't need to recurse.
  if (createDirectory(path))
    return true;

  // If that failed, attempt to create the parent.
  StringRef parent = llvm::sys::path::parent_path(path);
  if (parent.empty())
    return false;
  return createDirectories(parent) && createDirectory(path);
}


std::unique_ptr<llvm::MemoryBuffer>
DeviceAgnosticFileSystem::getFileContents(const std::string& path) {
  return impl->getFileContents(path);
}

std::unique_ptr<llvm::MemoryBuffer>
ChecksumOnlyFileSystem::getFileContents(const std::string& path) {
  return impl->getFileContents(path);
}
namespace {

class LocalFileSystem : public FileSystem {
public:
  LocalFileSystem() {}

  virtual bool
  createDirectory(const std::string& path) override {
    if (!llbuild::basic::sys::mkdir(path.c_str())) {
      if (errno != EEXIST) {
        return false;
      }

      // Target exists, check that it is actually a directory
      llbuild::basic::sys::StatStruct statbuf;
      if (llbuild::basic::sys::lstat(path.c_str(), &statbuf) != 0) {
        return false;
      }
      if (!S_ISDIR(statbuf.st_mode)) {
        return false;
      }
    }
    return true;
  }

  virtual std::unique_ptr<llvm::MemoryBuffer>
  getFileContents(const std::string& path) override {
    auto result = llvm::MemoryBuffer::getFile(path);
    if (result.getError()) {
      return nullptr;
    }
    return std::unique_ptr<llvm::MemoryBuffer>(result->release());
  }

  bool rm_tree(const char* path) {
    uint32_t count = 0;
    return !_remove_all_r(path, file_type::directory_file, count);
  }

  virtual bool remove(const std::string& path) override {
    // Assume `path` is a regular file.
    if (llbuild::basic::sys::unlink(path.c_str()) == 0) {
      return true;
    }

#if defined(_WIN32)
    // Windows sets EACCES if the file is a directory
    if (errno != EACCES) {
      return false;
    }
#else
    // Error can't be that `path` is actually a directory (on Linux `EISDIR` will be returned since 2.1.132).
    if (errno != EPERM && errno != EISDIR) {
      return false;
    }
#endif

    // Check if `path` is a directory.
    llbuild::basic::sys::StatStruct statbuf;
    if (llbuild::basic::sys::lstat(path.c_str(), &statbuf) != 0) {
      return false;
    }

    if (S_ISDIR(statbuf.st_mode)) {
      if (llbuild::basic::sys::rmdir(path.c_str()) == 0) {
        return true;
      } else {
        return rm_tree(path.c_str());
      }
    }

    return false;
  }

  virtual FileChecksum getFileChecksum(const std::string& path) override {
    return FileChecksum::getChecksumForPath(path);
  }

  virtual FileInfo getFileInfo(const std::string& path) override {
    return FileInfo::getInfoForPath(path);
  }
  
  virtual FileInfo getLinkInfo(const std::string& path) override {
    return FileInfo::getInfoForPath(path, /*isLink:*/ true);
  }

  virtual bool createSymlink(const std::string& src,
                             const std::string& target) override {
    return (llbuild::basic::sys::symlink(src.c_str(), target.c_str()) == 0);
  }
};
  
}

std::unique_ptr<FileSystem> basic::createLocalFileSystem() {
  return llvm::make_unique<LocalFileSystem>();
}

std::unique_ptr<FileSystem>
basic::DeviceAgnosticFileSystem::from(std::unique_ptr<FileSystem> fs) {
  return llvm::make_unique<DeviceAgnosticFileSystem>(std::move(fs));
}

std::unique_ptr<FileSystem>
basic::ChecksumOnlyFileSystem::from(std::unique_ptr<FileSystem> fs) {
  return llvm::make_unique<ChecksumOnlyFileSystem>(std::move(fs));
}