• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dirent.h>
21 #include <fcntl.h>
22 #include <fstream>
23 #include <iostream>
24 #include <iterator>
25 #include <pthread.h>
26 #include <sys/prctl.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 
30 #include <sys/syscall.h>
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace FileUtil {
35 using namespace std;
36 
IncludeTrailingPathDelimiter(const std::string & path)37 std::string IncludeTrailingPathDelimiter(const std::string& path)
38 {
39     return "";
40 }
41 
ExcludeTrailingPathDelimiter(const std::string & path)42 std::string ExcludeTrailingPathDelimiter(const std::string& path)
43 {
44     return "";
45 }
46 
ChangeModeFile(const string & fileName,const mode_t & mode)47 bool ChangeModeFile(const string& fileName, const mode_t& mode)
48 {
49     if (access(fileName.c_str(), F_OK) != 0) {
50         return false;
51     }
52 
53     return ChangeMode(fileName, mode);
54 }
55 
Umask(const mode_t & mode)56 mode_t Umask(const mode_t& mode)
57 {
58     return umask(mode);
59 }
60 
Open(const std::string & path,const int flags,const mode_t mode)61 int Open(const std::string& path, const int flags, const mode_t mode)
62 {
63     return open(path.c_str(), flags, mode);
64 }
65 
CreateDirWithDefaultPerm(const std::string & path,uid_t aidRoot,uid_t aidSystem)66 void CreateDirWithDefaultPerm(const std::string& path, uid_t aidRoot, uid_t aidSystem)
67 {
68     FileUtil::ForceCreateDirectory(path);
69     chown(path.c_str(), aidRoot, aidSystem);
70 }
71 
WriteBufferToFd(int fd,const char * buffer,size_t size)72 bool WriteBufferToFd(int fd, const char* buffer, size_t size)
73 {
74     if (fd < 0) {
75         return false;
76     }
77 
78     if (buffer == nullptr) {
79         return false;
80     }
81 
82     ssize_t writeSize = size;
83     if (writeSize != TEMP_FAILURE_RETRY(write(fd, buffer, size))) {
84         return false;
85     }
86 
87     return true;
88 }
89 
SaveStringToFd(int fd,const std::string & content)90 bool SaveStringToFd(int fd, const std::string& content)
91 {
92     if (fd <= 0) {
93         return false;
94     }
95 
96     if (content.empty()) {
97         return true;
98     }
99 
100     const long len = write(fd, content.c_str(), content.length());
101     if (len < 0) {
102         return false;
103     }
104 
105     if ((unsigned long)len != content.length()) {
106         return false;
107     }
108 
109     return true;
110 }
111 
RenameFile(const std::string & src,const std::string & dest)112 bool RenameFile(const std::string& src, const std::string& dest)
113 {
114     if (std::rename(src.c_str(), dest.c_str()) == 0) {
115         return true;
116     }
117     return false;
118 }
119 } // namespace FileUtil
120 } // namespace HiviewDFX
121 } // namespace OHOS