-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilsEx.cpp
160 lines (144 loc) · 5.17 KB
/
UtilsEx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Created by Spreadst
*/
/* write string to file */
#ifdef UMS
#include <cutils/properties.h>
status_t ReadFile(const std::string& file, char* buf, int buf_size) {
int fd;
if ((fd = open(file.c_str(), O_RDONLY)) < 0) {
PLOG(ERROR) << StringPrintf(" unable to open file (%s)", file.c_str());
return -EIO;
}
int readRes = OK;
readRes = read(fd, buf, buf_size);
LOG(VERBOSE) << StringPrintf(" read file(%s) content (%s)", file.c_str(), buf);
if(readRes < 0) {
PLOG(ERROR) << StringPrintf(" unable to read file (%s)", file.c_str());
close(fd);
return -EIO;
}
close(fd);
return OK;
}
bool getSupportUms() {
return false;
char support_ums[PROPERTY_VALUE_MAX];
property_get("persist.sys.usb.support_ums", support_ums, "");
if(!strcmp(support_ums, "true"))
return true;
return false;
}
#endif
status_t WriteToFile(const std::string& preMsg, const std::string& file, const std::string& str, const char byte) {
int fd;
if ((fd = open(file.c_str(), O_WRONLY)) < 0) {
PLOG(ERROR) << preMsg << StringPrintf(" unable to open file (%s)", file.c_str());
return -EIO;
}
int writeRes = OK;
if (str.empty()) {
LOG(VERBOSE) << StringPrintf(" write byte num %d to file \'%s\'", byte, file.c_str());
writeRes = write(fd, &byte, 1);
} else {
LOG(VERBOSE) << StringPrintf(" write string \'%s\' to file \'%s\'", str.c_str(), file.c_str());
writeRes = write(fd, str.c_str(), str.length());
}
if (writeRes < 0) {
PLOG(ERROR) << preMsg << StringPrintf(" unable to write file (%s)", file.c_str());
close(fd);
return -EIO;
}
close(fd);
return OK;
}
/* @} */
status_t ForceUnmount(const std::string& path) {
const char* cpath = path.c_str();
LOG(ERROR)<<"Begin umount2" << path;
if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
LOG(ERROR)<<"umount2 ok";
return OK;
}
/* SPRD: replace storage path to /storage @{ */
LOG(WARNING) << "Failed to unmount " << path;
std::string storage_path = path;
std::string path_prefix = "/mnt/runtime/default";
std::size_t pos = storage_path.find(path_prefix);
LOG(WARNING) << "storage_path1= " + storage_path << ", pos=" << pos;
if (pos != std::string::npos) {
storage_path = storage_path.replace(pos, path_prefix.length(), "/storage");
}
path_prefix = "/mnt/runtime/write";
pos = storage_path.find(path_prefix);
LOG(WARNING) << "storage_path2= " + storage_path << ", pos=" << pos;
if (pos != std::string::npos) {
storage_path = storage_path.replace(pos, path_prefix.length(), "/storage");
}
const char* cpath2 = storage_path.c_str();
/* @} */
// Apps might still be handling eject request, so wait before
// we start sending signals
/* SPRD: replace storage path to /storage and add for unmount sdcard performance @{
*/
if (sSleepOnUnmount) sleep(1);
KillProcessesWithOpenFiles(cpath2, SIGINT);
if (sSleepOnUnmount) sleep(1);
if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
return OK;
}
LOG(WARNING) << "After SIGINT. Failed to unmount " << path;
KillProcessesWithOpenFiles(cpath2, SIGTERM);
if (sSleepOnUnmount) sleep(1);
if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
return OK;
}
LOG(WARNING) << "After SIGTERM. Failed to unmount " << path;
KillProcessesWithOpenFiles(cpath2, SIGKILL);
if (sSleepOnUnmount) sleep(5);
if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
return OK;
}
LOG(WARNING) << "After SIGKILL. Failed to unmount " << path;
KillProcessesWithOpenFiles(cpath2, SIGKILL);
if (sSleepOnUnmount) sleep(5);
if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
return OK;
}
LOG(WARNING) << "After Second SIGKILL. Failed to unmount " << path;
/* SPRD: Lazy umount @{ */
if (sSleepOnUnmount) sleep(7);
if (!umount2(cpath, UMOUNT_NOFOLLOW|MNT_DETACH) || errno == EINVAL || errno == ENOENT) {
PLOG(WARNING) << "Success lazy unmount " << path;
return OK;
}
LOG(ERROR) << "Failed to Lazy unmount " << path;
/* @} */
return -errno;
}
/* SPRD: add for storage @{ */
/* create symlink */
status_t CreateSymlink(const std::string& source, const std::string& target) {
status_t res = 0;
if(!remove(target.c_str())) {
LOG(INFO) << "delete symlink " << target << "sucess";
} else {
LOG(INFO) << "delete symlink " << target << "fail:" << strerror(errno);
}
LOG(INFO) << "create symlink " << target << "->" << source;
if (symlink(source.c_str(), target.c_str()) < 0) {
PLOG(ERROR)<< "Failed to create symlink " << target << "->" << source;
res = -errno;
}
return res;
}
/* delete symlink */
status_t DeleteSymlink(const std::string& path) {
status_t res = 0;
LOG(INFO) << "delete symlink " << path;
if (remove(path.c_str()) < 0) {
PLOG(ERROR)<< "Failed to delete symlink " << path;
res = -errno;
}
return res;
}