60 #include <sys/types.h> 75 struct dirent **files = {0};
81 printf(
"dlt_offline_trace_storage_dir_info: Invalid parameter(s)");
85 cnt = scandir(path, &files,
NULL, alphasort);
92 for (i = 0; i < cnt; i++)
95 len = strlen(file_name);
96 if ((strncmp(files[i]->d_name, file_name, len) == 0) && (files[i]->d_name[len] ==
'.'))
100 if (tmp_old ==
NULL || (strlen(tmp_old) >= strlen(files[i]->d_name)))
104 tmp_old = files[i]->d_name;
107 else if (strlen(tmp_old) > strlen(files[i]->d_name))
109 tmp_old = files[i]->d_name;
113 if (strcmp(tmp_old, files[i]->d_name) > 0)
114 tmp_old = files[i]->d_name;
118 if (tmp_new ==
NULL || (strlen(tmp_new) <= strlen(files[i]->d_name)))
122 tmp_new = files[i]->d_name;
125 else if (strlen(tmp_new) < strlen(files[i]->d_name))
127 tmp_new = files[i]->d_name;
131 if (strcmp(tmp_new, files[i]->d_name) < 0)
133 tmp_new = files[i]->d_name;
159 for (i = 0; i < cnt; i++)
171 sprintf(file_index,
"%010u", idx);
177 strncat(log_file_name, file_index,
sizeof(file_index));
183 const char d[2] =
".";
185 unsigned int idx = 0;
192 token = strtok(file, d);
194 token = strtok(
NULL, d);
198 idx = strtol(token,
NULL, 10);
214 unsigned int idx = 0;
223 printf(
"dlt_offline_trace_create_new_file: pointer to tmp is NULL!");
226 if (strftime(outstr,
sizeof(outstr),
"%Y%m%d_%H%M%S", tmp) == 0) {
228 ret = snprintf(trace->
filename, NAME_MAX ,
"%s/dlt_offlinetrace_%s.dlt", trace->
directory, outstr);
229 if ((ret < 0) || (ret >= NAME_MAX))
231 printf(
"dlt_offlinetrace filename cannot be concatenated\n");
244 if ((ret < 0) || (ret >= NAME_MAX))
246 printf(
"filename cannot be concatenated\n");
253 trace->
ohandle = open(trace->
filename,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
257 printf(
"Offline trace file %s cannot be created\n",trace->
filename);
267 unsigned long size = 0;
272 while ((dp=readdir(dir)) !=
NULL) {
275 int res = snprintf(filename,
sizeof(filename),
"%s/%s",trace->
directory,dp->d_name);
279 if( (
unsigned int)res<
sizeof(filename) && res>0 )
281 if(0 == stat(filename,&status))
283 size += status.st_size;
286 printf(
"Offline trace file %s cannot be stat-ed",filename);
303 char filename[PATH_MAX+1];
304 char filename_oldest[PATH_MAX+1];
305 unsigned long size_oldest = 0;
307 time_t time_oldest = 0;
310 filename_oldest[0] = 0;
314 while ((dp=readdir(dir)) !=
NULL) {
316 int res = snprintf(filename,
sizeof(filename),
"%s/%s",trace->
directory,dp->d_name);
320 if( (
unsigned int)res<
sizeof(filename) && res>0 )
322 if(0 == stat(filename,&status))
324 if(time_oldest == 0 || status.st_mtime < time_oldest) {
325 time_oldest = status.st_mtime;
326 size_oldest = status.st_size;
327 strncpy(filename_oldest,filename,PATH_MAX);
328 filename_oldest[PATH_MAX]=0;
332 printf(
"Old offline trace file %s cannot be stat-ed",filename);
339 if(filename_oldest[0]) {
340 if(
remove(filename_oldest)) {
341 printf(
"Remove file %s failed!\n",filename_oldest);
346 printf(
"No file to be removed!\n");
359 if (stat(trace->
directory, &status) == -1)
361 dlt_vlog(LOG_ERR,
"Offline trace directory: %s doesn't exist \n", trace->
directory);
366 else if(access(trace->
directory, W_OK) != 0)
368 dlt_vlog(LOG_ERR,
"Offline trace directory: %s doesn't have the write access \n", trace->
directory);
387 strncpy(trace->
directory,directory,NAME_MAX);
404 if((lseek(trace->
ohandle,0,SEEK_CUR)+size1+size2+size3)>=trace->
fileSize)
418 if(data1 && (trace->
ohandle >= 0)) {
419 if(write(trace->
ohandle,data1,size1)!=size1) {
420 printf(
"Offline trace write failed!\n");
424 if(data2 && (trace->
ohandle >= 0)) {
425 if(write(trace->
ohandle,data2,size2)!=size2) {
426 printf(
"Offline trace write failed!\n");
430 if(data3 && (trace->
ohandle >= 0)) {
431 if(write(trace->
ohandle,data3,size3)!=size3) {
432 printf(
"Offline trace write failed!\n");
DltReturnValue dlt_offline_trace_free(DltOfflineTrace *trace)
#define DLT_OFFLINETRACE_FILENAME_BASE
#define DLT_OFFLINETRACE_FILENAME_TO_COMPARE
DltReturnValue dlt_offline_trace_check_size(DltOfflineTrace *trace)
unsigned int dlt_offline_trace_get_idx_of_log_file(char *file)
DltReturnValue dlt_offline_trace_write(DltOfflineTrace *trace, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
char filename[NAME_MAX+1]
#define DLT_OFFLINETRACE_FILENAME_EXT
unsigned int dlt_offline_trace_storage_dir_info(char *path, char *file_name, char *newest, char *oldest)
int dlt_offline_trace_delete_oldest_file(DltOfflineTrace *trace)
DltReturnValue dlt_offline_trace_create_new_file(DltOfflineTrace *trace)
#define DLT_OFFLINETRACE_FILENAME_MAX_SIZE
unsigned long dlt_offline_trace_get_total_size(DltOfflineTrace *trace)
#define DLT_OFFLINETRACE_FILENAME_DELI
void dlt_offline_trace_file_name(char *log_file_name, char *name, unsigned int idx)
int filenameTimestampBased
char directory[NAME_MAX+1]
DltReturnValue dlt_vlog(int prio, const char *format,...)
DltReturnValue dlt_offline_trace_init(DltOfflineTrace *trace, const char *directory, int fileSize, int maxSize, int filenameTimestampBased)