Skip to content

Commit

Permalink
blaze_utils: fork file handling for platforms
Browse files Browse the repository at this point in the history
This commit repurposes file_posix.cc to be the
POSIX API file handling implementation, and adds
file_windows.cc for the Win32 API implementations.
Furthermore it introduces file_platform.h that
declares the interface.

Subsequent changes will replace POSIX API calls in
the rest of the C++ code with these abstract
methods.

Motivation: our code is so littered with
POSIX-isms that we need an abstraction layer if we
hope to compile it with MSVC.

--
MOS_MIGRATED_REVID=138615822
  • Loading branch information
laszlocsomor authored and aehlig committed Nov 9, 2016
1 parent f926f3e commit b9d7767
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
9 changes: 1 addition & 8 deletions src/main/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ cc_library(
deps = [
"//src/main/cpp/util",
"//src/main/cpp/util:blaze_exit_code",
] + select({
"//src:darwin": [],
"//src:darwin_x86_64": [],
"//src:freebsd": [],
"//src:windows": [],
"//src:windows_msvc": [],
"//conditions:default": ["//src/main/cpp/util:file_posix"],
}),
],
)

cc_library(
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/blaze_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "src/main/cpp/util/errors.h"
#include "src/main/cpp/util/exit_code.h"
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_posix.h"
#include "src/main/cpp/util/file_platform.h"
#include "src/main/cpp/util/port.h"
#include "src/main/cpp/util/strings.h"

Expand Down
15 changes: 12 additions & 3 deletions src/main/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cc_library(
hdrs = [
"errors.h",
"file.h",
"file_platform.h",
"numbers.h",
"port.h",
],
Expand All @@ -14,16 +15,24 @@ cc_library(
":blaze_exit_code",
":errors",
":file",
":file_platform",
":numbers",
":port",
":strings",
],
)

cc_library(
name = "file_posix",
srcs = ["file_posix.cc"],
hdrs = ["file_posix.h"],
name = "file_platform",
srcs = select({
"//src:windows_msvc": [
"file_windows.cc",
],
"//conditions:default": [
"file_posix.cc",
],
}),
hdrs = ["file_platform.h"],
visibility = ["//src/main/cpp:__pkg__"],
deps = [
":blaze_exit_code",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef BAZEL_SRC_MAIN_CPP_UTIL_FILE_POSIX_H_
#define BAZEL_SRC_MAIN_CPP_UTIL_FILE_POSIX_H_
#ifndef BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_
#define BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_

#include <string>

Expand All @@ -24,6 +24,16 @@ namespace blaze_util {
// looking up PATH fails.
std::string Which(const std::string &executable);

// Returns true if this path exists.
bool PathExists(const std::string& path);

// Returns true if the path exists and can be accessed to read/write as desired.
//
// If `exec` is true and the path refers to a file, it means the file must be
// executable; if the path is a directory, it means the directory must be
// openable.
bool CanAccess(const std::string& path, bool read, bool write, bool exec);

} // namespace blaze_util

#endif // BAZEL_SRC_MAIN_CPP_UTIL_FILE_POSIX_H_
#endif // BAZEL_SRC_MAIN_CPP_UTIL_FILE_PLATFORM_H_
20 changes: 19 additions & 1 deletion src/main/cpp/util/file_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 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 "src/main/cpp/util/file_posix.h"
#include "src/main/cpp/util/file_platform.h"

#include <sys/stat.h>
#include <stdlib.h> // getenv
Expand Down Expand Up @@ -53,4 +53,22 @@ string Which(const string &executable) {
return "";
}

bool PathExists(const string& path) {
return access(path.c_str(), F_OK) == 0;
}

bool CanAccess(const string& path, bool read, bool write, bool exec) {
int mode = 0;
if (read) {
mode |= R_OK;
}
if (write) {
mode |= W_OK;
}
if (exec) {
mode |= X_OK;
}
return access(path.c_str(), mode) == 0;
}

} // namespace blaze_util
36 changes: 36 additions & 0 deletions src/main/cpp/util/file_windows.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2016 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 "src/main/cpp/util/file_platform.h"

#include <windows.h>

namespace blaze_util {

using std::string;

string Which(const string &executable) {
pdie(255, "blaze_util::Which is not implemented on Windows");
}

bool PathExists(const string& path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::PathExists is not implemented on Windows");
}

bool CanAccess(const string& path, bool read, bool write, bool exec) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::CanAccess is not implemented on Windows");
}

} // namespace blaze_util

0 comments on commit b9d7767

Please sign in to comment.