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 "file_util.h"
17 #include <algorithm>
18 #include <cstdio>
19 #include <cstring>
20 #include <filesystem>
21 #include <fstream>
22 #include <iostream>
23 #include <iterator>
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace FileUtil {
28 using namespace std;
29 namespace fs = std::filesystem;
30 constexpr int PATH_MAX = 4096;
31 constexpr int MODE_MAX = 07777;
32 constexpr int MAX_FILE_LENGTH = 32 * 1024 * 1024; // 32MB
33
SaveStringToFd(int fd,const std::string & content)34 bool SaveStringToFd(int fd, const std::string& content)
35 {
36 return true;
37 }
38
IncludeTrailingPathDelimiter(const std::string & path)39 std::string IncludeTrailingPathDelimiter(const std::string& path)
40 {
41 return "";
42 }
43
ExcludeTrailingPathDelimiter(const std::string & path)44 std::string ExcludeTrailingPathDelimiter(const std::string& path)
45 {
46 return "";
47 }
48
49
Umask(const mode_t & mode)50 mode_t Umask(const mode_t& mode)
51 {
52 return mode;
53 }
54
Open(const std::string & path,const int flags,const mode_t mode)55 int Open(const std::string& path, const int flags, const mode_t mode)
56 {
57 const mode_t defaultMode = 0;
58 return open(path.c_str(), flags, defaultMode);
59 }
60
CreateDirWithDefaultPerm(const std::string & path,uid_t aidRoot,uid_t aidSystem)61 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aidSystem)
62 {
63 FileUtil::ForceCreateDirectory(path);
64 }
65
FormatPath2UnixStyle(std::string & path)66 void FormatPath2UnixStyle(std::string &path)
67 {
68 replace(path.begin(), path.end(), '\\', '/');
69 }
70
RemoveFolderBeginWith(const std::string & path,const std::string & folderName)71 void RemoveFolderBeginWith(const std::string &path, const std::string &folderName)
72 {
73 if (!fs::exists(path)) {
74 return;
75 }
76
77 for (auto& cur : fs::recursive_directory_iterator(path)) {
78 if (cur.is_directory() &&
79 strncmp(cur.path().filename().string().c_str(), folderName.c_str(), folderName.length()) == 0) {
80 ForceRemoveDirectory(cur.path().string());
81 }
82 }
83 }
84
WriteBufferToFd(int fd,const char * buffer,size_t size)85 bool WriteBufferToFd(int fd, const char* buffer, size_t size)
86 {
87 return true;
88 }
89
RenameFile(const std::string & src,const std::string & dest)90 bool RenameFile(const std::string& src, const std::string& dest)
91 {
92 if (std::rename(src.c_str(), dest.c_str()) == 0) {
93 return true;
94 }
95 return false;
96 }
97 } // namespace FileUtil
98 } // namespace HiviewDFX
99 } // namespace OHOS