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