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