├── .bazeliskrc ├── .bazelrc ├── .clang-format ├── .github └── workflows │ ├── build_and_test.yml │ └── check_code_style.yml ├── .gitignore ├── .gitmodules ├── .mergequeue └── config.yml ├── 3rdparty ├── bazel-rules-picojson │ ├── BUILD.bazel │ └── repos.bzl └── bazel-rules-rapidjson │ ├── BUILD.bazel │ └── repos.bzl ├── BUILD.bazel ├── LICENSE ├── README.md ├── WORKSPACE.bazel ├── bazel ├── BUILD.bazel ├── deps.bzl └── repos.bzl ├── include └── stout │ ├── abort.h │ ├── adaptor.h │ ├── archiver.h │ ├── assert.h │ ├── atomic-backoff.h │ ├── attributes.h │ ├── base64.h │ ├── bits.h │ ├── borrowable.h │ ├── borrowed_ptr.h │ ├── boundedhashmap.h │ ├── bytes.h │ ├── cache.h │ ├── check.h │ ├── circular_buffer.h │ ├── copy.h │ ├── cpp14.h │ ├── cpp17.h │ ├── duration.h │ ├── dynamiclibrary.h │ ├── elf.h │ ├── error.h │ ├── errorbase.h │ ├── exit.h │ ├── flags │ ├── flags.cc │ ├── flags.h │ └── v1 │ │ ├── BUILD.bazel │ │ ├── flag.proto │ │ ├── positional_argument.proto │ │ └── subcommand.proto │ ├── foreach.h │ ├── format.h │ ├── fs.h │ ├── gtest.h │ ├── gzip.h │ ├── hashmap.h │ ├── hashset.h │ ├── internal │ └── windows │ │ ├── attributes.h │ │ ├── grp.h │ │ ├── inherit.h │ │ ├── longpath.h │ │ ├── overlapped.h │ │ ├── pwd.h │ │ ├── reparsepoint.h │ │ └── symlink.h │ ├── interval.h │ ├── ip.h │ ├── json.h │ ├── jsonify.h │ ├── lambda.h │ ├── linkedhashmap.h │ ├── mac.h │ ├── multihashmap.h │ ├── multimap.h │ ├── net.h │ ├── none.h │ ├── nothing.h │ ├── notification.h │ ├── numify.h │ ├── option.h │ ├── os.h │ ├── os │ ├── access.h │ ├── bootid.h │ ├── chdir.h │ ├── chroot.h │ ├── close.h │ ├── constants.h │ ├── copyfile.h │ ├── dup.h │ ├── environment.h │ ├── exists.h │ ├── fcntl.h │ ├── find.h │ ├── fork.h │ ├── freebsd.h │ ├── fsync.h │ ├── ftruncate.h │ ├── getcwd.h │ ├── getenv.h │ ├── int_fd.h │ ├── kill.h │ ├── killtree.h │ ├── linux.h │ ├── ls.h │ ├── lseek.h │ ├── lsof.h │ ├── mkdir.h │ ├── mkdtemp.h │ ├── mktemp.h │ ├── open.h │ ├── os.h │ ├── osx.h │ ├── pagesize.h │ ├── permissions.h │ ├── pipe.h │ ├── posix │ │ ├── bootid.h │ │ ├── chdir.h │ │ ├── chown.h │ │ ├── chroot.h │ │ ├── close.h │ │ ├── copyfile.h │ │ ├── dup.h │ │ ├── environment.h │ │ ├── exists.h │ │ ├── fcntl.h │ │ ├── fork.h │ │ ├── fsync.h │ │ ├── ftruncate.h │ │ ├── getcwd.h │ │ ├── getenv.h │ │ ├── kill.h │ │ ├── killtree.h │ │ ├── ls.h │ │ ├── lseek.h │ │ ├── lsof.h │ │ ├── mkdir.h │ │ ├── mkdtemp.h │ │ ├── mktemp.h │ │ ├── open.h │ │ ├── pagesize.h │ │ ├── pipe.h │ │ ├── read.h │ │ ├── realpath.h │ │ ├── rename.h │ │ ├── rm.h │ │ ├── rmdir.h │ │ ├── sendfile.h │ │ ├── shell.h │ │ ├── signals.h │ │ ├── socket.h │ │ ├── stat.h │ │ ├── su.h │ │ ├── temp.h │ │ ├── which.h │ │ ├── write.h │ │ └── xattr.h │ ├── process.h │ ├── pstree.h │ ├── raw │ │ ├── argv.h │ │ └── environment.h │ ├── read.h │ ├── realpath.h │ ├── rename.h │ ├── rm.h │ ├── rmdir.h │ ├── sendfile.h │ ├── shell.h │ ├── signals.h │ ├── socket.h │ ├── stat.h │ ├── strerror.h │ ├── su.h │ ├── sunos.h │ ├── sysctl.h │ ├── temp.h │ ├── touch.h │ ├── utime.h │ ├── wait.h │ ├── which.h │ ├── windows │ │ ├── bootid.h │ │ ├── chdir.h │ │ ├── chroot.h │ │ ├── close.h │ │ ├── copyfile.h │ │ ├── dup.h │ │ ├── environment.h │ │ ├── exists.h │ │ ├── fcntl.h │ │ ├── fd.h │ │ ├── fork.h │ │ ├── fsync.h │ │ ├── ftruncate.h │ │ ├── getcwd.h │ │ ├── getenv.h │ │ ├── jobobject.h │ │ ├── kill.h │ │ ├── killtree.h │ │ ├── ls.h │ │ ├── lseek.h │ │ ├── lsof.h │ │ ├── mkdir.h │ │ ├── mkdtemp.h │ │ ├── mktemp.h │ │ ├── open.h │ │ ├── pagesize.h │ │ ├── pipe.h │ │ ├── read.h │ │ ├── realpath.h │ │ ├── rename.h │ │ ├── rm.h │ │ ├── rmdir.h │ │ ├── sendfile.h │ │ ├── shell.h │ │ ├── socket.h │ │ ├── stat.h │ │ ├── su.h │ │ ├── temp.h │ │ ├── which.h │ │ ├── write.h │ │ └── xattr.h │ ├── write.h │ └── xattr.h │ ├── overload.h │ ├── path.h │ ├── posix │ ├── dynamiclibrary.h │ ├── fs.h │ ├── ip.h │ ├── mac.h │ ├── net.h │ └── os.h │ ├── preprocessor.h │ ├── proc.h │ ├── protobuf.h │ ├── recordio.h │ ├── representation.h │ ├── result.h │ ├── result_of.h │ ├── set.h │ ├── some.h │ ├── stateful-tally.h │ ├── stopwatch.h │ ├── stringify.h │ ├── strings.h │ ├── svn.h │ ├── synchronized.h │ ├── tests │ ├── environment.h │ └── utils.h │ ├── thread.h │ ├── traits.h │ ├── try.h │ ├── unimplemented.h │ ├── unreachable.h │ ├── uri.h │ ├── utils.h │ ├── uuid.h │ ├── variant.h │ ├── version.h │ ├── windows.h │ └── windows │ ├── dynamiclibrary.h │ ├── error.h │ ├── format.h │ ├── fs.h │ ├── ip.h │ ├── mac.h │ ├── net.h │ └── os.h └── tests ├── BUILD.bazel ├── adaptor_tests.cc ├── archiver_tests.cc ├── atomic-backoff.cc ├── base64_tests.cc ├── bits_tests.cc ├── borrowed_ptr.cc ├── boundedhashmap_tests.cc ├── bytes_tests.cc ├── cache_tests.cc ├── cpp17_tests.cc ├── duration_tests.cc ├── dynamiclibrary_tests.cc ├── error_tests.cc ├── flags ├── BUILD.bazel ├── default_values.cc ├── duplicate_flag_name.cc ├── environment_variables.cc ├── help.cc ├── missing_flag_name.cc ├── overload_parsing.cc ├── parse.cc ├── positional_arguments.cc ├── subcommand_flags.cc ├── test_default_values.proto ├── test_flags.proto ├── test_positional_arguments.proto ├── test_subcommands.proto └── validate.cc ├── gzip_tests.cc ├── hashmap_tests.cc ├── hashset_tests.cc ├── interval_tests.cc ├── ip_tests.cc ├── json_tests.cc ├── jsonify_tests.cc ├── lambda_tests.cc ├── linkedhashmap_tests.cc ├── mac_tests.cc ├── main.cc ├── multimap_tests.cc ├── none_tests.cc ├── notification.cc ├── numify_tests.cc ├── option_tests.cc ├── os ├── copyfile_tests.cc ├── env_tests.cc ├── filesystem_tests.cc ├── process_tests.cc ├── rmdir_tests.cc ├── sendfile_tests.cc ├── signals_tests.cc ├── socket_tests.cc ├── strerror_tests.cc └── systems_tests.cc ├── os_tests.cc ├── path_tests.cc ├── proc_tests.cc ├── protobuf_tests.cc ├── protobuf_tests.proto ├── recordio_tests.cc ├── result_tests.cc ├── some_tests.cc ├── stateful-tally.cc ├── stringify_tests.cc ├── strings_tests.cc ├── svn_tests.cc ├── temporary_directory_test_tests.cc ├── try_tests.cc ├── uri_tests.cc ├── uuid_tests.cc ├── variant_tests.cc └── version_tests.cc /.bazeliskrc: -------------------------------------------------------------------------------- 1 | USE_BAZEL_VERSION=5.4.1 2 | -------------------------------------------------------------------------------- /.bazelrc: -------------------------------------------------------------------------------- 1 | # Specific Bazel build/test options. 2 | 3 | build:macos --cxxopt='-std=c++17' 4 | 5 | build:linux --cxxopt='-std=c++17' 6 | 7 | build:windows --cxxopt="/std:c++17" 8 | 9 | build --enable_platform_specific_config 10 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | dev-tools/.clang-format -------------------------------------------------------------------------------- /.github/workflows/build_and_test.yml: -------------------------------------------------------------------------------- 1 | # Workflow for building and testing flags on macOS, Ubuntu and Windows. 2 | name: Build and Run all tests 3 | 4 | # We use action's triggers 'push' and 'pull_request'. 5 | # The strategy is the following: this action will be 6 | # triggered on any push to 'main' branch and any pull 7 | # request to any branch. Thus we avoid duplicate work- 8 | # flows. 9 | on: 10 | push: 11 | branches: 12 | - "main" 13 | pull_request: 14 | branches: 15 | - "**" 16 | workflow_dispatch: 17 | inputs: 18 | # Live debug failures using tmate by toggling input parameter 19 | # 'debug_enabled': 20 | # https://github.com/mxschmitt/action-tmate#manually-triggered-debug 21 | # When manually running this workflow: 22 | # https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow 23 | debug_enabled: 24 | description: "Enable tmate debugging" 25 | type: boolean 26 | default: false 27 | 28 | jobs: 29 | build-and-test: 30 | name: Build and Test 31 | runs-on: ${{ matrix.os }} 32 | strategy: 33 | matrix: 34 | # 'windows-latest' (windows-2022) is broken due to a github runner update. 35 | # See https://github.com/actions/runner-images/issues/7662 for more details. 36 | os: ["macos-latest", "ubuntu-latest", "windows-2019"] 37 | defaults: 38 | run: 39 | shell: bash 40 | 41 | steps: 42 | # Checkout the repository under $GITHUB_WORKSPACE. 43 | - uses: actions/checkout@v2 44 | with: 45 | submodules: "recursive" 46 | 47 | - name: Build 48 | run: | 49 | bazel build \ 50 | -c dbg \ 51 | --strip="never" \ 52 | ... 53 | 54 | - name: Test 55 | run: | 56 | bazel test \ 57 | -c dbg \ 58 | --strip="never" \ 59 | --test_output=errors \ 60 | tests/... \ 61 | --test_arg=--gtest_shuffle \ 62 | --test_arg=--gtest_repeat=100 63 | 64 | - name: Debug using tmate (if failure) 65 | uses: mxschmitt/action-tmate@v3 66 | # Optionally enable tmate debugging if the workflow was manually-triggered 67 | # with `debug_enabled` set to `true`. 68 | # https://github.com/mxschmitt/action-tmate#manually-triggered-debug 69 | if: ${{ failure() && github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }} 70 | -------------------------------------------------------------------------------- /.github/workflows/check_code_style.yml: -------------------------------------------------------------------------------- 1 | name: Check Code Style 2 | 3 | # We use action's triggers 'push' and 'pull_request'. 4 | # The strategy is the following: this action will be 5 | # triggered on any push to 'main' branch and any pull 6 | # request to any branch. Thus we avoid duplicate work- 7 | # flows. 8 | on: 9 | push: 10 | branches: 11 | - "main" 12 | pull_request: 13 | branches: 14 | - "**" 15 | workflow_dispatch: 16 | inputs: 17 | # Live debug failures using tmate by toggling input parameter 18 | # 'debug_enabled': 19 | # https://github.com/mxschmitt/action-tmate#manually-triggered-debug 20 | # When manually running this workflow: 21 | # https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow 22 | debug_enabled: 23 | description: "Enable tmate debugging" 24 | type: boolean 25 | default: false 26 | 27 | jobs: 28 | check_code_style: 29 | runs-on: ${{ matrix.os }} 30 | strategy: 31 | matrix: 32 | os: ["ubuntu-latest"] 33 | 34 | steps: 35 | # We should checkout the repo with submodules 36 | # cause we need to have symlink to 37 | # dev-tools/.clang-format file for all code 38 | # style checks. 39 | - uses: actions/checkout@v2 40 | with: 41 | submodules: "recursive" 42 | 43 | # Call the composite action to check files 44 | # for correct code style. This action (action.yml) 45 | # is in `dev-tools` submodule. 46 | - uses: ./dev-tools/check-code-style 47 | with: 48 | os: ${{matrix.os}} 49 | 50 | - name: Debug using tmate (if failure) 51 | uses: mxschmitt/action-tmate@v3 52 | # Optionally enable tmate debugging if the workflow was manually-triggered 53 | # with `debug_enabled` set to `true`. 54 | # https://github.com/mxschmitt/action-tmate#manually-triggered-debug 55 | if: ${{ failure() && github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }} 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | .vscode 3 | bazel-* 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "dev-tools"] 2 | path = dev-tools 3 | url = https://github.com/3rdparty/dev-tools.git 4 | -------------------------------------------------------------------------------- /.mergequeue/config.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.0 2 | merge_rules: 3 | labels: 4 | trigger: mergequeue-ready 5 | skip_line: "" 6 | merge_failed: mergequeue-failed 7 | skip_delete_branch: "" 8 | update_latest: true 9 | delete_branch: true 10 | use_rebase: true 11 | enable_comments: true 12 | ci_timeout_mins: 0 13 | preconditions: 14 | number_of_approvals: 1 15 | required_checks: 16 | - check_code_style (ubuntu-latest) 17 | - Build and Test (windows-2019) 18 | - Build and Test (ubuntu-latest) 19 | - Build and Test (macos-latest) 20 | use_github_mergeability: false 21 | conversation_resolution_required: true 22 | merge_mode: 23 | type: default 24 | parallel_mode: null 25 | auto_update: 26 | enabled: false 27 | label: "" 28 | max_runs_for_update: 0 29 | merge_commit: 30 | use_title_and_body: false 31 | merge_strategy: 32 | name: squash 33 | override_labels: 34 | squash: mergequeue-squash 35 | merge: "" 36 | rebase: mergequeue-rebase 37 | -------------------------------------------------------------------------------- /3rdparty/bazel-rules-picojson/BUILD.bazel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/3rdparty/stout/77b920d359e2aff03018d0e22b976eb5f7d7eb4f/3rdparty/bazel-rules-picojson/BUILD.bazel -------------------------------------------------------------------------------- /3rdparty/bazel-rules-picojson/repos.bzl: -------------------------------------------------------------------------------- 1 | """Adds necessary repostories/archives.""" 2 | 3 | ######################################################################## 4 | # DO NOT EDIT THIS FILE unless you are inside the 5 | # https://github.com/3rdparty/bazel-rules-picojson repository. If you 6 | # encounter it anywhere else it is because it has been copied there in 7 | # order to simplify adding transitive dependencies. If you want a 8 | # different version of bazel-rules-picojson follow the Bazel build 9 | # instructions at https://github.com/3rdparty/bazel-rules-picojson. 10 | ######################################################################## 11 | 12 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 13 | 14 | def repos(external = True, repo_mapping = {}): 15 | if external and "com_github_3rdparty_bazel_rules_picojson" not in native.existing_rules(): 16 | git_repository( 17 | name = "com_github_3rdparty_bazel_rules_picojson", 18 | commit = "aace5edcab4d5005bf8c76b14abadf4c0324504c", 19 | remote = "https://github.com/3rdparty/bazel-rules-picojson", 20 | shallow_since = "1616863011 -0700", 21 | repo_mapping = repo_mapping, 22 | ) 23 | -------------------------------------------------------------------------------- /3rdparty/bazel-rules-rapidjson/BUILD.bazel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/3rdparty/stout/77b920d359e2aff03018d0e22b976eb5f7d7eb4f/3rdparty/bazel-rules-rapidjson/BUILD.bazel -------------------------------------------------------------------------------- /3rdparty/bazel-rules-rapidjson/repos.bzl: -------------------------------------------------------------------------------- 1 | """Adds necessary repostories/archives.""" 2 | 3 | ######################################################################## 4 | # DO NOT EDIT THIS FILE unless you are inside the 5 | # https://github.com/3rdparty/bazel-rules-rapidjson repository. If you 6 | # encounter it anywhere else it is because it has been copied there in 7 | # order to simplify adding transitive dependencies. If you want a 8 | # different version of bazel-rules-rapidjson follow the Bazel build 9 | # instructions at https://github.com/3rdparty/bazel-rules-rapidjson. 10 | ######################################################################## 11 | 12 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 13 | 14 | def repos(external = True, repo_mapping = {}): 15 | if external and "com_github_3rdparty_bazel_rules_rapidjson" not in native.existing_rules(): 16 | git_repository( 17 | name = "com_github_3rdparty_bazel_rules_rapidjson", 18 | commit = "3b3f64f0efda7fb543b9d3af46ec0226eac12d3c", 19 | remote = "https://github.com/3rdparty/bazel-rules-rapidjson", 20 | shallow_since = "1616863826 -0700", 21 | repo_mapping = repo_mapping, 22 | ) 23 | -------------------------------------------------------------------------------- /WORKSPACE.bazel: -------------------------------------------------------------------------------- 1 | workspace(name = "stout") 2 | 3 | load("//bazel:repos.bzl", "repos") 4 | 5 | repos(external = False) 6 | 7 | load("//bazel:deps.bzl", "deps") 8 | 9 | deps() 10 | -------------------------------------------------------------------------------- /bazel/BUILD.bazel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/3rdparty/stout/77b920d359e2aff03018d0e22b976eb5f7d7eb4f/bazel/BUILD.bazel -------------------------------------------------------------------------------- /bazel/deps.bzl: -------------------------------------------------------------------------------- 1 | """Dependency specific initialization for stout.""" 2 | 3 | load("@com_github_3rdparty_bazel_rules_picojson//bazel:deps.bzl", picojson_deps = "deps") 4 | load("@com_github_3rdparty_bazel_rules_rapidjson//bazel:deps.bzl", rapidjson_deps = "deps") 5 | load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps") 6 | load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") 7 | 8 | def deps(repo_mapping = {}): 9 | """Propagate all dependencies. 10 | 11 | Args: 12 | repo_mapping (str): {}. 13 | """ 14 | boost_deps() 15 | 16 | picojson_deps( 17 | repo_mapping = repo_mapping, 18 | ) 19 | 20 | rapidjson_deps( 21 | repo_mapping = repo_mapping, 22 | ) 23 | 24 | protobuf_deps() 25 | -------------------------------------------------------------------------------- /include/stout/adaptor.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // limitations under the License. 11 | 12 | #pragma once 13 | 14 | #include 15 | 16 | //////////////////////////////////////////////////////////////////////// 17 | 18 | // This is modeled after boost's Range Adaptor. It wraps an existing 19 | // iterator to provide a new iterator with different behavior. See 20 | // details in boost's reference document: 21 | // http://www.boost.org/doc/libs/1_53_0/libs/range/doc/html/index.html 22 | namespace adaptor { 23 | 24 | using boost::adaptors::reverse; 25 | 26 | } // namespace adaptor 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | -------------------------------------------------------------------------------- /include/stout/assert.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/abort.h" 16 | 17 | //////////////////////////////////////////////////////////////////////// 18 | 19 | // Provide an async signal safe version `assert`. Please use `assert` 20 | // instead if you don't need async signal safety. 21 | #ifdef NDEBUG 22 | #define ASSERT(e) ((void) 0) 23 | #else 24 | #define ASSERT(e) ((void) ((e) ? ((void) 0) : ABORT(#e))) 25 | #endif 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | -------------------------------------------------------------------------------- /include/stout/atomic-backoff.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "stout/thread.h" 4 | 5 | //////////////////////////////////////////////////////////////////////// 6 | 7 | namespace stout { 8 | 9 | //////////////////////////////////////////////////////////////////////// 10 | 11 | class AtomicBackoff { 12 | public: 13 | AtomicBackoff(size_t pauses_before_yield = 16, size_t pauses = 1) 14 | : pauses_before_yield(pauses_before_yield), 15 | pauses(pauses) {} 16 | 17 | AtomicBackoff(const AtomicBackoff&) = delete; 18 | AtomicBackoff& operator=(const AtomicBackoff&) = delete; 19 | 20 | public: 21 | void pause() { 22 | if (pauses <= pauses_before_yield) { 23 | this_thread::pause(); 24 | pauses *= 2; 25 | } else { 26 | std::this_thread::yield(); 27 | } 28 | } 29 | 30 | private: 31 | const size_t pauses_before_yield; 32 | size_t pauses; 33 | }; 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace stout 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/attributes.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | #ifdef _WIN32 18 | #define NORETURN __declspec(noreturn) 19 | #else 20 | #define NORETURN __attribute__((noreturn)) 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/bits.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | //////////////////////////////////////////////////////////////////////// 18 | 19 | // Provides efficient bit operations. 20 | // More details can be found at: 21 | // http://graphics.stanford.edu/~seander/bithacks.html 22 | namespace bits { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | // Counts set bits from a 32 bit unsigned integer using Hamming weight. 27 | inline int countSetBits(uint32_t value) { 28 | int count = 0; 29 | value = value - ((value >> 1) & 0x55555555); 30 | value = (value & 0x33333333) + ((value >> 2) & 0x33333333); 31 | count = (((value + (value >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; 32 | 33 | return count; 34 | } 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | 38 | } // namespace bits 39 | 40 | //////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /include/stout/borrowable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "stout/borrowed_ptr.h" 4 | 5 | // TODO(benh): remove this file? 6 | -------------------------------------------------------------------------------- /include/stout/circular_buffer.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | // Using `boost::circular_buffer::debug_iterator` can lead to segfaults 18 | // because they are not thread-safe (see MESOS-9177), so we must ensure 19 | // they're disabled. Both versions of this macro are needed to account 20 | // for differences between older and newer Boost versions. 21 | #define BOOST_CB_DISABLE_DEBUG 1 22 | #define BOOST_CB_ENABLE_DEBUG 0 23 | #include 24 | 25 | using boost::circular_buffer; 26 | 27 | //////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /include/stout/copy.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | //////////////////////////////////////////////////////////////////////// 18 | 19 | namespace stout { 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | template 24 | std::decay_t copy(T&& t) { 25 | return std::decay_t(t); 26 | } 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | } // namespace stout 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | -------------------------------------------------------------------------------- /include/stout/dynamiclibrary.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | // For readability, we minimize the number of #ifdef blocks in the code by 18 | // splitting platform specific system calls into separate directories. 19 | #ifdef _WIN32 20 | #include "stout/windows/dynamiclibrary.h" 21 | #else 22 | #include "stout/posix/dynamiclibrary.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/error.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | // NOTE: The order of these `#include`s is important. This file is structured 15 | // as a series of `#include`s for historical reasons. Before, `stout/error` 16 | // simply contained the definitions of `Error` and `ErrnoError`. The addition 17 | // of Windows required the addition of `WindowsError`. But, we try to avoid 18 | // `#ifdef`'ing code, opting instead to `#ifdef` `#include` statements. Hence, 19 | // we simply move the `error.hpp` code to `errorbase.hpp` and include the 20 | // Windows error code below it. 21 | #include "stout/errorbase.h" 22 | 23 | #ifdef _WIN32 24 | #include "stout/windows/error.h" 25 | #endif // _WIN32 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | using SocketError = 30 | #ifdef _WIN32 31 | WindowsSocketError; 32 | #else 33 | ErrnoError; 34 | #endif // _WIN32 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | -------------------------------------------------------------------------------- /include/stout/errorbase.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "stout/os/strerror.h" 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | // A useful type that can be used to represent a Try that has 25 | // failed. You can also use 'ErrnoError' to append the error message 26 | // associated with the current 'errno' to your own error message. 27 | // 28 | // Examples: 29 | // 30 | // Result result = Error("uninitialized"); 31 | // Try = Error("uninitialized"); 32 | // 33 | // void foo(Try t) {} 34 | // 35 | // foo(Error("some error here")); 36 | 37 | class Error { 38 | public: 39 | explicit Error(const std::string& _message) 40 | : message(_message) {} 41 | 42 | bool operator==(const Error& that) const { 43 | return message == that.message; 44 | } 45 | 46 | const std::string message; 47 | }; 48 | 49 | //////////////////////////////////////////////////////////////////////// 50 | 51 | class ErrnoError : public Error { 52 | public: 53 | ErrnoError() 54 | : ErrnoError(errno) {} 55 | 56 | explicit ErrnoError(int _code) 57 | : Error(os::strerror(_code)), 58 | code(_code) {} 59 | 60 | explicit ErrnoError(const std::string& message) 61 | : ErrnoError(errno, message) {} 62 | 63 | ErrnoError(int _code, const std::string& message) 64 | : Error(message + ": " + os::strerror(_code)), 65 | code(_code) {} 66 | 67 | const int code; 68 | }; 69 | 70 | //////////////////////////////////////////////////////////////////////// 71 | 72 | inline std::ostream& operator<<(std::ostream& stream, const Error& error) { 73 | return stream << error.message; 74 | } 75 | 76 | //////////////////////////////////////////////////////////////////////// 77 | -------------------------------------------------------------------------------- /include/stout/flags/v1/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_proto_library") 2 | load("@rules_proto//proto:defs.bzl", "proto_library") 3 | 4 | proto_library( 5 | name = "flag_proto", 6 | srcs = [ 7 | ":flag.proto", 8 | ":positional_argument.proto", 9 | ":subcommand.proto", 10 | ], 11 | visibility = ["//visibility:public"], 12 | deps = [ 13 | # Well known protos should be included as deps in the 14 | # proto_library rules of the source files importing them. 15 | # A list of all @com_google_protobuf well known protos can 16 | # seen with: 17 | # `bazel query 'kind(proto_library, @com_google_protobuf//:all)'` 18 | "@com_google_protobuf//:descriptor_proto", 19 | ], 20 | ) 21 | 22 | cc_proto_library( 23 | name = "flag", 24 | visibility = ["//visibility:public"], 25 | deps = [":flag_proto"], 26 | ) 27 | -------------------------------------------------------------------------------- /include/stout/flags/v1/flag.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package stout.v1; 4 | 5 | import "google/protobuf/descriptor.proto"; 6 | 7 | message Flag { 8 | repeated string names = 1; 9 | repeated string deprecated_names = 2; 10 | string help = 3; 11 | // Default value. Will be validated at a parsing time. 12 | optional string default = 4; 13 | // Whether or not this flag is required. 14 | bool required = 5; 15 | // TODO: bool deprecated = 5; 16 | // TODO: bool aggregatable = 6; 17 | // TODO: bool sensitive = 7; 18 | } 19 | 20 | extend google.protobuf.FieldOptions { 21 | optional Flag flag = 12345; 22 | } 23 | 24 | message StandardFlags { 25 | bool help = 1 [ 26 | (stout.v1.flag) = { 27 | names: [ "help" ] 28 | help: "whether or not to display this help message" 29 | } 30 | ]; 31 | } 32 | -------------------------------------------------------------------------------- /include/stout/flags/v1/subcommand.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package stout.v1; 4 | 5 | import "google/protobuf/descriptor.proto"; 6 | 7 | message Subcommand { 8 | repeated string names = 1; 9 | repeated string deprecated_names = 2; 10 | string help = 3; 11 | // TODO: bool deprecated = 4; 12 | } 13 | 14 | extend google.protobuf.FieldOptions { 15 | optional Subcommand subcommand = 1001; 16 | } 17 | -------------------------------------------------------------------------------- /include/stout/fs.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | // For readability, we minimize the number of #ifdef blocks in the code by 18 | // splitting platform specific system calls into separate directories. 19 | #ifdef _WIN32 20 | #include "stout/windows/fs.h" 21 | #else 22 | #include "stout/posix/fs.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/internal/windows/attributes.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | // Since 'fmt' library doesn't support 'wide' conversion (e.g 19 | // from 'std::wstring' to 'std::string' and vice versa) we use 20 | // API from 'include/stout/stringify.h' (e.g: 21 | // std::string stringify(const std::wstring& wstr) - function). 22 | // Check the issue for fmt conversion on github: 23 | // https://github.com/fmtlib/fmt/issues/1116 24 | #include "stout/stringify.h" 25 | #include "stout/try.h" 26 | #include "stout/windows.h" 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | namespace internal { 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | 34 | namespace windows { 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | 38 | inline Try get_file_attributes(const std::wstring& path) { 39 | const DWORD attributes = ::GetFileAttributesW(path.data()); 40 | if (attributes == INVALID_FILE_ATTRIBUTES) { 41 | return WindowsError( 42 | "Failed to get attributes for file '" + stringify(path) + "'"); 43 | } 44 | return attributes; 45 | } 46 | 47 | //////////////////////////////////////////////////////////////////////// 48 | 49 | } // namespace windows 50 | 51 | //////////////////////////////////////////////////////////////////////// 52 | 53 | } // namespace internal 54 | 55 | //////////////////////////////////////////////////////////////////////// 56 | -------------------------------------------------------------------------------- /include/stout/internal/windows/grp.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "stout/windows.h" 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | // Dummy struct for POSIX compliance. 26 | // The name of the group - `gr_name`. 27 | // Numerical group ID - `gr_gid`. 28 | // Pointer to a null-terminated array of character pointers to 29 | // member names - `gr_mem`. 30 | struct group { 31 | char* gr_name; 32 | gid_t gr_gid; 33 | char** gr_mem; 34 | }; 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | 38 | // Dummy implementation of `getgrgid` for POSIX compliance. Per the POSIX 39 | // specification[1], we are to return `nullptr` if an entry matching the GID is 40 | // not found. On Windows, we will never find such an entry, so we always return 41 | // `nullptr`. Just to be safe, we also set `errno` to `ENOSYS` which indicates 42 | // the function is not implemented. 43 | // 44 | // [1] http://pubs.opengroup.org/onlinepubs/009695399/functions/getgrgid.html 45 | inline struct group* getgrgid(gid_t) { 46 | errno = ENOSYS; 47 | return nullptr; 48 | } 49 | 50 | //////////////////////////////////////////////////////////////////////// 51 | -------------------------------------------------------------------------------- /include/stout/internal/windows/pwd.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "stout/windows.h" 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | // Dummy struct for POSIX compliance. 26 | struct passwd { 27 | char* pw_name; // User's login name. 28 | uid_t pw_uid; // Numerical user ID. 29 | gid_t pw_gid; // Numerical group ID. 30 | char* pw_dir; // Initial working directory. 31 | char* pw_shell; // Program to use as shell. 32 | }; 33 | 34 | //////////////////////////////////////////////////////////////////////// 35 | 36 | // Dummy implementation of `getpwuid` for POSIX compliance. Per the POSIX 37 | // specification[1], we are to return `nullptr` if an entry matching the UID is 38 | // not found. On Windows, we will never find such an entry, so we always return 39 | // `nullptr`. Just to be safe, we also set `errno` to `ENOSYS` which indicates 40 | // the function is not implemented. 41 | // 42 | // [1] http://pubs.opengroup.org/onlinepubs/009695399/functions/getgrgid.html 43 | inline struct passwd* getpwuid(uid_t) { 44 | errno = ENOSYS; 45 | return nullptr; 46 | } 47 | 48 | //////////////////////////////////////////////////////////////////////// 49 | -------------------------------------------------------------------------------- /include/stout/none.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | // A useful type that can be used to represent an Option or Result. 18 | // 19 | // Examples: 20 | // 21 | // Result result = None(); 22 | // Option = None(); 23 | // 24 | // void foo(Option o = None()) {} 25 | // 26 | // foo(None()); 27 | 28 | struct None {}; 29 | 30 | //////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /include/stout/nothing.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | struct Nothing {}; 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | -------------------------------------------------------------------------------- /include/stout/os/access.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/try.h" 19 | 20 | #ifdef _WIN32 21 | #include "stout/internal/windows/longpath.h" 22 | #include "stout/windows.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | namespace os { 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | inline Try access(const std::string& path, int how) { 32 | int result; 33 | 34 | #ifdef _WIN32 35 | std::wstring longpath = ::internal::windows::longpath(path); 36 | result = ::_waccess(longpath.data(), how); 37 | #else 38 | result = ::access(path.data(), how); 39 | #endif 40 | 41 | if (result < 0) { 42 | if (errno == EACCES) { 43 | return false; 44 | } else { 45 | return ErrnoError(); 46 | } 47 | } 48 | 49 | return true; 50 | } 51 | 52 | //////////////////////////////////////////////////////////////////////// 53 | 54 | } // namespace os 55 | 56 | //////////////////////////////////////////////////////////////////////// 57 | -------------------------------------------------------------------------------- /include/stout/os/bootid.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/bootid.h" 19 | #else 20 | #include "stout/os/posix/bootid.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/chdir.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/chdir.h" 19 | #else 20 | #include "stout/os/posix/chdir.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/chroot.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #ifdef _WIN32 20 | #include "stout/os/windows/chroot.h" 21 | #else 22 | #include "stout/os/posix/chroot.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/os/close.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | #include "stout/os/windows/close.h" 17 | #else 18 | #include "stout/os/posix/close.h" 19 | #endif // _WIN32 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | -------------------------------------------------------------------------------- /include/stout/os/constants.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | //////////////////////////////////////////////////////////////////////// 18 | 19 | namespace os { 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | constexpr char WINDOWS_PATH_SEPARATOR = '\\'; 24 | constexpr char POSIX_PATH_SEPARATOR = '/'; 25 | 26 | #ifndef _WIN32 27 | constexpr char PATH_SEPARATOR = POSIX_PATH_SEPARATOR; 28 | #else 29 | constexpr char PATH_SEPARATOR = WINDOWS_PATH_SEPARATOR; 30 | #endif // _WIN32 31 | 32 | #ifndef _WIN32 33 | constexpr char DEV_NULL[] = "/dev/null"; 34 | #else 35 | constexpr char DEV_NULL[] = "NUL"; 36 | #endif // _WIN32 37 | 38 | #ifdef _WIN32 39 | // This prefix is prepended to absolute paths on Windows to indicate the path 40 | // may be greater than 255 characters. 41 | // 42 | // NOTE: We do not use a R"raw string" here because syntax highlighters do not 43 | // handle mismatched backslashes well. 44 | constexpr char LONGPATH_PREFIX[] = "\\\\?\\"; 45 | #endif // _WIN32 46 | 47 | //////////////////////////////////////////////////////////////////////// 48 | 49 | } // namespace os 50 | 51 | //////////////////////////////////////////////////////////////////////// 52 | -------------------------------------------------------------------------------- /include/stout/os/copyfile.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | #include "stout/os/windows/copyfile.h" 17 | #else 18 | #include "stout/os/posix/copyfile.h" 19 | #endif // _WIN32 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | -------------------------------------------------------------------------------- /include/stout/os/dup.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/dup.h" 19 | #else 20 | #include "stout/os/posix/dup.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/environment.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/environment.h" 19 | #else 20 | #include "stout/os/posix/environment.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/exists.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/exists.h" 19 | #else 20 | #include "stout/os/posix/exists.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/fcntl.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/fcntl.h" 19 | #else 20 | #include "stout/os/posix/fcntl.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/fork.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/fork.h" 19 | #else 20 | #include "stout/os/posix/fork.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/fsync.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/fsync.h" 19 | #else 20 | #include "stout/os/posix/fsync.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/ftruncate.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | // For readability, we minimize the number of #ifdef blocks in the code by 15 | // splitting platform specific system calls into separate directories. 16 | #ifdef _WIN32 17 | #include "stout/os/windows/ftruncate.h" 18 | #else 19 | #include "stout/os/posix/ftruncate.h" 20 | #endif // _WIN32 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | -------------------------------------------------------------------------------- /include/stout/os/getcwd.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/getcwd.h" 19 | #else 20 | #include "stout/os/posix/getcwd.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/getenv.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | #include "stout/os/windows/getenv.h" 17 | #else 18 | #include "stout/os/posix/getenv.h" 19 | #endif // _WIN32 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | -------------------------------------------------------------------------------- /include/stout/os/int_fd.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specifc system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/fd.h" 19 | #endif // _WIN32 20 | 21 | // The `int_fd` type is designed to be able to keep / continue to write the 22 | // existing POSIX file descriptor pattern in a portable manner with Windows. 23 | // 24 | // IMPORTANT: Use the `int_fd` in platform-agnostic code paths, and use `int` 25 | // or `os::WindowsFD` directly in platform-specific code paths. 26 | // 27 | // NOTE: The `int_` prefix is meant to indicate that on POSIX, `int_fd` will 28 | // behave exactly as-is. 29 | using int_fd = 30 | #ifdef _WIN32 31 | os::WindowsFD; 32 | #else 33 | int; 34 | #endif // _WIN32 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | -------------------------------------------------------------------------------- /include/stout/os/kill.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/kill.h" 19 | #else 20 | #include "stout/os/posix/kill.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/killtree.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/killtree.h" 19 | #else 20 | #include "stout/os/posix/killtree.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/ls.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/ls.h" 19 | #else 20 | #include "stout/os/posix/ls.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/lseek.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/lseek.h" 19 | #else 20 | #include "stout/os/posix/lseek.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/lsof.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/lsof.h" 19 | #else 20 | #include "stout/os/posix/lsof.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/mkdir.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/mkdir.h" 19 | #else 20 | #include "stout/os/posix/mkdir.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/mkdtemp.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | // For readability, we minimize the number of #ifdef blocks in the code by 20 | // splitting platform specific system calls into separate directories. 21 | #ifdef _WIN32 22 | #include "stout/os/windows/mkdtemp.h" 23 | #else 24 | #include "stout/os/posix/mkdtemp.h" 25 | #endif // _WIN32 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | -------------------------------------------------------------------------------- /include/stout/os/mktemp.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/mktemp.h" 19 | #else 20 | #include "stout/os/posix/mktemp.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/open.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/open.h" 19 | #else 20 | #include "stout/os/posix/open.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/os.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/bytes.h" 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | 21 | namespace os { 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | // Structure returned by loadavg(). Encodes system load average 26 | // for the last 1, 5 and 15 minutes. 27 | struct Load { 28 | double one; 29 | double five; 30 | double fifteen; 31 | }; 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | 35 | // Structure returned by memory() containing the total size of main 36 | // and free memory. 37 | struct Memory { 38 | Bytes total; 39 | Bytes free; 40 | Bytes totalSwap; 41 | Bytes freeSwap; 42 | }; 43 | 44 | //////////////////////////////////////////////////////////////////////// 45 | 46 | // The structure returned by uname describing the currently running system. 47 | struct UTSInfo { 48 | std::string sysname; // Operating system name (e.g. Linux). 49 | std::string nodename; // Network name of this machine. 50 | std::string release; // Release level of the operating system. 51 | std::string version; // Version level of the operating system. 52 | std::string machine; // Machine hardware platform. 53 | }; 54 | 55 | //////////////////////////////////////////////////////////////////////// 56 | 57 | } // namespace os 58 | 59 | //////////////////////////////////////////////////////////////////////// 60 | -------------------------------------------------------------------------------- /include/stout/os/pagesize.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/pagesize.h" 19 | #else 20 | #include "stout/os/posix/pagesize.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/permissions.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "stout/error.h" 20 | #include "stout/try.h" 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | namespace os { 25 | 26 | //////////////////////////////////////////////////////////////////////// 27 | 28 | struct Permissions { 29 | explicit Permissions(mode_t mode) { 30 | owner.r = (mode & S_IRUSR) != 0; 31 | owner.w = (mode & S_IWUSR) != 0; 32 | owner.x = (mode & S_IXUSR) != 0; 33 | owner.rwx = (mode & S_IRWXU) != 0; 34 | group.r = (mode & S_IRGRP) != 0; 35 | group.w = (mode & S_IWGRP) != 0; 36 | group.x = (mode & S_IXGRP) != 0; 37 | group.rwx = (mode & S_IRWXG) != 0; 38 | others.r = (mode & S_IROTH) != 0; 39 | others.w = (mode & S_IWOTH) != 0; 40 | others.x = (mode & S_IXOTH) != 0; 41 | others.rwx = (mode & S_IRWXO) != 0; 42 | setuid = (mode & S_ISUID) != 0; 43 | setgid = (mode & S_ISGID) != 0; 44 | sticky = (mode & S_ISVTX) != 0; 45 | } 46 | 47 | struct 48 | { 49 | bool r; 50 | bool w; 51 | bool x; 52 | bool rwx; 53 | } owner, group, others; 54 | 55 | bool setuid; 56 | bool setgid; 57 | bool sticky; 58 | }; 59 | 60 | //////////////////////////////////////////////////////////////////////// 61 | 62 | inline Try permissions(const std::string& path) { 63 | #ifdef _WIN32 64 | VLOG(2) << "`os::permissions` has been called, but is a stub on Windows"; 65 | return Permissions(0); 66 | #else 67 | struct stat status; 68 | if (::stat(path.c_str(), &status) < 0) { 69 | return ErrnoError(); 70 | } 71 | 72 | return Permissions(status.st_mode); 73 | #endif // _WIN32 74 | } 75 | 76 | //////////////////////////////////////////////////////////////////////// 77 | 78 | } // namespace os 79 | 80 | //////////////////////////////////////////////////////////////////////// 81 | -------------------------------------------------------------------------------- /include/stout/os/pipe.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/pipe.h" 19 | #else 20 | #include "stout/os/posix/pipe.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/posix/bootid.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "fmt/format.h" 20 | #include "stout/error.h" 21 | #include "stout/os/read.h" 22 | #include "stout/strings.h" 23 | #include "stout/try.h" 24 | 25 | #if defined(__APPLE__) || defined(__FreeBSD__) 26 | #include "stout/os/sysctl.h" 27 | #endif // __APPLE__ || __FreeBSD__ 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | namespace os { 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | 35 | inline Try bootId() { 36 | #ifdef __linux__ 37 | Try read = os::read("/proc/sys/kernel/random/boot_id"); 38 | if (read.isError()) { 39 | return read; 40 | } 41 | return strings::trim(read.get()); 42 | #elif defined(__APPLE__) || defined(__FreeBSD__) 43 | // For OS X, we use the boot time in seconds as a unique boot id. 44 | // Although imperfect, this works quite well in practice. NOTE: we can't use 45 | // milliseconds here instead of seconds because the relatively high 46 | // imprecision of millisecond resolution would cause `bootId` to return a 47 | // different number nearly every time it was called. 48 | Try boot_time = os::sysctl(CTL_KERN, KERN_BOOTTIME).time(); 49 | if (boot_time.isError()) { 50 | return Error(boot_time.error()); 51 | } 52 | return stringify(boot_time->tv_sec); 53 | #else 54 | return Error("Not implemented"); 55 | #endif 56 | } 57 | 58 | //////////////////////////////////////////////////////////////////////// 59 | 60 | } // namespace os 61 | 62 | //////////////////////////////////////////////////////////////////////// 63 | -------------------------------------------------------------------------------- /include/stout/os/posix/chdir.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/nothing.h" 19 | #include "stout/try.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try chdir(const std::string& directory) { 28 | if (::chdir(directory.c_str()) < 0) { 29 | return ErrnoError(); 30 | } 31 | 32 | return Nothing(); 33 | } 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace os 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/os/posix/chroot.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "stout/error.h" 22 | #include "stout/nothing.h" 23 | #include "stout/try.h" 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | namespace os { 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | inline Try chroot(const std::string& directory) { 32 | if (::chroot(directory.c_str()) < 0) { 33 | return ErrnoError(); 34 | } 35 | 36 | return Nothing(); 37 | } 38 | 39 | } // namespace os 40 | 41 | //////////////////////////////////////////////////////////////////////// 42 | -------------------------------------------------------------------------------- /include/stout/os/posix/close.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/nothing.h" 19 | #include "stout/try.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try close(int fd) { 28 | if (::close(fd) != 0) { 29 | return ErrnoError(); 30 | } 31 | 32 | return Nothing(); 33 | } 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace os 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/os/posix/dup.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/nothing.h" 18 | #include "stout/try.h" 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | inline Try dup(int fd) { 27 | int result = ::dup(fd); 28 | if (result < 0) { 29 | return ErrnoError(); 30 | } 31 | return result; 32 | } 33 | 34 | //////////////////////////////////////////////////////////////////////// 35 | 36 | } // namespace os 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | -------------------------------------------------------------------------------- /include/stout/os/posix/environment.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include "stout/os/raw/environment.h" 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | inline std::map environment() { 27 | char** env = os::raw::environment(); 28 | 29 | std::map result; 30 | 31 | for (size_t index = 0; env[index] != nullptr; index++) { 32 | std::string entry(env[index]); 33 | size_t position = entry.find_first_of('='); 34 | if (position == std::string::npos) { 35 | continue; // Skip malformed environment entries. 36 | } 37 | 38 | result[entry.substr(0, position)] = entry.substr(position + 1); 39 | } 40 | 41 | return result; 42 | } 43 | 44 | //////////////////////////////////////////////////////////////////////// 45 | 46 | } // namespace os 47 | 48 | //////////////////////////////////////////////////////////////////////// 49 | -------------------------------------------------------------------------------- /include/stout/os/posix/exists.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline bool exists(const std::string& path) { 28 | struct stat s; 29 | 30 | if (::lstat(path.c_str(), &s) < 0) { 31 | return false; 32 | } 33 | return true; 34 | } 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | 38 | // Determine if the process identified by pid exists. 39 | // NOTE: Zombie processes have a pid and therefore exist. See os::process(pid) 40 | // to get details of a process. 41 | inline bool exists(pid_t pid) { 42 | // The special signal 0 is used to check if the process exists; see kill(2). 43 | // If the current user does not have permission to signal pid, but it does 44 | // exist, then ::kill will return -1 and set errno == EPERM. 45 | if (::kill(pid, 0) == 0 || errno == EPERM) { 46 | return true; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | //////////////////////////////////////////////////////////////////////// 53 | 54 | } // namespace os 55 | 56 | //////////////////////////////////////////////////////////////////////// 57 | -------------------------------------------------------------------------------- /include/stout/os/posix/fsync.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "stout/nothing.h" 20 | #include "stout/os/close.h" 21 | #include "stout/os/int_fd.h" 22 | #include "stout/os/open.h" 23 | #include "stout/try.h" 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | namespace os { 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | inline Try fsync(int fd) { 32 | if (::fsync(fd) == -1) { 33 | return ErrnoError(); 34 | } 35 | 36 | return Nothing(); 37 | } 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | 41 | // A wrapper function for the above `fsync()` with opening and closing the 42 | // file. 43 | // NOTE: This function is POSIX only and can be used to commit changes to a 44 | // directory (e.g., renaming files) to the filesystem. 45 | inline Try fsync(const std::string& path) { 46 | Try fd = os::open(path, O_RDONLY | O_CLOEXEC); 47 | 48 | if (fd.isError()) { 49 | return Error(fd.error()); 50 | } 51 | 52 | Try result = fsync(fd.get()); 53 | 54 | // We ignore the return value of `close()` since any I/O-related failure 55 | // scenarios would already have been triggered by `open()` or `fsync()`. 56 | os::close(fd.get()); 57 | 58 | return result; 59 | } 60 | 61 | //////////////////////////////////////////////////////////////////////// 62 | 63 | } // namespace os 64 | 65 | //////////////////////////////////////////////////////////////////////// 66 | -------------------------------------------------------------------------------- /include/stout/os/posix/ftruncate.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "fmt/format.h" 18 | #include "stout/error.h" 19 | #include "stout/nothing.h" 20 | #include "stout/try.h" 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | namespace os { 25 | 26 | //////////////////////////////////////////////////////////////////////// 27 | 28 | inline Try ftruncate(int fd, off_t length) { 29 | if (::ftruncate(fd, length) != 0) { 30 | return ErrnoError( 31 | fmt::format( 32 | "Failed to truncate file at file descriptor '{}' to {} bytes", 33 | fd, 34 | length)); 35 | } 36 | 37 | return Nothing(); 38 | } 39 | 40 | //////////////////////////////////////////////////////////////////////// 41 | 42 | } // namespace os 43 | 44 | //////////////////////////////////////////////////////////////////////// 45 | -------------------------------------------------------------------------------- /include/stout/os/posix/getcwd.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/try.h" 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | 21 | namespace os { 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | inline std::string getcwd() { 26 | size_t size = 100; 27 | 28 | while (true) { 29 | char* temp = new char[size]; 30 | if (::getcwd(temp, size) == temp) { 31 | std::string result(temp); 32 | delete[] temp; 33 | return result; 34 | } else { 35 | if (errno != ERANGE) { 36 | delete[] temp; 37 | return std::string(); 38 | } 39 | size *= 2; 40 | delete[] temp; 41 | } 42 | } 43 | 44 | return std::string(); 45 | } 46 | 47 | //////////////////////////////////////////////////////////////////////// 48 | 49 | } // namespace os 50 | 51 | //////////////////////////////////////////////////////////////////////// 52 | -------------------------------------------------------------------------------- /include/stout/os/posix/getenv.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "stout/none.h" 20 | #include "stout/option.h" 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | namespace os { 25 | 26 | //////////////////////////////////////////////////////////////////////// 27 | 28 | // Looks in the environment variables for the specified key and 29 | // returns a string representation of its value. If no environment 30 | // variable matching key is found, None() is returned. 31 | inline Option getenv(const std::string& key) { 32 | char* value = ::getenv(key.c_str()); 33 | 34 | if (value == nullptr) { 35 | return None(); 36 | } 37 | 38 | return std::string(value); 39 | } 40 | 41 | //////////////////////////////////////////////////////////////////////// 42 | 43 | } // namespace os 44 | 45 | //////////////////////////////////////////////////////////////////////// 46 | -------------------------------------------------------------------------------- /include/stout/os/posix/kill.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | namespace os { 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | inline int kill(pid_t pid, int sig) { 25 | return ::kill(pid, sig); 26 | } 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | } // namespace os 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | -------------------------------------------------------------------------------- /include/stout/os/posix/ls.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include "stout/error.h" 23 | #include "stout/try.h" 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | namespace os { 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | inline Try> ls(const std::string& directory) { 32 | DIR* dir = opendir(directory.c_str()); 33 | 34 | if (dir == nullptr) { 35 | return ErrnoError("Failed to opendir '" + directory + "'"); 36 | } 37 | 38 | std::list result; 39 | struct dirent* entry; 40 | 41 | // Zero `errno` before starting to call `readdir`. This is necessary 42 | // to allow us to determine when `readdir` returns an error. 43 | errno = 0; 44 | 45 | while ((entry = readdir(dir)) != nullptr) { 46 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { 47 | continue; 48 | } 49 | result.push_back(entry->d_name); 50 | } 51 | 52 | if (errno != 0) { 53 | // Preserve `readdir` error. 54 | Error error = ErrnoError("Failed to read directory"); 55 | closedir(dir); 56 | return error; 57 | } 58 | 59 | if (closedir(dir) == -1) { 60 | return ErrnoError("Failed to close directory"); 61 | } 62 | 63 | return result; 64 | } 65 | 66 | //////////////////////////////////////////////////////////////////////// 67 | 68 | } // namespace os 69 | 70 | //////////////////////////////////////////////////////////////////////// 71 | -------------------------------------------------------------------------------- /include/stout/os/posix/lseek.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/os/int_fd.h" 19 | #include "stout/try.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try lseek(int_fd fd, off_t offset, int whence) { 28 | off_t result = ::lseek(fd, offset, whence); 29 | if (result < 0) { 30 | return ErrnoError(); 31 | } 32 | return result; 33 | } 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace os 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/os/posix/mkdtemp.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | #include 23 | 24 | #include "stout/error.h" 25 | #include "stout/os/temp.h" 26 | #include "stout/path.h" 27 | #include "stout/try.h" 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | namespace os { 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | 35 | // Creates a temporary directory using the specified path 36 | // template. The template may be any path with _6_ `Xs' appended to 37 | // it, for example /tmp/temp.XXXXXX. The trailing `Xs' are replaced 38 | // with a unique alphanumeric combination. 39 | inline Try mkdtemp( 40 | const std::string& path = path::join(os::temp(), "XXXXXX")) { 41 | char* temp = new char[path.size() + 1]; 42 | ::memcpy(temp, path.c_str(), path.size() + 1); 43 | 44 | if (::mkdtemp(temp) != nullptr) { 45 | std::string result(temp); 46 | delete[] temp; 47 | return result; 48 | } else { 49 | delete[] temp; 50 | return ErrnoError(); 51 | } 52 | } 53 | 54 | //////////////////////////////////////////////////////////////////////// 55 | 56 | } // namespace os 57 | 58 | //////////////////////////////////////////////////////////////////////// 59 | -------------------------------------------------------------------------------- /include/stout/os/posix/mktemp.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | #include "stout/error.h" 21 | #include "stout/os/close.h" 22 | #include "stout/os/int_fd.h" 23 | #include "stout/os/temp.h" 24 | #include "stout/path.h" 25 | #include "stout/try.h" 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | namespace os { 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | // Creates a temporary file using the specified path template. The 34 | // template may be any path with _6_ `Xs' appended to it, for example 35 | // /tmp/temp.XXXXXX. The trailing `Xs' are replaced with a unique 36 | // alphanumeric combination. 37 | inline Try mktemp( 38 | const std::string& path = path::join(os::temp(), "XXXXXX")) { 39 | char* _path = new char[path.size() + 1]; 40 | ::memcpy(_path, path.c_str(), path.size() + 1); 41 | 42 | int_fd fd = ::mkstemp(_path); 43 | std::string temp(_path); 44 | delete[] _path; 45 | 46 | if (fd < 0) { 47 | return ErrnoError(); 48 | } 49 | 50 | Try close = os::close(fd); 51 | 52 | // We propagate `close` failures if creation of the temp file was successful. 53 | if (close.isError()) { 54 | return Error("Failed to close '" + stringify(fd) + "':" + close.error()); 55 | } 56 | 57 | return temp; 58 | } 59 | 60 | //////////////////////////////////////////////////////////////////////// 61 | 62 | } // namespace os 63 | 64 | //////////////////////////////////////////////////////////////////////// 65 | -------------------------------------------------------------------------------- /include/stout/os/posix/open.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | #include "stout/error.h" 21 | #include "stout/nothing.h" 22 | #include "stout/os/close.h" 23 | #include "stout/os/fcntl.h" 24 | #include "stout/os/int_fd.h" 25 | #include "stout/try.h" 26 | 27 | #ifndef O_CLOEXEC 28 | #error "missing O_CLOEXEC support on this platform" 29 | #endif 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | namespace os { 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | inline Try open(const std::string& path, int oflag, mode_t mode = 0) { 38 | int_fd fd = ::open(path.c_str(), oflag, mode); 39 | if (fd < 0) { 40 | return ErrnoError(); 41 | } 42 | 43 | return fd; 44 | } 45 | 46 | //////////////////////////////////////////////////////////////////////// 47 | 48 | } // namespace os 49 | 50 | //////////////////////////////////////////////////////////////////////// 51 | -------------------------------------------------------------------------------- /include/stout/os/posix/pagesize.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | namespace os { 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | // The alternative `getpagesize()` is not defined by POSIX. 25 | inline size_t pagesize() { 26 | // We assume that `sysconf` will not fail in practice. 27 | long result = ::sysconf(_SC_PAGESIZE); 28 | CHECK(result >= 0); 29 | return result; 30 | } 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | 34 | } // namespace os 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | -------------------------------------------------------------------------------- /include/stout/os/posix/read.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | namespace os { 21 | 22 | // Compatibility function. On POSIX, this function is trivial, but on Windows, 23 | // we have to check whether the file descriptor is a socket or a file to read 24 | // from it. 25 | using ::read; 26 | 27 | } // namespace os 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | -------------------------------------------------------------------------------- /include/stout/os/posix/realpath.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/result.h" 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | inline Result realpath(const std::string& path) { 27 | char temp[PATH_MAX]; 28 | if (::realpath(path.c_str(), temp) == nullptr) { 29 | if (errno == ENOENT || errno == ENOTDIR) { 30 | return None(); 31 | } 32 | 33 | return ErrnoError(); 34 | } 35 | 36 | return std::string(temp); 37 | } 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | 41 | } // namespace os 42 | 43 | //////////////////////////////////////////////////////////////////////// 44 | -------------------------------------------------------------------------------- /include/stout/os/posix/rm.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | #pragma once 13 | 14 | #include 15 | 16 | #include 17 | 18 | #include "stout/error.h" 19 | #include "stout/nothing.h" 20 | #include "stout/try.h" 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | namespace os { 25 | 26 | //////////////////////////////////////////////////////////////////////// 27 | 28 | inline Try rm(const std::string& path) { 29 | if (::remove(path.c_str()) != 0) { 30 | return ErrnoError(); 31 | } 32 | 33 | return Nothing(); 34 | } 35 | 36 | //////////////////////////////////////////////////////////////////////// 37 | 38 | } // namespace os 39 | 40 | //////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /include/stout/os/posix/temp.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/os/getenv.h" 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | 21 | namespace os { 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | // Attempts to resolve the system-designated temporary directory before 26 | // back on a sensible default. On POSIX platforms, this involves checking 27 | // the POSIX-standard `TMPDIR` environment variable before falling 28 | // back to `/tmp`. 29 | inline std::string temp() { 30 | Option tmpdir = os::getenv("TMPDIR"); 31 | 32 | return tmpdir.getOrElse("/tmp"); 33 | } 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace os 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/os/posix/which.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include "stout/none.h" 19 | #include "stout/option.h" 20 | #include "stout/os.h" 21 | #include "stout/os/exists.h" 22 | #include "stout/os/permissions.h" 23 | #include "stout/path.h" 24 | #include "stout/strings.h" 25 | 26 | //////////////////////////////////////////////////////////////////////// 27 | 28 | namespace os { 29 | 30 | //////////////////////////////////////////////////////////////////////// 31 | 32 | inline Option which( 33 | const std::string& command, 34 | const Option& _path = None()) { 35 | Option path = _path; 36 | 37 | if (path.isNone()) { 38 | path = getenv("PATH"); 39 | 40 | if (path.isNone()) { 41 | return None(); 42 | } 43 | } 44 | 45 | std::vector tokens = strings::tokenize(path.get(), ":"); 46 | foreach (const std::string& token, tokens) { 47 | const std::string commandPath = path::join(token, command); 48 | if (!os::exists(commandPath)) { 49 | continue; 50 | } 51 | 52 | Try permissions = os::permissions(commandPath); 53 | if (permissions.isError()) { 54 | continue; 55 | } 56 | 57 | if (!permissions->owner.x 58 | && !permissions->group.x 59 | && !permissions->others.x) { 60 | continue; 61 | } 62 | 63 | return commandPath; 64 | } 65 | 66 | return None(); 67 | } 68 | 69 | //////////////////////////////////////////////////////////////////////// 70 | 71 | } // namespace os 72 | 73 | //////////////////////////////////////////////////////////////////////// 74 | -------------------------------------------------------------------------------- /include/stout/os/posix/write.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | namespace os { 21 | 22 | //////////////////////////////////////////////////////////////////////// 23 | 24 | // Compatibility function. On POSIX, this function is trivial, but on Windows, 25 | // we have to check whether the file descriptor is a socket or a file to write 26 | // to it. 27 | using ::write; 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | } // namespace os 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | -------------------------------------------------------------------------------- /include/stout/os/realpath.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/realpath.h" 19 | #else 20 | #include "stout/os/posix/realpath.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/rename.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | #include "stout/os/windows/rename.h" 17 | #else 18 | #include "stout/os/posix/rename.h" 19 | #endif // _WIN32 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | -------------------------------------------------------------------------------- /include/stout/os/rm.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/rm.h" 19 | #else 20 | #include "stout/os/posix/rm.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/rmdir.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/rmdir.h" 19 | #else 20 | #include "stout/os/posix/rmdir.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/sendfile.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/sendfile.h" 19 | #else 20 | #include "stout/os/posix/sendfile.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/shell.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/shell.h" 19 | #else 20 | #include "stout/os/posix/shell.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/signals.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | // 18 | // NOTE: The `os::signals` namespace is not, and will not be, 19 | // implemented on Windows. We do not throw an error error here so that 20 | // the inclusion of this header does not need to guarded; however, 21 | // uses of `os::signals` will need to be guarded. 22 | #ifndef _WIN32 23 | #include "stout/os/posix/signals.h" 24 | 25 | #define SUPPRESS(signal) \ 26 | if (os::signals::internal::Suppressor suppressor##signal = \ 27 | os::signals::internal::Suppressor(signal)) 28 | #endif // _WIN32 29 | 30 | //////////////////////////////////////////////////////////////////////// 31 | -------------------------------------------------------------------------------- /include/stout/os/socket.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/error.h" 16 | #include "stout/os/int_fd.h" 17 | #include "stout/try.h" 18 | 19 | #ifdef _WIN32 20 | #include "stout/os/windows/socket.h" 21 | #else 22 | #include "stout/os/posix/socket.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/os/stat.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/stat.h" 19 | #else 20 | #include "stout/os/posix/stat.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/su.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #ifdef _WIN32 20 | #include "stout/os/windows/su.h" 21 | #else 22 | #include "stout/os/posix/su.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/os/temp.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | #include "stout/os/windows/temp.h" 17 | #else 18 | #include "stout/os/posix/temp.h" 19 | #endif // _WIN32 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | -------------------------------------------------------------------------------- /include/stout/os/touch.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/nothing.h" 16 | #include "stout/os/close.h" 17 | #include "stout/os/exists.h" 18 | #include "stout/os/int_fd.h" 19 | #include "stout/os/open.h" 20 | #include "stout/os/utime.h" 21 | #include "stout/try.h" 22 | 23 | #ifdef _WIN32 24 | #include "stout/windows.h" 25 | #endif // _WIN32 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | namespace os { 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | inline Try touch(const std::string& path) { 34 | if (!os::exists(path)) { 35 | Try fd = os::open( 36 | path, 37 | O_RDWR | O_CREAT, 38 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 39 | 40 | if (fd.isError()) { 41 | return Error("Failed to open file: " + fd.error()); 42 | } 43 | 44 | return os::close(fd.get()); 45 | } 46 | 47 | // Update the access and modification times. 48 | return os::utime(path); 49 | } 50 | 51 | //////////////////////////////////////////////////////////////////////// 52 | 53 | } // namespace os 54 | 55 | //////////////////////////////////////////////////////////////////////// 56 | -------------------------------------------------------------------------------- /include/stout/os/utime.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #ifdef _WIN32 18 | #include 19 | #else 20 | #include 21 | #endif // _WIN32 22 | 23 | #include "stout/error.h" 24 | #include "stout/nothing.h" 25 | #include "stout/try.h" 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | namespace os { 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | // Sets the access and modification times of 'path' to the current time. 34 | inline Try utime(const std::string& path) { 35 | // NOTE: on Windows, this correctly dispatches to `_utime`, so there is no 36 | // need to create a function alias in `stout/windows.hpp` as we have done for 37 | // `stout/os/mkdir.hpp` and friends. 38 | if (::utime(path.c_str(), nullptr) == -1) { 39 | return ErrnoError(); 40 | } 41 | 42 | return Nothing(); 43 | } 44 | 45 | //////////////////////////////////////////////////////////////////////// 46 | 47 | } // namespace os 48 | 49 | //////////////////////////////////////////////////////////////////////// 50 | -------------------------------------------------------------------------------- /include/stout/os/wait.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #ifdef _WIN32 16 | // TODO(klueska): Move all `WAIT` related functions out of 17 | // `windows.hpp` and into this file. 18 | #include "stout/windows.h" 19 | #else 20 | #include 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | #ifndef W_EXITCODE 26 | #define W_EXITCODE(ret, sig) ((ret) << 8 | (sig)) 27 | #endif 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | namespace os { 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | 35 | // TODO(klueska): Add helper functions for common wait related 36 | // operations in this header file. 37 | 38 | } // namespace os 39 | 40 | //////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /include/stout/os/which.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | // For readability, we minimize the number of #ifdef blocks in the code by 16 | // splitting platform specific system calls into separate directories. 17 | #ifdef _WIN32 18 | #include "stout/os/windows/which.h" 19 | #else 20 | #include "stout/os/posix/which.h" 21 | #endif // _WIN32 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | -------------------------------------------------------------------------------- /include/stout/os/windows/bootid.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include "fmt/format.h" 19 | #include "stout/try.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try bootId() { 28 | // NOTE: We follow the precedent of the OS X design here and use the boot 29 | // time in seconds since the Unix epoch as a boot ID. See comment in 30 | // `stout/os/posix/bootid.hpp` for discussion of this approach. Note also 31 | // that we can't use milliseconds here instead of seconds because the 32 | // relatively high imprecision of millisecond resolution would cause `bootId` 33 | // to return a different number nearly every time it was called. 34 | 35 | std::chrono::milliseconds uptime = 36 | std::chrono::milliseconds(::GetTickCount64()); 37 | 38 | std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); 39 | std::chrono::system_clock::time_point boot_time = now - uptime; 40 | 41 | long long boot_time_secs = std::chrono::duration_cast( 42 | boot_time.time_since_epoch()) 43 | .count(); 44 | 45 | return fmt::format("{}", boot_time_secs); 46 | } 47 | 48 | //////////////////////////////////////////////////////////////////////// 49 | 50 | } // namespace os 51 | 52 | //////////////////////////////////////////////////////////////////////// 53 | -------------------------------------------------------------------------------- /include/stout/os/windows/chdir.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/internal/windows/longpath.h" 19 | #include "stout/nothing.h" 20 | #include "stout/try.h" 21 | #include "stout/windows.h" 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | namespace os { 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | inline Try chdir(const std::string& directory) { 30 | std::wstring longpath = ::internal::windows::longpath(directory); 31 | if (::SetCurrentDirectoryW(longpath.data()) == 0) { 32 | return WindowsError(); 33 | } 34 | 35 | return Nothing(); 36 | } 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | 40 | } // namespace os 41 | 42 | //////////////////////////////////////////////////////////////////////// 43 | -------------------------------------------------------------------------------- /include/stout/os/windows/chroot.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "stout/nothing.h" 22 | #include "stout/try.h" 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | namespace os { 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | // NOTE: `chroot` is deleted because Windows does not support POSIX `chroot` 31 | // semantics. On POSIX platforms it remains important to (e.g.) the launcher 32 | // API; passing in the `rootfs` flag, for example, will tell the launcher to 33 | // `chroot` to that directory, before launching the process. On Windows, we 34 | // simply conditionally compile out the `rootfs` flag so we can be guaranteed 35 | // to never have to invoke `chroot`. 36 | inline Try chroot(const std::string& directory) = delete; 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | 40 | } // namespace os 41 | 42 | //////////////////////////////////////////////////////////////////////// 43 | -------------------------------------------------------------------------------- /include/stout/os/windows/close.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/error.h" 16 | #include "stout/nothing.h" 17 | #include "stout/os/int_fd.h" 18 | #include "stout/os/windows/socket.h" 19 | #include "stout/try.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try close(const int_fd& fd) { 28 | switch (fd.type()) { 29 | case WindowsFD::Type::HANDLE: { 30 | if (!fd.is_valid()) { 31 | // NOTE: We return early here because 32 | // `CloseHandle(INVALID_HANDLE_VALUE)` will not return an error, but 33 | // instead (sometimes) triggers the invalid parameter handler, thus 34 | // throwing an exception. We'd rather return an error. 35 | return WindowsError(ERROR_INVALID_HANDLE); 36 | } 37 | 38 | if (::CloseHandle(fd) == FALSE) { 39 | return WindowsError(); 40 | } 41 | 42 | return Nothing(); 43 | } 44 | case WindowsFD::Type::SOCKET: { 45 | // NOTE: Since closing an unconnected socket is not an error in POSIX, 46 | // we simply ignore it here. 47 | if (::shutdown(fd, SD_BOTH) == SOCKET_ERROR 48 | && WSAGetLastError() != WSAENOTCONN) { 49 | return WindowsSocketError("Failed to shutdown a socket"); 50 | } 51 | 52 | if (::closesocket(fd) == SOCKET_ERROR) { 53 | return WindowsSocketError("Failed to close a socket"); 54 | } 55 | 56 | return Nothing(); 57 | } 58 | } 59 | 60 | UNREACHABLE(); 61 | } 62 | 63 | //////////////////////////////////////////////////////////////////////// 64 | 65 | } // namespace os 66 | 67 | //////////////////////////////////////////////////////////////////////// 68 | -------------------------------------------------------------------------------- /include/stout/os/windows/copyfile.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/internal/windows/longpath.h" 19 | #include "stout/nothing.h" 20 | #include "stout/path.h" 21 | #include "stout/try.h" 22 | #include "stout/windows.h" 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | namespace os { 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | // Uses the `CopyFile` Windows API to perform a file copy. 31 | // Unlike the POSIX implementation, we do not need to check if the 32 | // source or destination are directories, because `CopyFile` only 33 | // works on files. 34 | // 35 | // https://tinyurl.com/mrxcxzeb 36 | // 37 | // NOLINT(whitespace/line_length) 38 | inline Try copyfile( 39 | const std::string& source, 40 | const std::string& destination) { 41 | if (!path::absolute(source)) { 42 | return Error("`source` was a relative path"); 43 | } 44 | 45 | if (!path::absolute(destination)) { 46 | return Error("`destination` was a relative path"); 47 | } 48 | 49 | if (!::CopyFileW( 50 | ::internal::windows::longpath(source).data(), 51 | ::internal::windows::longpath(destination).data(), 52 | // NOTE: This allows the destination to be overwritten if the 53 | // destination already exists, as is the case in the POSIX version of 54 | // `copyfile`. 55 | false)) { 56 | return WindowsError(); 57 | } 58 | 59 | return Nothing(); 60 | } 61 | 62 | //////////////////////////////////////////////////////////////////////// 63 | 64 | } // namespace os 65 | 66 | //////////////////////////////////////////////////////////////////////// 67 | -------------------------------------------------------------------------------- /include/stout/os/windows/fork.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | namespace os { 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | 21 | struct Exec { 22 | UNIMPLEMENTED; 23 | }; 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | struct Wait {}; 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | struct Fork { 32 | UNIMPLEMENTED; 33 | }; 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | } // namespace os 38 | 39 | //////////////////////////////////////////////////////////////////////// 40 | -------------------------------------------------------------------------------- /include/stout/os/windows/fsync.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/nothing.h" 19 | #include "stout/os/int_fd.h" 20 | #include "stout/try.h" 21 | #include "stout/windows.h" 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | namespace os { 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | inline Try fsync(const int_fd& fd) { 30 | if (!::FlushFileBuffers(fd)) { 31 | return WindowsError( 32 | "os::fsync: Could not flush file buffers for given file descriptor"); 33 | } 34 | 35 | return Nothing(); 36 | } 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | 40 | } // namespace os 41 | 42 | //////////////////////////////////////////////////////////////////////// 43 | -------------------------------------------------------------------------------- /include/stout/os/windows/ftruncate.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/error.h" 16 | #include "stout/nothing.h" 17 | #include "stout/os/int_fd.h" 18 | #include "stout/try.h" 19 | #include "stout/windows.h" 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | namespace os { 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | 27 | inline Try ftruncate(const int_fd& fd, off_t length) { 28 | FILE_END_OF_FILE_INFO info; 29 | info.EndOfFile.QuadPart = length; 30 | if (::SetFileInformationByHandle( 31 | fd, 32 | FileEndOfFileInfo, 33 | &info, 34 | sizeof(info)) 35 | == FALSE) { 36 | return WindowsError(); 37 | } 38 | 39 | return Nothing(); 40 | } 41 | 42 | //////////////////////////////////////////////////////////////////////// 43 | 44 | } // namespace os 45 | 46 | //////////////////////////////////////////////////////////////////////// 47 | -------------------------------------------------------------------------------- /include/stout/os/windows/getcwd.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/check.h" 16 | #include "stout/error.h" 17 | #include "stout/internal/windows/longpath.h" 18 | // Since 'fmt' library doesn't support 'wide' conversion (e.g 19 | // from 'std::wstring' to 'std::string' and vice versa) we use 20 | // API from 'include/stout/stringify.h' (e.g: 21 | // std::string stringify(const std::wstring& wstr) - function). 22 | // Check the issue for fmt conversion on github: 23 | // https://github.com/fmtlib/fmt/issues/1116 24 | #include "stout/stringify.h" 25 | #include "stout/try.h" 26 | #include "stout/windows.h" 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | namespace os { 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | 34 | // TODO(josephw): Consider changing the return type to a `Try` 35 | // so that we do not CHECK-fail upon error. 36 | inline std::string getcwd() { 37 | // First query for the buffer size required. 38 | const DWORD length = ::GetCurrentDirectoryW(0, nullptr); 39 | CHECK(length != 0) << "Failed to retrieve current directory buffer size"; 40 | 41 | std::vector buffer; 42 | buffer.reserve(static_cast(length)); 43 | 44 | const DWORD result = ::GetCurrentDirectoryW(length, buffer.data()); 45 | CHECK(result != 0) << "Failed to determine current directory"; 46 | 47 | return strings::remove( 48 | stringify(std::wstring(buffer.data())), 49 | os::LONGPATH_PREFIX, 50 | strings::Mode::PREFIX); 51 | } 52 | 53 | //////////////////////////////////////////////////////////////////////// 54 | 55 | } // namespace os 56 | 57 | //////////////////////////////////////////////////////////////////////// 58 | -------------------------------------------------------------------------------- /include/stout/os/windows/killtree.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/os.h" 16 | #include "stout/os/windows/jobobject.h" 17 | #include "stout/try.h" 18 | #include "stout/windows.h" // For `SharedHandle` and `pid_t`. 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | // Terminate the "process tree" rooted at the specified pid. 27 | // Since there is no process tree concept on Windows, 28 | // internally this function looks up the job object for the given pid 29 | // and terminates the job. This is possible because `name_job` 30 | // provides an idempotent one-to-one mapping from pid to name. 31 | inline Try> killtree( 32 | pid_t pid, 33 | int signal, 34 | bool groups = false, 35 | bool sessions = false) { 36 | const Try name = os::name_job(pid); 37 | if (name.isError()) { 38 | return Error("Failed to determine job object name: " + name.error()); 39 | } 40 | 41 | Try handle = 42 | os::open_job(JOB_OBJECT_TERMINATE, false, name.get()); 43 | if (handle.isError()) { 44 | return Error("Failed to open job object: " + handle.error()); 45 | } 46 | 47 | Try killJobResult = os::kill_job(handle.get()); 48 | if (killJobResult.isError()) { 49 | return Error("Failed to delete job object: " + killJobResult.error()); 50 | } 51 | 52 | // NOTE: This return value is unused. A future refactor 53 | // may change the return type to `Try`. 54 | std::list process_tree_list; 55 | return process_tree_list; 56 | } 57 | 58 | //////////////////////////////////////////////////////////////////////// 59 | 60 | } // namespace os 61 | 62 | //////////////////////////////////////////////////////////////////////// 63 | -------------------------------------------------------------------------------- /include/stout/os/windows/lseek.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/error.h" 16 | #include "stout/os/int_fd.h" 17 | #include "stout/try.h" 18 | #include "stout/windows.h" 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | inline Try lseek(int_fd fd, off_t offset, int whence) { 27 | // NOTE: The values for `SEEK_SET`, `SEEK_CUR`, and `SEEK_END` are 28 | // 0, 1, 2, the same as `FILE_BEGIN`, `FILE_CURRENT`, and 29 | // `FILE_END`. Thus we don't need to map them, and they can be 30 | // casted to a `DWORD` safely. 31 | 32 | LARGE_INTEGER offset_; 33 | offset_.QuadPart = offset; 34 | 35 | LARGE_INTEGER new_offset; 36 | 37 | // TODO(andschwa): This may need to be synchronized if users aren't 38 | // careful about sharing their file handles among threads. 39 | const BOOL result = 40 | ::SetFilePointerEx(fd, offset_, &new_offset, static_cast(whence)); 41 | 42 | if (result == FALSE) { 43 | return WindowsError(); 44 | } 45 | 46 | return static_cast(new_offset.QuadPart); 47 | } 48 | 49 | //////////////////////////////////////////////////////////////////////// 50 | 51 | } // namespace os 52 | 53 | //////////////////////////////////////////////////////////////////////// 54 | -------------------------------------------------------------------------------- /include/stout/os/windows/lsof.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/os/int_fd.h" 18 | #include "stout/try.h" 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | namespace os { 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | inline Try> lsof() = delete; 27 | 28 | //////////////////////////////////////////////////////////////////////// 29 | 30 | } // namespace os 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | -------------------------------------------------------------------------------- /include/stout/os/windows/pagesize.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include "stout/windows.h" 16 | 17 | //////////////////////////////////////////////////////////////////////// 18 | 19 | namespace os { 20 | 21 | //////////////////////////////////////////////////////////////////////// 22 | 23 | inline size_t pagesize() { 24 | SYSTEM_INFO info; 25 | ::GetSystemInfo(&info); 26 | return static_cast(info.dwPageSize); 27 | } 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | } // namespace os 32 | 33 | //////////////////////////////////////////////////////////////////////// 34 | -------------------------------------------------------------------------------- /include/stout/os/windows/rename.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | 17 | #include "stout/error.h" 18 | #include "stout/internal/windows/longpath.h" 19 | #include "stout/nothing.h" 20 | #include "stout/try.h" 21 | #include "stout/windows.h" 22 | 23 | //////////////////////////////////////////////////////////////////////// 24 | 25 | namespace os { 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | inline Try rename( 30 | const std::string& from, 31 | const std::string& to, 32 | bool sync = false) { 33 | // Use `MoveFile` to perform the file move. The MSVCRT implementation of 34 | // `::rename` fails if the `to` file already exists[1], while some UNIX 35 | // implementations allow that[2]. 36 | // 37 | // Use `MOVEFILE_COPY_ALLOWED` to allow moving the file to another volume and 38 | // `MOVEFILE_REPLACE_EXISTING` to comply with the UNIX implementation and 39 | // replace an existing file[3]. 40 | // 41 | // [1] https://msdn.microsoft.com/en-us/library/zw5t957f.aspx 42 | // [2] http://man7.org/linux/man-pages/man2/rename.2.html 43 | // [3] https://tinyurl.com/2p8dvxk8 44 | const BOOL result = ::MoveFileExW( 45 | ::internal::windows::longpath(from).data(), 46 | ::internal::windows::longpath(to).data(), 47 | MOVEFILE_COPY_ALLOWED 48 | | MOVEFILE_REPLACE_EXISTING | (sync ? MOVEFILE_WRITE_THROUGH : 0)); 49 | 50 | if (!result) { 51 | return WindowsError( 52 | "`os::rename` failed to move file '" + from + "' to '" + to + "'"); 53 | } 54 | 55 | return Nothing(); 56 | } 57 | 58 | //////////////////////////////////////////////////////////////////////// 59 | 60 | } // namespace os 61 | 62 | //////////////////////////////////////////////////////////////////////// 63 | -------------------------------------------------------------------------------- /include/stout/os/windows/temp.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | // Since 'fmt' library doesn't support 'wide' conversion (e.g 19 | // from 'std::wstring' to 'std::string' and vice versa) we use 20 | // API from 'include/stout/stringify.h' (e.g: 21 | // std::string stringify(const std::wstring& wstr) - function). 22 | // Check the issue for fmt conversion on github: 23 | // https://github.com/fmtlib/fmt/issues/1116 24 | #include "stout/stringify.h" 25 | #include "stout/windows.h" 26 | 27 | //////////////////////////////////////////////////////////////////////// 28 | 29 | namespace os { 30 | 31 | //////////////////////////////////////////////////////////////////////// 32 | 33 | // Attempts to resolve the system-designated temporary directory before 34 | // falling back to a sensible default. On Windows, this involves checking 35 | // (in this order) environment variables for `TMP`, `TEMP`, and `USERPROFILE` 36 | // followed by the Windows directory (`::GetTimePath`). In the unlikely event 37 | // where none of these are found, this function returns the current directory. 38 | inline std::string temp() { 39 | const size_t size = static_cast(MAX_PATH) + 2; 40 | std::vector buffer; 41 | buffer.reserve(size); 42 | if (::GetTempPathW(static_cast(size), buffer.data()) == 0) { 43 | // Failed, use current directory. 44 | if (::GetCurrentDirectoryW(static_cast(size), buffer.data()) == 0) { 45 | // Failed, use relative path. 46 | return "."; 47 | } 48 | } 49 | 50 | return stringify(std::wstring(buffer.data())); 51 | } 52 | 53 | //////////////////////////////////////////////////////////////////////// 54 | 55 | } // namespace os 56 | 57 | //////////////////////////////////////////////////////////////////////// 58 | -------------------------------------------------------------------------------- /include/stout/os/windows/xattr.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | namespace os { 18 | 19 | //////////////////////////////////////////////////////////////////////// 20 | 21 | // NOTE: These functions are deleted because Windows does 22 | // not support POSIX extended attribute semantics. 23 | inline Try setxattr( 24 | const std::string& path, 25 | const std::string& name, 26 | const std::string& value, 27 | int flags) = delete; 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | inline Try getxattr( 32 | const std::string& path, 33 | const std::string& name) = delete; 34 | 35 | //////////////////////////////////////////////////////////////////////// 36 | 37 | inline Try removexattr( 38 | const std::string& path, 39 | const std::string& name) = delete; 40 | 41 | //////////////////////////////////////////////////////////////////////// 42 | 43 | } // namespace os 44 | 45 | //////////////////////////////////////////////////////////////////////// 46 | -------------------------------------------------------------------------------- /include/stout/os/xattr.h: -------------------------------------------------------------------------------- 1 | // Licensed to the Apache Software Foundation (ASF) under one 2 | // or more contributor license agreements. See the NOTICE file 3 | // distributed with this work for additional information 4 | // regarding copyright ownership. The ASF licenses this file 5 | // to you under the Apache License, Version 2.0 (the 6 | // "License"); you may not use this file except in compliance 7 | // with the License. You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | #pragma once 18 | 19 | #ifdef _WIN32 20 | #include "stout/os/windows/xattr.h" 21 | #else 22 | #include "stout/os/posix/xattr.h" 23 | #endif // _WIN32 24 | 25 | //////////////////////////////////////////////////////////////////////// 26 | -------------------------------------------------------------------------------- /include/stout/preprocessor.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | 26 | // Provides aliases to a bunch of preprocessor macros useful for 27 | // creating template definitions that have varying number of 28 | // parameters (should be removable with C++-11 variadic templates). 29 | 30 | #define CAT BOOST_PP_CAT 31 | #define INC BOOST_PP_INC 32 | #define INTERCEPT BOOST_PP_INTERCEPT 33 | #define ENUM_PARAMS BOOST_PP_ENUM_PARAMS 34 | #define ENUM_BINARY_PARAMS BOOST_PP_ENUM_BINARY_PARAMS 35 | #define ENUM BOOST_PP_ENUM 36 | #define ENUM_TRAILING_PARAMS BOOST_PP_ENUM_TRAILING_PARAMS 37 | #define REPEAT BOOST_PP_REPEAT 38 | #define REPEAT_FROM_TO BOOST_PP_REPEAT_FROM_TO 39 | 40 | //////////////////////////////////////////////////////////////////////// 41 | -------------------------------------------------------------------------------- /include/stout/set.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include // For std::set_intersection and std::set_difference. 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | template 21 | std::set operator|(const std::set& left, const std::set& right) { 22 | // Note, we're not using 'set_union' since it affords us no benefit 23 | // in efficiency and is more complicated to use given we have sets. 24 | std::set result = left; 25 | result.insert(right.begin(), right.end()); 26 | return result; 27 | } 28 | 29 | //////////////////////////////////////////////////////////////////////// 30 | 31 | template 32 | std::set operator+(const std::set& left, const T& t) { 33 | std::set result = left; 34 | result.insert(t); 35 | return result; 36 | } 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | 40 | template 41 | std::set operator&(const std::set& left, const std::set& right) { 42 | std::set result; 43 | std::set_intersection( 44 | left.begin(), 45 | left.end(), 46 | right.begin(), 47 | right.end(), 48 | std::inserter(result, result.begin())); 49 | return result; 50 | } 51 | 52 | //////////////////////////////////////////////////////////////////////// 53 | 54 | template 55 | std::set operator-(const std::set& left, const std::set& right) { 56 | std::set result; 57 | std::set_difference( 58 | left.begin(), 59 | left.end(), 60 | right.begin(), 61 | right.end(), 62 | std::inserter(result, result.begin())); 63 | return result; 64 | } 65 | 66 | //////////////////////////////////////////////////////////////////////// 67 | -------------------------------------------------------------------------------- /include/stout/some.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | //////////////////////////////////////////////////////////////////////// 19 | 20 | // A useful type that can be used to represent an Option or Result. 21 | // 22 | // Examples: 23 | // 24 | // Result result = Some(42); 25 | // Option = Some("hello world"); 26 | // 27 | // void foo(Option o) {} 28 | // 29 | // foo(Some("hello world")); 30 | 31 | // NOTE: We use an intermediate type, _Some, so that one doesn't need 32 | // to explicitly define the template type when doing 'Some(value)'. 33 | template 34 | struct _Some { 35 | _Some(T _t) 36 | : t(std::move(_t)) {} 37 | 38 | T t; 39 | }; 40 | 41 | //////////////////////////////////////////////////////////////////////// 42 | 43 | template 44 | _Some::type> Some(T&& t) { 45 | return _Some::type>(std::forward(t)); 46 | } 47 | 48 | //////////////////////////////////////////////////////////////////////// 49 | -------------------------------------------------------------------------------- /include/stout/thread.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | //////////////////////////////////////////////////////////////////////// 6 | 7 | namespace stout { 8 | 9 | //////////////////////////////////////////////////////////////////////// 10 | 11 | namespace this_thread { 12 | 13 | //////////////////////////////////////////////////////////////////////// 14 | 15 | inline void pause() { 16 | #if (defined(_M_X64) || defined(__x86_64__)) && !defined(_MSC_VER) 17 | __asm__ volatile("pause" 18 | : 19 | : 20 | : "memory"); 21 | #elif (defined(_M_X64) || defined(__x86_64__)) && defined(_MSC_VER) 22 | // On this architecture `__asm__` , `__asm` , `asm` are not recognized. 23 | // We use `_mm_pause()` and `_ReadWriteBarrier()` instead. See the link 24 | // below: 25 | // https://github.com/facebook/folly/blob/main/folly/portability/Asm.h 26 | ::_mm_pause(); 27 | ::_ReadWriteBarrier(); 28 | #elif defined(_M_IX86) || defined(__i386__) || defined(__i386) 29 | __asm__ volatile("pause" 30 | : 31 | : 32 | : "memory"); 33 | #else 34 | std::this_thread::yield(); 35 | #endif 36 | } 37 | 38 | //////////////////////////////////////////////////////////////////////// 39 | 40 | } // namespace this_thread 41 | 42 | //////////////////////////////////////////////////////////////////////// 43 | 44 | } // namespace stout 45 | 46 | //////////////////////////////////////////////////////////////////////// 47 | -------------------------------------------------------------------------------- /include/stout/traits.h: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | 13 | #pragma once 14 | 15 | //////////////////////////////////////////////////////////////////////// 16 | 17 | template class C> 18 | struct is_specialization_of : std::false_type {}; 19 | 20 | //////////////////////////////////////////////////////////////////////// 21 | 22 | template