blob: 000e680093aacf305ca9ce6dfeeafe708fe152aa [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 {
b.liu0f7ffad2024-11-06 19:57:27 +080085 if ((tmp=(char *)strstr(pdst, psrc = src))) //使用strstr帮助寻找,找到保存到tmp
liubin281ac462023-07-19 14:22:54 +080086 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{
b.liu0f7ffad2024-11-06 19:57:27 +0800193 char old_name[32] = {0};
l.yangb7972c72024-10-15 22:34:51 -0700194 char new_name[32] = {0};
liubin281ac462023-07-19 14:22:54 +0800195
b.liu0f7ffad2024-11-06 19:57:27 +0800196
l.yangb7972c72024-10-15 22:34:51 -0700197 int rotate_file_count_max = 0;
b.liu0f7ffad2024-11-06 19:57:27 +0800198
liubin281ac462023-07-19 14:22:54 +0800199 if(_file_list->total == -1 || _file_list->total == 0 || _file_list->total > ROTATE_FILE_COUNT_MAX)
l.yangb7972c72024-10-15 22:34:51 -0700200 {
liubin281ac462023-07-19 14:22:54 +0800201 rotate_file_count_max = ROTATE_FILE_COUNT_MAX;
l.yangb7972c72024-10-15 22:34:51 -0700202 }
liubin281ac462023-07-19 14:22:54 +0800203 else
l.yangb7972c72024-10-15 22:34:51 -0700204 {
liubin281ac462023-07-19 14:22:54 +0800205 rotate_file_count_max = _file_list->total;
liubin281ac462023-07-19 14:22:54 +0800206 }
b.liu0f7ffad2024-11-06 19:57:27 +0800207
208
l.yangb7972c72024-10-15 22:34:51 -0700209 snprintf(new_name, sizeof(new_name), "%s.%d", base_file, rotate_file_count_max - 1);
210 remove(new_name);
211
b.liu0f7ffad2024-11-06 19:57:27 +0800212 for (int i = rotate_file_count_max - 1; i >= 0; i--)
l.yangb7972c72024-10-15 22:34:51 -0700213 {
b.liu0f7ffad2024-11-06 19:57:27 +0800214 if (i == 0)
l.yangb7972c72024-10-15 22:34:51 -0700215 {
216 snprintf(old_name, sizeof(old_name), "%s", base_file);
b.liu0f7ffad2024-11-06 19:57:27 +0800217 }
218 else
l.yangb7972c72024-10-15 22:34:51 -0700219 {
220 snprintf(old_name, sizeof(old_name), "%s.%d", base_file, i - 1);
liubin281ac462023-07-19 14:22:54 +0800221 }
l.yangb7972c72024-10-15 22:34:51 -0700222 snprintf(new_name, sizeof(new_name), "%s.%d", base_file, i);
223 rename(old_name, new_name);
liubin281ac462023-07-19 14:22:54 +0800224 }
225
l.yangb7972c72024-10-15 22:34:51 -0700226 close(fd);
227 int fd_new = open(base_file, O_CREAT | O_WRONLY | O_APPEND, 0600);
b.liu0f7ffad2024-11-06 19:57:27 +0800228 if (fd_new < 0)
l.yangb7972c72024-10-15 22:34:51 -0700229 {
230 fprintf(stderr, "failed to open %s: %s\n", base_file, strerror(errno));
231 exit(-1);
232 }
liubin281ac462023-07-19 14:22:54 +0800233 return fd_new;
234}
235
236#if 0
237void get_rotate_file_test()
238{
239 struct file_list_t file_list;
240 int i;
241 char *test_path = "/tmp/log/m_log.txt";
242
243 memset(&file_list, 0, sizeof(struct file_list_t));
244 file_list.total = 3;
245
246 int fd = open(test_path, O_CREAT | O_WRONLY | O_APPEND, 0600);
247 if (fd < 0) {
248 fprintf(stderr, "failed to open %s: %s\n", test_path, strerror(errno));
249 exit(-1);
250 }
251 write(fd, "test1111", 8);
252 for (i = 0; i < 6; ++i) {
253 // sleep(1);
254 fd = get_rotate_file(fd, test_path, &file_list);
255 write(fd, "test2222", 8);
256 }
257 close(fd);
258}
259#endif