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 <mem/mem.h>
17 #include "os/file.h"
18
19 namespace panda::os::unix::native_stack {
20
ReadOsFile(const std::string & file_name,std::string * result)21 bool ReadOsFile(const std::string &file_name, std::string *result)
22 {
23 panda::os::unix::file::File cmdfile = panda::os::file::Open(file_name, panda::os::file::Mode::READONLY);
24 panda::os::file::FileHolder fholder(cmdfile);
25 constexpr size_t BUFF_SIZE = 8_KB;
26 std::vector<char> buffer(BUFF_SIZE);
27 auto res = cmdfile.Read(&buffer[0], buffer.size());
28 if (res) {
29 result->append(&buffer[0], res.Value());
30 return true;
31 }
32 return false;
33 }
34
WriterOsFile(const void * buffer,size_t count,int fd)35 bool WriterOsFile(const void *buffer, size_t count, int fd)
36 {
37 panda::os::unix::file::File myfile(fd);
38 panda::os::file::FileHolder fholder(myfile);
39 return myfile.WriteAll(buffer, count);
40 }
41
42 } // namespace panda::os::unix::native_stack
43