57 #include <sys/inotify.h> 72 #define INOTIFY_SZ (sizeof(struct inotify_event)) 73 #define INOTIFY_LEN (INOTIFY_SZ + NAME_MAX + 1) 75 #define Z_CHUNK_SZ 1024*128 76 #define COMPRESS_EXTENSION ".gz" 77 #define SUBDIR_COMPRESS ".tocompress" 78 #define SUBDIR_TOSEND ".tosend" 99 char *origin_name(
char *src){
100 if (strlen( (
char*) basename(src)) > 10 ){
101 return (
char*)(basename(src)+10);
105 DLT_STRING(
"dlt-system-filetransfer, error in recreating origin name!"));
113 DLT_STRING(
"dlt-system-filetransfer, creating unique temporary file name."));
114 time_t t = time(
NULL);
121 char *basename_f = basename(src);
123 int len = 11+strlen(basename_f);
126 DLT_STRING(
"dlt-system-filetransfer, unique name creation needs to shorten the filename:"),
DLT_STRING(basename_f));
130 char *ret = malloc(len);
133 snprintf(ret, len,
"%010" PRIu32
"%s", l,basename_f);
147 char *fn = origin_name(dst_tosend);
155 while(lastpkg < pkgcount)
160 while((total-used) < (total/2))
180 DLT_STRING(
"dlt-system-filetransfer, sent dumped file"));
199 snprintf(dst_mode,8,
"wb%d", level);
204 dst_file = gzopen(dst, dst_mode);
205 if(dst_file == Z_NULL)
211 src_file = fopen(src,
"r");
223 while(!feof(src_file))
225 int read = fread(buf, 1,
Z_CHUNK_SZ, src_file);
234 gzwrite(dst_file, buf, read);
255 DLT_STRING(
"dlt-system-filetransfer, sending a file."));
259 char *fn = basename(src);
268 char *fdir = strndup(src,PATH_MAX);
270 fdir = dirname(fdir);
279 DLT_STRING(
"file information not available, may be file got overwritten"));
287 DLT_STRING(
"dlt-system-filetransfer, Moving file to tmp directory for compressing it."));
289 char *dst_tocompress;
293 dst_tocompress = malloc(len);
299 if(rename(src, dst_tocompress) < 0)
306 free(dst_tocompress);
311 dst_tosend = malloc(len);
318 free(dst_tocompress);
322 free(dst_tocompress);
328 DLT_STRING(
"dlt-system-filetransfer, Moving file to tmp directory."));
330 dst_tosend = malloc(len);
337 if(rename(src, dst_tosend) < 0)
352 DLT_STRING(
"dlt-system-filetransfer, File ready to send"));
369 dir = opendir(send_dir);
373 while((dp = readdir(dir)) !=
NULL)
375 if(dp->d_type != DT_REG)
379 DLT_STRING(
"dlt-system-filetransfer, old compressed file found in send directory:"),
DLT_STRING(dp->d_name));
380 int len = strlen(send_dir)+strlen(dp->d_name)+2;
383 snprintf(fn,len,
"%s/%s", send_dir, dp->d_name);
396 int len = strlen(tmp)+strlen(compress_dir)+1+1;
397 char *path_uncompressed = malloc(len);
399 snprintf(path_uncompressed,len,
"%s/%s",compress_dir,tmp);
402 if (stat(path_uncompressed,&sb)==-1)
406 DLT_STRING(
"dlt-system-filetransfer, sending file."));
414 DLT_STRING(
"dlt-system-filetransfer, uncompressed version exists. Deleting partially compressed version."));
415 if (sb.st_mode & S_IFREG)
419 if(
remove(fn ) != 0 )
430 DLT_STRING(
"dlt-system-filetransfer, Oldfile is a not regular file! Do we have a problem?"),
DLT_STRING(fn));
434 free(path_uncompressed);
439 DLT_STRING(
"dlt-system-filetransfer, Sending uncompressed file from previous LC."),
DLT_STRING(fn));
463 dir = opendir(compress_dir);
466 while((dp = readdir(dir)) !=
NULL)
468 if(dp->d_type != DT_REG)
471 DLT_STRING(
"dlt-system-filetransfer, old file found in compress-directory."));
475 int len = strlen(compress_dir)+strlen(dp->d_name)+2;
476 char *cd_filename = malloc(len);
478 snprintf(cd_filename,len,
"%s/%s",compress_dir,dp->d_name);
482 char *dst_tosend = malloc(len);
514 const char *sdir = opts->
Directory[which];
518 while((dp = readdir(dir)) !=
NULL)
520 if(dp->d_type != DT_REG){
525 DLT_STRING(
"dlt-system-filetransfer, old file found in directory."));
526 int len = strlen(sdir)+strlen(dp->d_name)+2;
527 char *fn = malloc(len);
529 snprintf(fn,len,
"%s/%s", sdir, dp->d_name);
561 DLT_STRING(
"dlt-system-filetransfer, flush directory of old files."));
566 compress_dir = malloc (len);
571 send_dir = malloc (len);
608 DLT_STRING(
"dlt-system-filetransfer, initializing inotify on directories."));
611 ino.handle = inotify_init();
616 DLT_STRING(
"Failed to initialize inotify in dlt-system file transfer."));
621 for(i = 0;i < opts->
Count;i++)
627 subdirpath= malloc (len);
630 int ret = mkdir(subdirpath,0777);
632 if (0 != ret && EEXIST != errno){
641 subdirpath= malloc (len);
644 ret = mkdir(subdirpath,0777);
645 if (0 != ret && EEXIST != errno){
654 ino.fd[i] = inotify_add_watch(ino.handle, opts->
Directory[i],
655 IN_CLOSE_WRITE|IN_MOVED_TO);
659 snprintf(buf, 1024,
"Failed to add inotify watch to directory %s in dlt-system file transfer.",
677 static char buf[INOTIFY_LEN];
678 ssize_t len = read(ino.handle, buf, INOTIFY_LEN);
682 DLT_STRING(
"Error while waiting for files in dlt-system file transfer."));
687 while(i < (len-INOTIFY_SZ))
689 struct inotify_event *ie = (
struct inotify_event *)&buf[i];
692 if((ie->mask & IN_CLOSE_WRITE) || (ie->mask & IN_MOVED_TO))
695 for(j = 0;j < opts->
Count;j++)
697 if(ie->wd == ino.fd[j])
700 int length = strlen(opts->
Directory[j])+ie->len+1;
701 if (length > PATH_MAX)
704 DLT_STRING(
"dlt-system-filetransfer: Very long path for file transfer. Cancelling transfer! Length is: "),
DLT_INT(length));
707 char *tosend = malloc(length);
708 snprintf(tosend,length,
"%s/%s", opts->
Directory[j], ie->name);
715 i += INOTIFY_SZ + ie->len;
726 "File transfer manager.");
747 static pthread_attr_t t_attr;
#define COMPRESS_EXTENSION
void send_dumped_file(FiletransferOptions const *opts, char *dst_tosend)
void filetransfer_thread(void *v_conf)
int flush_dir_compress(FiletransferOptions const *opts, int which, const char *compress_dir, const char *send_dir)
int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename)
This method gives information about the number of packages the file have.
int CompressionLevel[DLT_SYSTEM_LOG_DIRS_MAX]
#define DLT_SYSTEM_LOG_DIRS_MAX
int dlt_user_log_file_header_alias(DltContext *fileContext, const char *filename, const char *alias)
Transfer the head of the file as a dlt logs.
void start_filetransfer(DltSystemConfiguration *conf)
int wait_for_files(FiletransferOptions const *opts)
int flush_dir(FiletransferOptions const *opts, int which)
Cleans the surveyed directories and subdirectories. Sends residing files into trace.
int init_filetransfer_dirs(FiletransferOptions const *opts)
Initializes the surveyed directories.
char * Directory[DLT_SYSTEM_LOG_DIRS_MAX]
uint32_t getFileSerialNumber(const char *file, int *ok)
Get some information about the file serial number of a file.
FiletransferOptions Filetransfer
int Compression[DLT_SYSTEM_LOG_DIRS_MAX]
#define DLT_DECLARE_CONTEXT(CONTEXT)
DltReturnValue dlt_user_check_buffer(int *total_size, int *used_size)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
int compress_file_to(char *src, char *dst, int level)
int flush_dir_original(FiletransferOptions const *opts, int which)
int dlt_user_log_file_end(DltContext *fileContext, const char *filename, int deleteFlag)
Transfer the end of the file as a dlt logs.
int dlt_user_log_file_data(DltContext *fileContext, const char *filename, int packageToTransfer, int timeout)
Transfer the content data of a file.
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
DLT_IMPORT_CONTEXT(dltsystem)
int send_one(char *src, FiletransferOptions const *opts, int which)
Sends one file over DLT.
int flush_dir_send(FiletransferOptions const *opts, const char *compress_dir, const char *send_dir)
pthread_t threads[MAX_THREADS]
char * unique_name(char *src)
DltReturnValue dlt_user_log_resend_buffer(void)