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 20 namespace panda::os::file { 21 GetFlags(Mode mode)22static int GetFlags(Mode mode) 23 { 24 switch (mode) { 25 case Mode::READONLY: 26 return O_RDONLY; 27 28 case Mode::READWRITE: 29 return O_RDWR; 30 31 case Mode::WRITEONLY: 32 return O_WRONLY | O_CREAT | O_TRUNC; // NOLINT(hicpp-signed-bitwise) 33 34 case Mode::READWRITECREATE: 35 return O_RDWR | O_CREAT; // NOLINT(hicpp-signed-bitwise) 36 37 default: 38 break; 39 } 40 41 UNREACHABLE(); 42 } 43 Open(std::string_view filename,Mode mode)44File Open(std::string_view filename, Mode mode) 45 { 46 // NOLINTNEXTLINE(hicpp-signed-bitwise) 47 const auto PERM = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; 48 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) 49 return File(open(filename.data(), GetFlags(mode), PERM)); 50 } 51 52 } // namespace panda::os::file 53