-
Notifications
You must be signed in to change notification settings - Fork 318
Enable file compression on file rotation #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0c23e92
ce4dc32
2808f00
4bcd6cc
c65afc3
b134687
f8cc172
8253e26
2478992
c860d92
8e18bcd
7229988
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -372,7 +372,8 @@ int dlt_logstorage_storage_dir_info(DltLogStorageUserConfig *file_config, | |
| config->records = NULL; | ||
| } | ||
|
|
||
| char *suffix = config->gzip_compression == DLT_LOGSTORAGE_GZIP_ON ? ".dlt.gz" : ".dlt"; | ||
| char *suffix = ".dlt.gz"; | ||
| char *gzsuffix = ".dlt"; | ||
|
|
||
| for (i = 0; i < cnt; i++) { | ||
| int len = 0; | ||
|
|
@@ -385,10 +386,13 @@ int dlt_logstorage_storage_dir_info(DltLogStorageUserConfig *file_config, | |
| if (config->num_files == 1 && file_config->logfile_optional_counter) { | ||
| /* <filename>.dlt or <filename>_<tmsp>.dlt */ | ||
| if ((files[i]->d_name[len] == suffix[0]) || | ||
| (files[i]->d_name[len] == gzsuffix[0]) || | ||
| (file_config->logfile_timestamp && | ||
| (files[i]->d_name[len] == file_config->logfile_delimiter))) { | ||
| (files[i]->d_name[len] == | ||
| file_config->logfile_delimiter))) { | ||
| current_idx = 1; | ||
| } else { | ||
| } | ||
| else { | ||
| continue; | ||
| } | ||
| } else { | ||
|
|
@@ -732,6 +736,29 @@ int dlt_logstorage_open_log_file(DltLogStorageFilterConfig *config, | |
| } | ||
| } | ||
|
|
||
| #ifdef DLT_LOGSTORAGE_USE_GZIP | ||
| if (config->gzip_compression == DLT_LOGSTORAGE_GZIP_FILE) { | ||
| tmp = &config->records; | ||
| while ((*tmp)->next != NULL) { | ||
| int len = strlen((*tmp)->name); | ||
| const char *suffix = ".dlt"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need more context/info/description about this feature:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (strcmp(&(*tmp)->name[len - strlen(suffix)], suffix) == | ||
| 0) { | ||
| memset(absolute_file_path, 0, | ||
| sizeof(absolute_file_path) / sizeof(char)); | ||
| strcat(absolute_file_path, storage_path); | ||
| strncat(absolute_file_path, (*tmp)->name, len); | ||
| dlt_vlog(LOG_INFO, | ||
| "%s: Compressing '%s' (num_log_files: %d, " | ||
| "file_name:%s)\n", | ||
| __func__, absolute_file_path, num_log_files, | ||
| (*tmp)->name); | ||
| dlt_logstorage_compress_dlt_file(absolute_file_path); | ||
| } | ||
| tmp = &(*tmp)->next; | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -761,6 +788,114 @@ int dlt_logstorage_open_log_file(DltLogStorageFilterConfig *config, | |
| return ret; | ||
| } | ||
|
|
||
| #ifdef DLT_LOGSTORAGE_USE_GZIP | ||
| /** | ||
| * dlt_logstorage_compress_dlt_file | ||
| * | ||
| * Compress content of a file and remove the original file. | ||
| * compressed file name is the same as the original with .gz extension. | ||
| * | ||
| * @param file_path The file to compress | ||
| * @return 0 on success, -1 on error | ||
| */ | ||
| int dlt_logstorage_compress_dlt_file(char *file_path) | ||
| { | ||
| char file_path_dest[DLT_OFFLINE_LOGSTORAGE_MAX_PATH_LEN + 1] = {'\0'}; | ||
| strcat(file_path_dest, file_path); | ||
| strcat(file_path_dest, ".gz"); | ||
|
|
||
| FILE *source = fopen(file_path, "rb"); | ||
| if (source == NULL) { | ||
| dlt_vlog(LOG_ERR, "%s: could not open %s\n", __func__, file_path); | ||
| return -1; | ||
| } | ||
|
|
||
| FILE *dest = fopen(file_path_dest, "wb"); | ||
| if (dest == NULL) { | ||
| dlt_vlog(LOG_ERR, "%s: could not open %s\n", __func__, file_path_dest); | ||
| fclose(source); | ||
| return -1; | ||
| } | ||
|
|
||
| gzFile gzfile = gzdopen(fileno(dest), "wb"); | ||
| if (dest == NULL) { | ||
| dlt_vlog(LOG_ERR, "%s: could not gz open %s\n", __func__, | ||
| file_path_dest); | ||
| fclose(source); | ||
| fclose(dest); | ||
| return -1; | ||
| } | ||
|
|
||
| int ret = dlt_logstorage_compress_fd(source, gzfile); | ||
|
|
||
| fclose(source); | ||
| gzclose(gzfile); | ||
| fclose(dest); | ||
|
|
||
| if (ret == 0 && remove(file_path) != 0) { | ||
| dlt_vlog(LOG_ERR, "%s: could not remove original file %s \n", __func__, | ||
| file_path); | ||
| return -1; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| #endif | ||
|
|
||
| #ifdef DLT_LOGSTORAGE_USE_GZIP | ||
| /** | ||
| * dlt_logstorage_compress_fd | ||
| * | ||
| * Compress content of a file descriptor into a gzFile. | ||
| * | ||
| * @param source The file to compress | ||
| * @param dest The file to write to | ||
| * @return 0 on success, -1 on error | ||
| */ | ||
| int dlt_logstorage_compress_fd(FILE *source, gzFile dest) | ||
| { | ||
| int ret, flush; | ||
| z_stream strm; | ||
| unsigned char in[COMPRESS_CHUNK]; | ||
|
|
||
| /* allocate deflate state */ | ||
| strm.zalloc = Z_NULL; | ||
| strm.zfree = Z_NULL; | ||
| strm.opaque = Z_NULL; | ||
| ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); | ||
| if (ret != Z_OK) | ||
| return ret; | ||
|
|
||
| /* compress until end of file */ | ||
| do { | ||
| strm.avail_in = fread(in, 1, COMPRESS_CHUNK, source); | ||
| if (ferror(source)) { | ||
| (void)deflateEnd(&strm); | ||
| dlt_vlog(LOG_ERR, "%s: Can't open source\n", __func__); | ||
| return -1; | ||
| } | ||
|
|
||
| ret = gzwrite(dest, in, strm.avail_in); | ||
| if (ret == 0) { | ||
| (void)deflateEnd(&strm); | ||
| dlt_vlog(LOG_ERR, "%s: failed to write to log file\n", __func__); | ||
| return -1; | ||
| } | ||
| if (gzflush(dest, Z_SYNC_FLUSH) != 0) | ||
| dlt_vlog(LOG_ERR, "%s: failed to gzflush log file\n", __func__); | ||
|
|
||
| flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; | ||
| strm.next_in = in; | ||
|
|
||
| // done when last data in file processed | ||
| } while (flush != Z_FINISH); | ||
|
|
||
| // clean up and return | ||
| (void)deflateEnd(&strm); | ||
| return 0; | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * dlt_logstorage_find_dlt_header | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.