aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/cpp/runfiles/runfiles.cc
blob: 9ecc033a046fb338aa82465d68443ae7cc85395f (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tools/cpp/runfiles/runfiles.h"

#ifdef COMPILER_MSVC
#include <windows.h>
#else  // not COMPILER_MSVC
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif  // COMPILER_MSVC

#include <fstream>
#include <map>
#include <sstream>
#include <vector>

#ifdef COMPILER_MSVC
#include <memory>
#endif  // COMPILER_MSVC

namespace bazel {
namespace tools {
namespace cpp {
namespace runfiles {

using std::function;
using std::map;
using std::pair;
using std::string;
using std::vector;

namespace {

class RunfilesImpl : public Runfiles {
 public:
  static Runfiles* Create(const string& argv0,
                          function<string(const string&)> env_lookup,
                          string* error);

  string Rlocation(const string& path) const override;

  // Returns the runtime-location of a given runfile.
  //
  // This method assumes that the caller already validated the `path`. See
  // Runfiles::Rlocation for requirements.
  virtual string RlocationChecked(const string& path) const = 0;

 protected:
  RunfilesImpl() {}
  virtual ~RunfilesImpl() {}
};

// Runfiles implementation that parses a runfiles-manifest to look up runfiles.
class ManifestBased : public RunfilesImpl {
 public:
  // Returns a new `ManifestBased` instance.
  // Reads the file at `manifest_path` to build a map of the runfiles.
  // Returns nullptr upon failure.
  static ManifestBased* Create(const string& manifest_path, string* error);

  vector<pair<string, string> > EnvVars() const override;
  string RlocationChecked(const string& path) const override;

 private:
  ManifestBased(const string& manifest_path, map<string, string>&& runfiles_map)
      : manifest_path_(manifest_path), runfiles_map_(runfiles_map) {}

  ManifestBased(const ManifestBased&) = delete;
  ManifestBased(ManifestBased&&) = delete;
  ManifestBased& operator=(const ManifestBased&) = delete;
  ManifestBased& operator=(ManifestBased&&) = delete;

  string RunfilesDir() const;
  static bool ParseManifest(const string& path, map<string, string>* result,
                            string* error);

  const string manifest_path_;
  const map<string, string> runfiles_map_;
};

// Runfiles implementation that appends runfiles paths to the runfiles root.
class DirectoryBased : public RunfilesImpl {
 public:
  DirectoryBased(string runfiles_path)
      : runfiles_path_(std::move(runfiles_path)) {}
  vector<pair<string, string> > EnvVars() const override;
  string RlocationChecked(const string& path) const override;

 private:
  DirectoryBased(const DirectoryBased&) = delete;
  DirectoryBased(DirectoryBased&&) = delete;
  DirectoryBased& operator=(const DirectoryBased&) = delete;
  DirectoryBased& operator=(DirectoryBased&&) = delete;

  const string runfiles_path_;
};

bool IsReadableFile(const string& path) {
  return std::ifstream(path).is_open();
}

bool IsDirectory(const string& path) {
#ifdef COMPILER_MSVC
  DWORD attrs = GetFileAttributesA(path.c_str());
  return (attrs != INVALID_FILE_ATTRIBUTES) &&
         (attrs & FILE_ATTRIBUTE_DIRECTORY);
#else
  struct stat buf;
  return stat(path.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode);
#endif
}

Runfiles* RunfilesImpl::Create(const string& argv0,
                               function<string(const string&)> env_lookup,
                               string* error) {
  string manifest(std::move(env_lookup("RUNFILES_MANIFEST_FILE")));
  if (!manifest.empty()) {
    return ManifestBased::Create(manifest, error);
  }

  string directory(std::move(env_lookup("RUNFILES_DIR")));
  if (!directory.empty()) {
    return new DirectoryBased(directory);
  }

  manifest = argv0 + ".runfiles_manifest";
  if (IsReadableFile(manifest)) {
    return CreateManifestBased(manifest, error);
  }

  manifest = argv0 + ".runfiles/MANIFEST";
  if (IsReadableFile(manifest)) {
    return CreateManifestBased(manifest, error);
  }

  directory = argv0 + ".runfiles";
  if (IsDirectory(directory)) {
    return CreateDirectoryBased(std::move(directory), error);
  }

  if (error) {
    std::ostringstream err;
    err << "ERROR: " << __FILE__ << "(" << __LINE__
        << "): cannot find runfiles (argv0=\"" << argv0 << "\")";
    *error = err.str();
  }
  return nullptr;
}

bool IsAbsolute(const string& path) {
  if (path.empty()) {
    return false;
  }
  char c = path.front();
  return (c == '/') || (path.size() >= 3 &&
                        ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) &&
                        path[1] == ':' && (path[2] == '\\' || path[2] == '/'));
}

string GetEnv(const string& key) {
#ifdef COMPILER_MSVC
  DWORD size = ::GetEnvironmentVariableA(key.c_str(), NULL, 0);
  if (size == 0) {
    return std::move(string());  // unset or empty envvar
  }
  std::unique_ptr<char[]> value(new char[size]);
  ::GetEnvironmentVariableA(key.c_str(), value.get(), size);
  return move(string(value.get()));
#else
  char* result = getenv(key.c_str());
  return std::move((result == NULL) ? string() : string(result));
#endif
}

string RunfilesImpl::Rlocation(const string& path) const {
  if (path.empty() || path.find("..") != string::npos) {
    return std::move(string());
  }
  if (IsAbsolute(path)) {
    return path;
  }
  return RlocationChecked(path);
}

ManifestBased* ManifestBased::Create(const string& manifest_path,
                                     string* error) {
  map<string, string> runfiles;
  return ParseManifest(manifest_path, &runfiles, error)
             ? new ManifestBased(manifest_path, std::move(runfiles))
             : nullptr;
}

string ManifestBased::RlocationChecked(const string& path) const {
  const auto value = runfiles_map_.find(path);
  return std::move(value == runfiles_map_.end() ? string() : value->second);
}

vector<pair<string, string> > ManifestBased::EnvVars() const {
  return std::move(vector<pair<string, string> >(
      {std::make_pair("RUNFILES_MANIFEST_FILE", manifest_path_),
       // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
       // pick up RUNFILES_DIR.
       std::make_pair("JAVA_RUNFILES", RunfilesDir())}));
}

string ManifestBased::RunfilesDir() const {
  const auto pos1 = manifest_path_.size() - 9;   // "_MANIFEST"
  const auto pos2 = manifest_path_.size() - 18;  // ".runfiles_manifest"
  if (manifest_path_.rfind("/MANIFEST") == pos1 ||
      manifest_path_.rfind("\\MANIFEST") == pos1 ||
      manifest_path_.rfind(".runfiles_manifest") == pos2) {
    return std::move(manifest_path_.substr(0, pos1));  // remove ".MANIFEST"
  } else {
    return std::move(string());
  }
}

bool ManifestBased::ParseManifest(const string& path,
                                  map<string, string>* result, string* error) {
  std::ifstream stm(path);
  if (!stm.is_open()) {
    if (error) {
      std::ostringstream err;
      err << "ERROR: " << __FILE__ << "(" << __LINE__
          << "): cannot open runfiles manifest \"" << path << "\"";
      *error = err.str();
    }
    return false;
  }
  string line;
  std::getline(stm, line);
  size_t line_count = 1;
  while (!line.empty()) {
    string::size_type idx = line.find_first_of(' ');
    if (idx == string::npos) {
      if (error) {
        std::ostringstream err;
        err << "ERROR: " << __FILE__ << "(" << __LINE__
            << "): bad runfiles manifest entry in \"" << path << "\" line #"
            << line_count << ": \"" << line << "\"";
        *error = err.str();
      }
      return false;
    }
    (*result)[line.substr(0, idx)] = line.substr(idx + 1);
    std::getline(stm, line);
    ++line_count;
  }
  return true;
}

string DirectoryBased::RlocationChecked(const string& path) const {
  return std::move(runfiles_path_ + "/" + path);
}

vector<pair<string, string> > DirectoryBased::EnvVars() const {
  return std::move(vector<pair<string, string> >(
      {std::make_pair("RUNFILES_DIR", runfiles_path_),
       // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
       // pick up RUNFILES_DIR.
       std::make_pair("JAVA_RUNFILES", runfiles_path_)}));
}

}  // namespace

namespace testing {

Runfiles* TestOnly_CreateRunfiles(const std::string& argv0,
                                  function<string(const string&)> env_lookup,
                                  string* error) {
  return RunfilesImpl::Create(argv0, env_lookup, error);
}

bool TestOnly_IsAbsolute(const string& path) { return IsAbsolute(path); }

}  // namespace testing

Runfiles* Runfiles::Create(const string& argv0, string* error) {
  return RunfilesImpl::Create(
      argv0,
      [](const string& key) {
        if (key == "RUNFILES_MANIFEST_FILE" || key == "RUNFILES_DIR") {
          string val(GetEnv(key));
          return std::move(val);
        } else {
          return std::move(string());
        }
      },
      error);
}

Runfiles* Runfiles::CreateManifestBased(const string& manifest_path,
                                        string* error) {
  return ManifestBased::Create(manifest_path, error);
}

Runfiles* Runfiles::CreateDirectoryBased(const string& directory_path,
                                         string* error) {
  // Note: `error` is intentionally unused because we don't expect any errors
  // here. We expect an `error` pointer so that we may use it in the future if
  // need be, without having to change the API.
  return new DirectoryBased(directory_path);
}

}  // namespace runfiles
}  // namespace cpp
}  // namespace tools
}  // namespace bazel