1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "os/file.h" 17 18 #include <fcntl.h> 19 #include <string_view> 20 21 #include "macros.h" 22 23 namespace panda::os::file { 24 GetFlags(Mode mode)25static int GetFlags(Mode mode) 26 { 27 switch (mode) { 28 case Mode::READONLY: 29 return O_RDONLY; 30 31 case Mode::READWRITE: 32 return O_RDWR; 33 34 case Mode::WRITEONLY: 35 return O_WRONLY | O_CREAT | O_TRUNC; // NOLINT(hicpp-signed-bitwise) 36 37 case Mode::READWRITECREATE: 38 return O_RDWR | O_CREAT; // NOLINT(hicpp-signed-bitwise) 39 40 default: 41 break; 42 } 43 44 UNREACHABLE(); 45 } 46 Open(std::string_view filename,Mode mode)47File Open(std::string_view filename, Mode mode) 48 { 49 // NOLINTNEXTLINE(hicpp-signed-bitwise) 50 const auto PERM = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; 51 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) 52 return File(open(filename.data(), GetFlags(mode), PERM)); 53 } 54 55 } // namespace panda::os::file 56