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