Add toolchain and mbtk source
Change-Id: Ie12546301367ea59240bf23d5e184ad7e36e40b3
diff --git a/mbtk/mbtk_logd/common.c b/mbtk/mbtk_logd/common.c
new file mode 100755
index 0000000..1e293b6
--- /dev/null
+++ b/mbtk/mbtk_logd/common.c
@@ -0,0 +1,282 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <time.h>
+#include <unistd.h>
+#include <time.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include "log_config.h"
+
+/* 文件大小和修改时间 */
+static int get_file_size_time(const char *filename)
+{
+ struct stat statbuf;
+ /* 判断未打开文件 */
+ if(stat(filename,&statbuf)==-1)
+ {
+ printf("Get stat on %s Error: %s\n", filename, strerror(errno));
+ return(-1);
+ }
+ if(S_ISDIR(statbuf.st_mode)) // 目录
+ return(1);
+ if(S_ISREG(statbuf.st_mode)) // 文件
+ printf("%s size: %ld bytes\tmodified at %s", filename, statbuf.st_size, ctime(&statbuf.st_mtime));
+ return(0);
+}
+
+int __main(int argc,char **argv)
+{
+ DIR *dirp;
+ struct dirent *direntp;
+ int stats;
+
+ if(argc!=2)
+ {
+ printf("Usage: %s filename\n\a", argv[0]);
+ exit(1);
+ }
+
+ if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1)) // 文件或出现错误
+ exit(1);
+
+ /* 打开目录 */
+ if((dirp=opendir(argv[1]))==NULL)
+ {
+ printf("Open Directory %s Error: %s\n", argv[1], strerror(errno));
+ exit(1);
+ }
+
+ /* 返回目录中文件大小和修改时间 */
+ while((direntp=readdir(dirp))!=NULL)
+ {
+ /* 给文件或目录名添加路径:argv[1]+"/"+direntp->d_name */
+ char dirbuf[512];
+ memset(dirbuf,0,sizeof(dirbuf));
+ strcpy(dirbuf,argv[1]);
+ strcat(dirbuf,"/");
+ strcat(dirbuf,direntp->d_name);
+ if(get_file_size_time(dirbuf)==-1) break;
+ }
+
+ closedir(dirp);
+ exit(1);
+}
+// 从后往前找
+char *strstr_tail(const char *dst, const char *src)
+{
+ if(NULL == dst || NULL == src)
+ return NULL;
+ const char *pdst = dst;
+ const char *psrc = src;
+ char *tmp = NULL ;
+ while (*pdst) //因为要从后向前找,则首先让pdst指向'\0'
+ {
+ pdst++;
+ }
+
+ while (pdst >= dst ) //当pdst大于dst则表明dst这个字符串还没有找完
+ {
+ if (tmp=(char *)strstr(pdst, psrc = src)) //使用strstr帮助寻找,找到保存到tmp
+ return tmp;
+
+ pdst--;
+ }
+
+ return NULL ;
+}
+
+char* strcpy_malloc(char* src)
+{
+ if (NULL == src || 0 == strlen(src)) {
+ return NULL;
+ }
+ int len = strlen(src);
+ char* temp = malloc(len + 1);
+ if (NULL == temp) {
+ return NULL;
+ }
+ memset(temp, 0, len + 1);
+ strcpy(temp, src);
+
+ return temp;
+}
+
+int get_time (char *out_time)
+{
+ time_t rawtime;
+ struct tm *info;
+ char buffer[16] = {0};
+
+ time( &rawtime );
+ info = localtime(&rawtime);
+ strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S \n", info);
+ if(out_time)
+ memcpy(out_time, &buffer[2], 12);
+
+ return(0);
+}
+
+int tcp_connect(char* ip, int port)
+{
+ int sockfd;
+ struct hostent* he;
+ struct sockaddr_in server;
+
+ he = gethostbyname(ip);
+
+ if (he == NULL) {
+ printf("gethostbyname error\n");
+ exit(1);
+ }
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ if (sockfd == -1) {
+ printf("socket error\n");
+ exit(1);
+ }
+
+ bzero(&server, sizeof(server));
+ server.sin_family = AF_INET;
+ server.sin_port = htons(port);
+ server.sin_addr = *((struct in_addr*)he->h_addr);
+
+ if (connect(sockfd, (struct sockaddr*)&server, sizeof(server)) == -1) {
+ printf("connect error\n");
+ exit(1);
+ }
+
+ return sockfd;
+}
+int movefile(char *src, char *dst)
+{
+ if (strcmp(src, dst) == 0)
+ {
+ printf("Destination file name and source file name can't be the same!");
+ return -1;
+ }
+ FILE *fsrc = fopen(src, "rb"); //一定要“rb”否则复制的文件有时会不能使用
+ FILE *fdst = fopen(dst, "wb"); //一定要“wb”否则复制的文件有时会不能使用
+ int buflen = 1024;
+ char buf[1024];
+ int nread = 0;
+ while ((nread = fread(buf, 1, buflen, fsrc)) > 0) //最好把读文件的代码放这里
+ {
+ fwrite(buf, 1, nread, fdst);
+ }
+
+ fclose(fsrc);
+ fsrc = NULL;
+ fclose(fdst);
+ fdst = NULL;
+ remove(src);//删除源文件
+
+ return 0;
+}
+/**
+ * @brief get_rotate_file
+ *
+ * @details 转移log文件
+ *
+ * @param param
+ *
+ * @return return type
+ */
+int get_rotate_file(int fd, char *base_file, struct file_list_t *_file_list)
+{
+ char _time[16] = {0};
+ int num = 0;
+ char tmp_buf[48] = {0};
+ char *current_file = _file_list->file[_file_list->current];
+ int len = strlen(base_file);
+ char *old = malloc(len + 20);
+ int rotate_file_count_max;
+
+ if(_file_list->total == -1 || _file_list->total == 0 || _file_list->total > ROTATE_FILE_COUNT_MAX)
+ rotate_file_count_max = ROTATE_FILE_COUNT_MAX;
+ else
+ rotate_file_count_max = _file_list->total;
+
+
+ if(0 == _file_list->current && NULL == current_file)
+ _file_list->current = rotate_file_count_max - 1;
+ if(NULL == old)
+ {
+ fprintf(stderr, "failed to malloc %s\n", strerror(errno));
+ exit(-1);
+ }
+ close(fd);
+ sprintf(tmp_buf, "/tmp/log%s", strstr_tail(base_file, "/"));
+ get_time(_time);
+ sprintf(old, "%s.%s", base_file, _time);
+ if (current_file && strstr(current_file, old)) {
+ int tmp_len = strlen(current_file);
+ // 以时间命名的文件是否已经存在
+ if(current_file[tmp_len - 3] == '.'){
+ num = atoi(¤t_file[tmp_len - 2]);
+ num++;
+ sprintf(old, "%s.%02d", old, num);
+ }else{
+ sprintf(old, "%s.%02d", old, num);
+ }
+ }
+ if (movefile(tmp_buf, old) != 0){
+ printf("%s %d: Renamed error ,%s to %s.\n", __FUNCTION__, __LINE__, tmp_buf, old);
+ }
+ _file_list->current++;
+ // ringbuf
+ if(_file_list->current > (rotate_file_count_max - 1))
+ {
+ _file_list->current = 0;
+ }
+ // 如果之前已经存在,先释放之前存放的资源
+ if(_file_list->file[_file_list->current])
+ {
+ // printf("%s %d: remove %s\n", __FUNCTION__, __LINE__, _file_list->file[_file_list->current]);
+ // 如果 不限制文件数目,则不删除文件
+ if(_file_list->total != -1)
+ unlink(_file_list->file[_file_list->current]);
+ free(_file_list->file[_file_list->current]);
+ }
+ // 将文件名保存下来
+ _file_list->file[_file_list->current] = strdup(old);
+ free(old);
+ int fd_new = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
+ if (fd_new < 0) {
+ fprintf(stderr, "failed to open %s: %s\n", tmp_buf, strerror(errno));
+ exit(-1);
+ }
+
+ return fd_new;
+}
+
+#if 0
+void get_rotate_file_test()
+{
+ struct file_list_t file_list;
+ int i;
+ char *test_path = "/tmp/log/m_log.txt";
+
+ memset(&file_list, 0, sizeof(struct file_list_t));
+ file_list.total = 3;
+
+ int fd = open(test_path, O_CREAT | O_WRONLY | O_APPEND, 0600);
+ if (fd < 0) {
+ fprintf(stderr, "failed to open %s: %s\n", test_path, strerror(errno));
+ exit(-1);
+ }
+ write(fd, "test1111", 8);
+ for (i = 0; i < 6; ++i) {
+ // sleep(1);
+ fd = get_rotate_file(fd, test_path, &file_list);
+ write(fd, "test2222", 8);
+ }
+ close(fd);
+}
+#endif