blob: 5b2de92d4f81d577372c6c82ce2def270704b1f7 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <sys/socket.h>
5#include <netdb.h>
6#include <fcntl.h>
7#include <time.h>
8#include <unistd.h>
9#include <time.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <string.h>
13#include <dirent.h>
14#include "log_config.h"
15
16/* 文件大小和修改时间 */
17static int get_file_size_time(const char *filename)
18{
19 struct stat statbuf;
20 /* 判断未打开文件 */
21 if(stat(filename,&statbuf)==-1)
22 {
23 printf("Get stat on %s Error: %s\n", filename, strerror(errno));
24 return(-1);
25 }
26 if(S_ISDIR(statbuf.st_mode)) // 目录
27 return(1);
28 if(S_ISREG(statbuf.st_mode)) // 文件
29 printf("%s size: %ld bytes\tmodified at %s", filename, statbuf.st_size, ctime(&statbuf.st_mtime));
30 return(0);
31}
32
33int __main(int argc,char **argv)
34{
35 DIR *dirp;
36 struct dirent *direntp;
37 int stats;
38
39 if(argc!=2)
40 {
41 printf("Usage: %s filename\n\a", argv[0]);
42 exit(1);
43 }
44
45 if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1)) // 文件或出现错误
46 exit(1);
47
48 /* 打开目录 */
49 if((dirp=opendir(argv[1]))==NULL)
50 {
51 printf("Open Directory %s Error: %s\n", argv[1], strerror(errno));
52 exit(1);
53 }
54
55 /* 返回目录中文件大小和修改时间 */
56 while((direntp=readdir(dirp))!=NULL)
57 {
58 /* 给文件或目录名添加路径:argv[1]+"/"+direntp->d_name */
59 char dirbuf[512];
60 memset(dirbuf,0,sizeof(dirbuf));
61 strcpy(dirbuf,argv[1]);
62 strcat(dirbuf,"/");
63 strcat(dirbuf,direntp->d_name);
64 if(get_file_size_time(dirbuf)==-1) break;
65 }
66
67 closedir(dirp);
68 exit(1);
69}
70// 从后往前找
71char *strstr_tail(const char *dst, const char *src)
72{
73 if(NULL == dst || NULL == src)
74 return NULL;
75 const char *pdst = dst;
76 const char *psrc = src;
77 char *tmp = NULL ;
78 while (*pdst) //因为要从后向前找,则首先让pdst指向'\0'
79 {
80 pdst++;
81 }
82
83 while (pdst >= dst ) //当pdst大于dst则表明dst这个字符串还没有找完
84 {
85 if (tmp=(char *)strstr(pdst, psrc = src)) //使用strstr帮助寻找,找到保存到tmp
86 return tmp;
87
88 pdst--;
89 }
90
91 return NULL ;
92}
93
94char* strcpy_malloc(char* src)
95{
96 if (NULL == src || 0 == strlen(src)) {
97 return NULL;
98 }
99 int len = strlen(src);
100 char* temp = malloc(len + 1);
101 if (NULL == temp) {
102 return NULL;
103 }
104 memset(temp, 0, len + 1);
105 strcpy(temp, src);
106
107 return temp;
108}
109
110int get_time (char *out_time)
111{
112 time_t rawtime;
113 struct tm *info;
114 char buffer[16] = {0};
115
116 time( &rawtime );
117 info = localtime(&rawtime);
118 strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S \n", info);
119 if(out_time)
120 memcpy(out_time, &buffer[2], 12);
121
122 return(0);
123}
124
125int tcp_connect(char* ip, int port)
126{
127 int sockfd;
128 struct hostent* he;
129 struct sockaddr_in server;
130
131 he = gethostbyname(ip);
132
133 if (he == NULL) {
134 printf("gethostbyname error\n");
135 exit(1);
136 }
137
138 sockfd = socket(AF_INET, SOCK_STREAM, 0);
139
140 if (sockfd == -1) {
141 printf("socket error\n");
142 exit(1);
143 }
144
145 bzero(&server, sizeof(server));
146 server.sin_family = AF_INET;
147 server.sin_port = htons(port);
148 server.sin_addr = *((struct in_addr*)he->h_addr);
149
150 if (connect(sockfd, (struct sockaddr*)&server, sizeof(server)) == -1) {
151 printf("connect error\n");
152 exit(1);
153 }
154
155 return sockfd;
156}
157int movefile(char *src, char *dst)
158{
159 if (strcmp(src, dst) == 0)
160 {
161 printf("Destination file name and source file name can't be the same!");
162 return -1;
163 }
164 FILE *fsrc = fopen(src, "rb"); //一定要“rb”否则复制的文件有时会不能使用
165 FILE *fdst = fopen(dst, "wb"); //一定要“wb”否则复制的文件有时会不能使用
166 int buflen = 1024;
167 char buf[1024];
168 int nread = 0;
169 while ((nread = fread(buf, 1, buflen, fsrc)) > 0) //最好把读文件的代码放这里
170 {
171 fwrite(buf, 1, nread, fdst);
172 }
173
174 fclose(fsrc);
175 fsrc = NULL;
176 fclose(fdst);
177 fdst = NULL;
178 remove(src);//删除源文件
179
180 return 0;
181}
182/**
183 * @brief get_rotate_file
184 *
185 * @details 转移log文件
186 *
187 * @param param
188 *
189 * @return return type
190 */
191int get_rotate_file(int fd, char *base_file, struct file_list_t *_file_list)
192{
193 char _time[16] = {0};
194 int num = 0;
195 char tmp_buf[48] = {0};
196 char *current_file = _file_list->file[_file_list->current];
197 int len = strlen(base_file);
198 char *old = malloc(len + 20);
199 int rotate_file_count_max;
200
201 if(_file_list->total == -1 || _file_list->total == 0 || _file_list->total > ROTATE_FILE_COUNT_MAX)
202 rotate_file_count_max = ROTATE_FILE_COUNT_MAX;
203 else
204 rotate_file_count_max = _file_list->total;
205
206
207 if(0 == _file_list->current && NULL == current_file)
208 _file_list->current = rotate_file_count_max - 1;
209 if(NULL == old)
210 {
211 fprintf(stderr, "failed to malloc %s\n", strerror(errno));
212 exit(-1);
213 }
214 close(fd);
215 sprintf(tmp_buf, "/tmp/log%s", strstr_tail(base_file, "/"));
216 get_time(_time);
217 sprintf(old, "%s.%s", base_file, _time);
218 if (current_file && strstr(current_file, old)) {
219 int tmp_len = strlen(current_file);
220 // 以时间命名的文件是否已经存在
221 if(current_file[tmp_len - 3] == '.'){
222 num = atoi(&current_file[tmp_len - 2]);
223 num++;
224 sprintf(old, "%s.%02d", old, num);
225 }else{
226 sprintf(old, "%s.%02d", old, num);
227 }
228 }
xf.li44e08692024-01-30 01:54:44 -0800229 printf("MBTK_LOGD: tmp_buf is %s, old is %s\n", tmp_buf, old);
liubin281ac462023-07-19 14:22:54 +0800230 if (movefile(tmp_buf, old) != 0){
231 printf("%s %d: Renamed error ,%s to %s.\n", __FUNCTION__, __LINE__, tmp_buf, old);
232 }
233 _file_list->current++;
234 // ringbuf
235 if(_file_list->current > (rotate_file_count_max - 1))
236 {
237 _file_list->current = 0;
238 }
239 // 如果之前已经存在,先释放之前存放的资源
240 if(_file_list->file[_file_list->current])
241 {
242 // printf("%s %d: remove %s\n", __FUNCTION__, __LINE__, _file_list->file[_file_list->current]);
243 // 如果 不限制文件数目,则不删除文件
244 if(_file_list->total != -1)
245 unlink(_file_list->file[_file_list->current]);
246 free(_file_list->file[_file_list->current]);
247 }
248 // 将文件名保存下来
249 _file_list->file[_file_list->current] = strdup(old);
250 free(old);
251 int fd_new = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
252 if (fd_new < 0) {
253 fprintf(stderr, "failed to open %s: %s\n", tmp_buf, strerror(errno));
254 exit(-1);
255 }
256
257 return fd_new;
258}
259
260#if 0
261void get_rotate_file_test()
262{
263 struct file_list_t file_list;
264 int i;
265 char *test_path = "/tmp/log/m_log.txt";
266
267 memset(&file_list, 0, sizeof(struct file_list_t));
268 file_list.total = 3;
269
270 int fd = open(test_path, O_CREAT | O_WRONLY | O_APPEND, 0600);
271 if (fd < 0) {
272 fprintf(stderr, "failed to open %s: %s\n", test_path, strerror(errno));
273 exit(-1);
274 }
275 write(fd, "test1111", 8);
276 for (i = 0; i < 6; ++i) {
277 // sleep(1);
278 fd = get_rotate_file(fd, test_path, &file_list);
279 write(fd, "test2222", 8);
280 }
281 close(fd);
282}
283#endif