blob: 36a283fab4b58fb71718e8416d31d2c4cd05934b
1 | #include "CFile.h" |
2 | #include "CTvLog.h" |
3 | |
4 | #include <stdlib.h> |
5 | #ifdef LOG_TAG |
6 | #undef LOG_TAG |
7 | #define LOG_TAG "CFILE" |
8 | #endif |
9 | |
10 | CFile::CFile() |
11 | { |
12 | mPath[0] = '\0'; |
13 | mFd = -1; |
14 | } |
15 | |
16 | CFile::~CFile() |
17 | { |
18 | closeFile(); |
19 | } |
20 | |
21 | CFile::CFile(const char *path) |
22 | { |
23 | strcpy(mPath, path); |
24 | mFd = -1; |
25 | } |
26 | |
27 | int CFile::openFile(const char *path) |
28 | { |
29 | LOGD("openFile = %s", path); |
30 | |
31 | if (mFd < 0) { |
32 | if (path == NULL) { |
33 | if (strlen(mPath) <= 0) |
34 | return -1; |
35 | |
36 | mFd = open(mPath, O_RDWR);//读写模式打开 |
37 | if (mFd < 0) LOGD("open file(--%s) fail", mPath); |
38 | } else { |
39 | mFd = open(path, O_RDWR);//读写模式打开 |
40 | LOGD("open file(%s fd=%d) ", path, mFd); |
41 | strcpy(mPath, path); |
42 | } |
43 | } |
44 | |
45 | return mFd; |
46 | } |
47 | |
48 | int CFile::closeFile() |
49 | { |
50 | if (mFd > 0) { |
51 | close(mFd); |
52 | mFd = -1; |
53 | } |
54 | return 0; |
55 | } |
56 | |
57 | int CFile::writeFile(const unsigned char *pData, int uLen) |
58 | { |
59 | int ret = -1; |
60 | if (mFd > 0) |
61 | ret = write(mFd, pData, uLen); |
62 | |
63 | return ret; |
64 | } |
65 | |
66 | int CFile::readFile(void *pBuf, int uLen) |
67 | { |
68 | int ret = 0; |
69 | if (mFd > 0) |
70 | { |
71 | ret = read(mFd, pBuf, uLen); |
72 | } |
73 | return ret; |
74 | } |
75 | |
76 | int CFile::copyTo(const char *dstPath) |
77 | { |
78 | if (strlen(mPath) <= 0) |
79 | return -1; |
80 | int dstFd; |
81 | if (mFd == -1) { |
82 | if ((mFd = open(mPath, O_RDONLY)) == -1) { |
83 | LOGE("Open %s Error:%s/n", mPath, strerror(errno)); |
84 | return -1; |
85 | } |
86 | } |
87 | |
88 | if ((dstFd = open(dstPath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) { |
89 | LOGE("Open %s Error:%s/n", dstPath, strerror(errno)); |
90 | } |
91 | |
92 | int bytes_read, bytes_write; |
93 | char buffer[BUFFER_SIZE]; |
94 | char *ptr; |
95 | int ret = 0; |
96 | while (bytes_read = read(mFd, buffer, BUFFER_SIZE)) { |
97 | /* 一个致命的错误发生了 */ |
98 | if ((bytes_read == -1) && (errno != EINTR)) { |
99 | ret = -1; |
100 | break; |
101 | } else if (bytes_read > 0) { |
102 | ptr = buffer; |
103 | while (bytes_write = write(dstFd, ptr, bytes_read)) { |
104 | /* 一个致命错误发生了 */ |
105 | if ((bytes_write == -1) && (errno != EINTR)) { |
106 | ret = -1; |
107 | break; |
108 | } |
109 | /* 写完了所有读的字节 */ |
110 | else if (bytes_write == bytes_read) { |
111 | ret = 0; |
112 | break; |
113 | } |
114 | /* 只写了一部分,继续写 */ |
115 | else if (bytes_write > 0) { |
116 | ptr += bytes_write; |
117 | bytes_read -= bytes_write; |
118 | } |
119 | } |
120 | /* 写的时候发生的致命错误 */ |
121 | if (bytes_write == -1) { |
122 | ret = -1; |
123 | break; |
124 | } |
125 | } |
126 | } |
127 | fsync(dstFd); |
128 | close(dstFd); |
129 | return ret; |
130 | } |
131 | |
132 | |
133 | int CFile::delFile(const char *path) |
134 | { |
135 | if (strlen(path) <= 0) return -1; |
136 | if (unlink(path) != 0) { |
137 | LOGD("delete file(%s) err=%s", path, strerror(errno)); |
138 | return -1; |
139 | } |
140 | return 0; |
141 | } |
142 | |
143 | int CFile::delFile() |
144 | { |
145 | if (strlen(mPath) <= 0) return -1; |
146 | if (unlink(mPath) != 0) { |
147 | LOGD("delete file(%s) err=%s", mPath, strerror(errno)); |
148 | return -1; |
149 | } |
150 | return 0; |
151 | } |
152 | |
153 | |
154 | int CFile::getFileAttrValue(const char *path) |
155 | { |
156 | int value; |
157 | |
158 | int fd = open(path, O_RDONLY); |
159 | if (fd <= 0) { |
160 | LOGE("open (%s)ERROR!!error = -%s- \n", path, strerror ( errno )); |
161 | } |
162 | char s[8]; |
163 | read(fd, s, sizeof(s)); |
164 | close(fd); |
165 | value = atoi(s); |
166 | return value; |
167 | } |
168 | |
169 | int CFile::setFileAttrValue(const char *path, int value) |
170 | { |
171 | FILE *fp = fopen ( path, "w" ); |
172 | |
173 | if ( fp == NULL ) { |
174 | LOGW ( "Open %s error(%s)!\n", path, strerror ( errno ) ); |
175 | return -1; |
176 | } |
177 | fprintf ( fp, "%d", value ); |
178 | fclose ( fp ); |
179 | return 0; |
180 | } |
181 | |
182 | int CFile::getFileAttrStr(const char *path, char *str) |
183 | { |
184 | return 0; |
185 | } |
186 | |
187 | int CFile::setFileAttrStr(const char *path, const char *str) |
188 | { |
189 | FILE *fp = fopen ( path, "w" ); |
190 | |
191 | if ( fp == NULL ) { |
192 | LOGW ( "Open %s error(%s)!\n", path, strerror ( errno ) ); |
193 | return -1; |
194 | } |
195 | fprintf ( fp, "%s", str ); |
196 | fclose ( fp ); |
197 | return 0; |
198 | } |
199 |