2 * upgrade-apply-deltafs
4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include <sys/types.h>
29 #define LOG_SIZE_OPT_PATH "/opt/data/recovery/.ua_log_size"
30 #define DEF_MAX_LOG_SIZE (2*1024*1024)
31 #define MAX_FILE_PATH 512
35 long backup_offset = 0;
36 long max_logfile_size = DEF_MAX_LOG_SIZE;
38 /*-----------------------------------------------------------------------------
40 ----------------------------------------------------------------------------*/
41 static long __check_existence(const char *file_path)
44 char filename[MAX_FILE_PATH];
46 if (strncpy(filename, file_path, strlen(file_path) + 1) == NULL)
48 if (stat(filename, &statbuf)) {
52 return statbuf.st_size;
55 /*-----------------------------------------------------------------------------
57 ----------------------------------------------------------------------------*/
58 static int __read_from_file(const char *path, char *buf, size_t size)
69 fd = open(path, O_RDONLY, 0);
73 count = read(fd, buf, size);
75 count = (count < (ssize_t)size) ? count : ((ssize_t)size - 1);
76 while (count > 0 && buf[count - 1] == '\n')
88 /*-----------------------------------------------------------------------------
90 ----------------------------------------------------------------------------*/
91 static int get_opt_logfile_size(void)
94 if status file does not exist, status = UP_START_NONE.
95 if status file exist, read status from file.
99 if (__check_existence(LOG_SIZE_OPT_PATH) == 0)
102 if (__read_from_file(LOG_SIZE_OPT_PATH, buf, sizeof(buf)) < 0)
108 /*-----------------------------------------------------------------------------
110 ----------------------------------------------------------------------------*/
111 void set_max_logfile_size(void)
113 int size = get_opt_logfile_size();
116 size = DEF_MAX_LOG_SIZE;
118 max_logfile_size = size;
121 /*-----------------------------------------------------------------------------
123 ----------------------------------------------------------------------------*/
124 int log_printf(FILE* log_fp, char* format_str, ...)
132 va_start(list, format_str);
133 vsnprintf(log_str, sizeof(log_str), format_str, list);
136 len = strlen(log_str);
137 next_offset = curr_offset + len;
139 if (next_offset <= max_logfile_size) {
140 if (fprintf(log_fp, "%s", log_str) < 0) {
144 curr_offset = next_offset;
145 if (curr_offset == max_logfile_size) {
150 backup_offset = max_logfile_size - curr_offset;
151 backup_ch = log_str[backup_offset];
152 log_str[backup_offset] = 0x00;
153 if (fprintf(log_fp, "%s", log_str) < 0) {
158 log_str[backup_offset] = backup_ch;
159 if (fprintf(log_fp, "%s", log_str+backup_offset) < 0) {
163 curr_offset = next_offset - max_logfile_size;