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