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