automotive-dlt
dlt-system-filetransfer.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
32 /*******************************************************************************
33 ** **
34 ** SRC-MODULE: dlt-system-filetransfer.c **
35 ** **
36 ** TARGET : linux **
37 ** **
38 ** PROJECT : DLT **
39 ** **
40 ** AUTHOR : Lassi Marttala <lassi.lm.marttala@partner.bmw.de> **
41 ** Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
42 ** **
43 ** PURPOSE : **
44 ** **
45 ** REMARKS : **
46 ** **
47 ** PLATFORM DEPENDANT [yes/no]: yes **
48 ** **
49 ** TO BE CHANGED BY USER [yes/no]: no **
50 ** **
51 *******************************************************************************/
52 
53 
54 #include <pthread.h>
55 #include <unistd.h>
56 #ifdef linux
57 #include <sys/inotify.h>
58 #endif
59 #include <libgen.h>
60 #include <dirent.h>
61 #include <zlib.h>
62 #include <time.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <inttypes.h>
66 
67 #include "dlt-system.h"
68 #include "dlt.h"
69 #include "dlt_filetransfer.h"
70 
71 #ifdef linux
72 #define INOTIFY_SZ (sizeof(struct inotify_event))
73 #define INOTIFY_LEN (INOTIFY_SZ + NAME_MAX + 1)
74 #endif
75 #define Z_CHUNK_SZ 1024*128
76 #define COMPRESS_EXTENSION ".gz"
77 #define SUBDIR_COMPRESS ".tocompress"
78 #define SUBDIR_TOSEND ".tosend"
79 
80 
81 
83 // From dlt_filetransfer
84 extern uint32_t getFileSerialNumber(const char* file, int *ok);
85 
87 DLT_DECLARE_CONTEXT(filetransferContext)
88 
89 #ifdef linux
90 typedef struct {
91  int handle;
93 } s_ft_inotify;
94 
95 s_ft_inotify ino;
96 #endif
97 
98 
99 char *origin_name(char *src){
100  if (strlen( (char*) basename(src)) > 10 ){
101  return (char*)(basename(src)+10);
102  }
103  else{
104  DLT_LOG(dltsystem, DLT_LOG_ERROR,
105  DLT_STRING("dlt-system-filetransfer, error in recreating origin name!"));
106  return NULL;
107  }
108 }
109 
110 char *unique_name(char *src)
111 {
112  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
113  DLT_STRING("dlt-system-filetransfer, creating unique temporary file name."));
114  time_t t = time(NULL);
115  int ok;
116  uint32_t l = getFileSerialNumber(src, &ok) ^ t;
117  if (!ok){
118  return (char*) NULL;
119  }
120 
121  char *basename_f = basename(src);
122  // Length of ULONG_MAX + 1
123  int len = 11+strlen(basename_f);
124  if (len > NAME_MAX){
125  DLT_LOG(dltsystem, DLT_LOG_WARN,
126  DLT_STRING("dlt-system-filetransfer, unique name creation needs to shorten the filename:"),DLT_STRING(basename_f));
127  len = NAME_MAX;
128  }
129 
130  char *ret = malloc(len);
131 
132  MALLOC_ASSERT(ret);
133  snprintf(ret, len, "%010" PRIu32 "%s", l,basename_f);
134  return ret;
135 }
136 
141 void send_dumped_file(FiletransferOptions const *opts,char *dst_tosend)
142 {
143  // check if a client is connected to the deamon. If not, try again in a second
144  while(dlt_get_log_state() != 1)
145  sleep(1);
146 
147  char *fn = origin_name(dst_tosend);
148  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
149  DLT_STRING("dlt-system-filetransfer, sending dumped file:"),DLT_STRING(fn));
150  if(dlt_user_log_file_header_alias(&filetransferContext, dst_tosend, fn) == 0)
151  {
152  int pkgcount = dlt_user_log_file_packagesCount(&filetransferContext, dst_tosend);
153  int lastpkg = 0;
154  int success = 1;
155  while(lastpkg < pkgcount)
156  {
157  int total = 2;
158  int used = 2;
159  dlt_user_check_buffer(&total, &used);
160  while((total-used) < (total/2))
161  {
162  struct timespec t;
163  t.tv_sec = 0;
164  t.tv_nsec = 1000000ul*opts->TimeoutBetweenLogs;
165  nanosleep(&t, NULL);
167  dlt_user_check_buffer(&total, &used);
168  }
169  lastpkg++;
170  if(dlt_user_log_file_data(&filetransferContext, dst_tosend, lastpkg, opts->TimeoutBetweenLogs) < 0)
171  {
172  success = 0;
173  break;
174  }
175  }
176  if (success)
177  dlt_user_log_file_end(&filetransferContext, dst_tosend, 1);
178  }
179  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
180  DLT_STRING("dlt-system-filetransfer, sent dumped file"));
181 }
182 
191 int compress_file_to(char *src, char *dst, int level)
192 {
193  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
194  DLT_STRING("dlt-system-filetransfer, compressing file from:"),DLT_STRING(src),DLT_STRING("to:"),DLT_STRING(dst));
195  char *buf;
196 
197 
198  char dst_mode[8];
199  snprintf(dst_mode,8, "wb%d", level);
200 
201  gzFile dst_file;
202  FILE *src_file;
203 
204  dst_file = gzopen(dst, dst_mode);
205  if(dst_file == Z_NULL)
206  {
207 
208  return -1;
209  }
210 
211  src_file = fopen(src, "r");
212 
213  if(src_file == NULL)
214  {
215  gzclose(dst_file);
216 
217  return -1;
218  }
219 
220  buf = malloc(Z_CHUNK_SZ);
221  MALLOC_ASSERT(buf);
222 
223  while(!feof(src_file))
224  {
225  int read = fread(buf, 1, Z_CHUNK_SZ, src_file);
226  if(ferror(src_file))
227  {
228  free(buf);
229 
230  gzclose(dst_file);
231  fclose(src_file);
232  return -1;
233  }
234  gzwrite(dst_file, buf, read);
235  }
236 
237  if(remove(src) < 0)
238  DLT_LOG(dltsystem, DLT_LOG_WARN, DLT_STRING("Could not remove file"), DLT_STRING(src));
239  free(buf);
240  fclose(src_file);
241  gzclose(dst_file);
242 
243  return 0;
244 }
245 
247 
252 int send_one(char *src, FiletransferOptions const *opts, int which)
253 {
254  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
255  DLT_STRING("dlt-system-filetransfer, sending a file."));
256  sleep(opts->TimeDelay);
257 
258  // Prepare all needed file names
259  char *fn = basename(src);
260  if (fn == NULL)
261  {
262  DLT_LOG(dltsystem,
264  DLT_STRING("basename not valid"));
265  return -1;
266  }
267 
268  char *fdir = strndup(src,PATH_MAX);
269  MALLOC_ASSERT(fdir);
270  fdir = dirname(fdir);//dirname overwrites its argument anyway
271  char *dst_tosend;//file which is going to be sent
272 
273  char *rn = unique_name(src);//new unique filename based on inode
274 
275  if (rn == NULL)
276  {
277  DLT_LOG(dltsystem,
279  DLT_STRING("file information not available, may be file got overwritten"));
280  return -1;
281  }
282 
283  // Compress if needed
284  if(opts->Compression[which] > 0)
285  {
286  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
287  DLT_STRING("dlt-system-filetransfer, Moving file to tmp directory for compressing it."));
288 
289  char *dst_tocompress;//file which is going to be compressed, the compressed one is named dst_tosend
290 
291 
292  int len = strlen(fdir)+strlen(SUBDIR_COMPRESS)+strlen(rn)+3;//the filename in .tocompress +2 for 2*"/", +1 for \0
293  dst_tocompress = malloc(len);
294  MALLOC_ASSERT(dst_tocompress);
295 
296  snprintf(dst_tocompress,len,"%s/%s/%s",fdir,SUBDIR_COMPRESS,rn);
297 
298  //moving in subdir, from where it can be compressed
299  if(rename(src, dst_tocompress) < 0)
300  {
301  DLT_LOG(dltsystem, DLT_LOG_ERROR,
302  DLT_STRING("Could not move file"),
303  DLT_STRING(src),
304  DLT_STRING(dst_tocompress));
305  free(rn);
306  free(dst_tocompress);
307  free(fdir);
308  return -1;
309  }
310  len = strlen(fdir)+strlen(SUBDIR_TOSEND)+strlen(rn)+strlen(COMPRESS_EXTENSION)+3;//the resulting filename in .tosend +2 for 2*"/", +1 for \0
311  dst_tosend = malloc(len);
312  MALLOC_ASSERT(dst_tosend);
313  snprintf(dst_tosend,len,"%s/%s/%s%s",fdir,SUBDIR_TOSEND,rn,COMPRESS_EXTENSION);
314 
315  if (compress_file_to(dst_tocompress,dst_tosend, opts->CompressionLevel[which]) != 0){
316  free(rn);
317  free(dst_tosend);
318  free(dst_tocompress);
319  free(fdir);
320  return -1;
321  }
322  free(dst_tocompress);
323 
324  }
325  else{
326  //move it directly into "tosend"
327  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
328  DLT_STRING("dlt-system-filetransfer, Moving file to tmp directory."));
329  int len = strlen(fdir)+strlen(SUBDIR_TOSEND)+strlen(rn)+3;
330  dst_tosend = malloc(len);//the resulting filename in .tosend +2 for 2*"/", +1 for \0
331 
332  snprintf(dst_tosend,len,"%s/%s/%s",fdir,SUBDIR_TOSEND,rn);
333 
334  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
335  DLT_STRING("dlt-system-filetransfer, Rename:"),DLT_STRING(src),DLT_STRING("to: "),DLT_STRING(dst_tosend));
336  //moving in subdir, from where it can be compressed
337  if(rename(src, dst_tosend) < 0)
338  {
339  DLT_LOG(dltsystem, DLT_LOG_ERROR,
340  DLT_STRING("Could not move file"),
341  DLT_STRING(src),
342  DLT_STRING(dst_tosend));
343  free(rn);
344  free(dst_tosend);
345  free(fdir);
346  return -1;
347  }
348 
349  }
350 
351  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
352  DLT_STRING("dlt-system-filetransfer, File ready to send"));
353 
354  send_dumped_file(opts,dst_tosend);
355 
356 
357  free(rn);
358  free(dst_tosend);
359  free(fdir);
360 
361  return 0;
362 }
363 
364 
365 int flush_dir_send(FiletransferOptions const *opts, const char *compress_dir, const char *send_dir){
366 
367  struct dirent *dp;
368  DIR *dir;
369  dir = opendir(send_dir);
370 
371  if(dir != NULL)
372  {
373  while((dp = readdir(dir)) != NULL)
374  {
375  if(dp->d_type != DT_REG)
376  continue;
377  char *fn;
378  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
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;
381  fn = malloc(len);
382  MALLOC_ASSERT(fn);
383  snprintf(fn,len, "%s/%s", send_dir, dp->d_name);
384 
385 
386  //if we have a file here and in the to_compress dir, we delete the to_send file: we can not be sure, that it has been properly compressed!
387  if (!strncmp( dp->d_name+strlen(dp->d_name)-strlen(COMPRESS_EXTENSION),COMPRESS_EXTENSION,strlen(COMPRESS_EXTENSION)))
388  {
389 
390  //ends with ".gz"
391  //old file name (not: path) would have been:
392  char tmp[strlen(dp->d_name)-strlen(COMPRESS_EXTENSION)+1];
393  strncpy(tmp,dp->d_name,strlen(dp->d_name)-strlen(COMPRESS_EXTENSION));
394  tmp[strlen(dp->d_name)-strlen(COMPRESS_EXTENSION)]='\0';
395 
396  int len = strlen(tmp)+strlen(compress_dir)+1+1;//2 sizes + 1*"/" + \0
397  char *path_uncompressed = malloc(len);
398  MALLOC_ASSERT(path_uncompressed);
399  snprintf(path_uncompressed,len,"%s/%s",compress_dir,tmp);
400 
401  struct stat sb;
402  if (stat(path_uncompressed,&sb)==-1)
403  {
404  //uncompressed equivalent does not exist. We can send it out.
405  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
406  DLT_STRING("dlt-system-filetransfer, sending file."));
407 
408  send_dumped_file(opts,fn);
409  }
410  else
411  {
412  //There is an uncompressed file. Compression seems to have been interrupted -> delete the compressed file instead of sending it!
413  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
414  DLT_STRING("dlt-system-filetransfer, uncompressed version exists. Deleting partially compressed version."));
415  if (sb.st_mode & S_IFREG)
416  {
417 
418 
419  if( remove(fn ) != 0 )
420  {
421  //"Error deleting file". Continue? If we would cancel, maybe the dump is never sent! Deletion would again be tried in next LC.
422  DLT_LOG(dltsystem, DLT_LOG_ERROR,
423  DLT_STRING("Error deleting file:"),DLT_STRING(fn));
424  }
425  }
426  else
427  {
428  //"Oldfile is a not reg file. Is this possible? Can we compress a directory?: %s\n",path_uncompressed);
429  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
430  DLT_STRING("dlt-system-filetransfer, Oldfile is a not regular file! Do we have a problem?"),DLT_STRING(fn));
431  }
432 
433  }
434  free(path_uncompressed);//it is no more used. It would be transferred in next step.
435  }//it is a .gz file
436  else{
437  //uncompressed file. We can just resend it, the action to put it here was a move action.
438  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
439  DLT_STRING("dlt-system-filetransfer, Sending uncompressed file from previous LC."),DLT_STRING(fn));
440  send_dumped_file(opts,fn);
441  }
442  free(fn);
443  }
444  }
445  else
446  {
447  DLT_LOG(dltsystem, DLT_LOG_ERROR,
448  DLT_STRING("Could not open directory"),
449  DLT_STRING(send_dir));
450  return -1;
451  }
452  closedir(dir);//end: send_dir
453  return 0;
454 }
455 
456 
457 int flush_dir_compress(FiletransferOptions const *opts, int which, const char *compress_dir, const char *send_dir){
458 
459  //check for files in compress_dir. Assumption: a file which lies here, should have been compressed, but that action was interrupted.
460  //As it can arrive here only by a rename, it is most likely to be a complete file
461  struct dirent *dp;
462  DIR *dir;
463  dir = opendir(compress_dir);
464  if(dir != NULL)
465  {
466  while((dp = readdir(dir)) != NULL)
467  {
468  if(dp->d_type != DT_REG)
469  continue;
470  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
471  DLT_STRING("dlt-system-filetransfer, old file found in compress-directory."));
472 
473 
474  //compress file into to_send dir
475  int len = strlen(compress_dir)+strlen(dp->d_name)+2;
476  char *cd_filename = malloc(len);
477  MALLOC_ASSERT(cd_filename);
478  snprintf(cd_filename,len,"%s/%s",compress_dir,dp->d_name);
479 
480 
481  len = strlen(send_dir)+strlen(dp->d_name)+strlen(COMPRESS_EXTENSION)+2;
482  char *dst_tosend = malloc(len);//the resulting filename in .tosend +2 for 1*"/", +1 for \0 + .gz
483  MALLOC_ASSERT(dst_tosend);
484  snprintf(dst_tosend,len,"%s/%s%s",send_dir,dp->d_name,COMPRESS_EXTENSION);
485 
486  if (compress_file_to(cd_filename,dst_tosend, opts->CompressionLevel[which]) != 0){
487  free(dst_tosend);
488  free(cd_filename);
489  closedir(dir);
490  return -1;
491  }
492 
493  //send file
494  send_dumped_file(opts,dst_tosend);
495  free(dst_tosend);
496  free(cd_filename);
497  }
498  }
499  else
500  {
501  DLT_LOG(dltsystem, DLT_LOG_ERROR,
502  DLT_STRING("Could not open directory"),
503  DLT_STRING(compress_dir));
504  return -1;
505  }
506  closedir(dir);//end: compress_dir
507 
508  return 0;
509 }
510 
511 int flush_dir_original(FiletransferOptions const *opts, int which){
512  struct dirent *dp;
513  DIR *dir;
514  const char *sdir = opts->Directory[which];
515  dir = opendir(sdir);
516  if(dir != NULL)
517  {
518  while((dp = readdir(dir)) != NULL)
519  {
520  if(dp->d_type != DT_REG){
521  //we don't send directories
522  continue;
523  }
524  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
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);
528  MALLOC_ASSERT(fn);
529  snprintf(fn,len, "%s/%s", sdir, dp->d_name);
530  if(send_one(fn, opts, which) < 0)
531  {
532  closedir(dir);
533  free(fn);
534  return -1;
535  }
536  free(fn);
537  }
538  }
539  else
540  {
541  DLT_LOG(dltsystem, DLT_LOG_ERROR,
542  DLT_STRING("Could not open directory"),
543  DLT_STRING(sdir));
544  return -1;
545  }
546  closedir(dir);
547  return 0;
548 }
549 
551 
556 int flush_dir(FiletransferOptions const *opts, int which)
557 {
558 
559 
560  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
561  DLT_STRING("dlt-system-filetransfer, flush directory of old files."));
562 
563  char *compress_dir;
564  char *send_dir;
565  int len = strlen(opts->Directory[which])+strlen(SUBDIR_COMPRESS)+2;
566  compress_dir = malloc (len);
567  MALLOC_ASSERT(compress_dir);
568  snprintf(compress_dir,len,"%s/%s",opts->Directory[which],SUBDIR_COMPRESS);
569 
570  len = strlen(opts->Directory[which])+strlen(SUBDIR_TOSEND)+2;
571  send_dir = malloc (len);
572  MALLOC_ASSERT(send_dir);
573  snprintf(send_dir,len,"%s/%s",opts->Directory[which],SUBDIR_TOSEND);
574 
575  //1st: scan the tosend directory.
576  if ( 0 != flush_dir_send(opts, compress_dir, send_dir) ){
577  free(send_dir);
578  free(compress_dir);
579  return -1;
580  }
581 
582  //1nd: scan the tocompress directory.
583  if (0 != flush_dir_compress(opts, which, compress_dir, send_dir)){
584  free(send_dir);
585  free(compress_dir);
586  return -1;
587  }
588 
589  free(send_dir);//no more used
590  free(compress_dir);
591 
592  //last step: scan the original directory - we can reuse the send_one function
593  if ( 0 != flush_dir_original(opts,which)){
594  return -1;
595  }
596 
597  return 0;
598 }
599 
601 
606 {
607  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
608  DLT_STRING("dlt-system-filetransfer, initializing inotify on directories."));
609  int i;
610 #ifdef linux
611  ino.handle = inotify_init();
612 
613  if(ino.handle < 0)
614  {
615  DLT_LOG(filetransferContext, DLT_LOG_FATAL,
616  DLT_STRING("Failed to initialize inotify in dlt-system file transfer."));
617  return -1;
618  }
619 #endif
620 
621  for(i = 0;i < opts->Count;i++)
622  {
623  //create subdirectories for processing the files
624 
625  char *subdirpath;
626  int len = strlen(opts->Directory[i])+strlen(SUBDIR_COMPRESS)+2;
627  subdirpath= malloc (len);
628  MALLOC_ASSERT(subdirpath);
629  snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_COMPRESS);
630  int ret = mkdir(subdirpath,0777);
631 
632  if (0 != ret && EEXIST != errno){
633  DLT_LOG(dltsystem, DLT_LOG_ERROR,
634  DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno));
635  free (subdirpath);
636  return -1;
637  }
638  free(subdirpath);
639 
640  len = strlen(opts->Directory[i])+strlen(SUBDIR_TOSEND)+2;
641  subdirpath= malloc (len);
642  MALLOC_ASSERT(subdirpath);
643  snprintf(subdirpath,len,"%s/%s",opts->Directory[i],SUBDIR_TOSEND);
644  ret = mkdir(subdirpath,0777);
645  if (0 != ret && EEXIST != errno){
646  DLT_LOG(dltsystem, DLT_LOG_ERROR,
647  DLT_STRING("dlt-system-filetransfer, error creating subdirectory: "),DLT_STRING(subdirpath),DLT_STRING(" Errorcode: "),DLT_INT(errno));
648  free (subdirpath);
649  return -1;
650  }
651  free(subdirpath);
652 
653 #ifdef linux
654  ino.fd[i] = inotify_add_watch(ino.handle, opts->Directory[i],
655  IN_CLOSE_WRITE|IN_MOVED_TO);
656  if(ino.fd[i] < 0)
657  {
658  char buf[1024];
659  snprintf(buf, 1024, "Failed to add inotify watch to directory %s in dlt-system file transfer.",
660  opts->Directory[i]);
661  DLT_LOG(filetransferContext, DLT_LOG_FATAL,
662  DLT_STRING(buf));
663  return -1;
664  }
665 #endif
666 
667  flush_dir(opts, i);
668 
669  }
670  return 0;
671 }
672 
674 {
675 #ifdef linux
676  DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, waiting for files."));
677  static char buf[INOTIFY_LEN];
678  ssize_t len = read(ino.handle, buf, INOTIFY_LEN);
679  if(len < 0)
680  {
681  DLT_LOG(filetransferContext, DLT_LOG_ERROR,
682  DLT_STRING("Error while waiting for files in dlt-system file transfer."));
683  return -1;
684  }
685 
686  unsigned int i = 0;
687  while(i < (len-INOTIFY_SZ))
688  {
689  struct inotify_event *ie = (struct inotify_event *)&buf[i];
690  if(ie->len > 0)
691  {
692  if((ie->mask & IN_CLOSE_WRITE) || (ie->mask & IN_MOVED_TO))
693  {
694  int j;
695  for(j = 0;j < opts->Count;j++)
696  {
697  if(ie->wd == ino.fd[j])
698  {
699  DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, found new file."), DLT_STRING(ie->name));
700  int length = strlen(opts->Directory[j])+ie->len+1;
701  if (length > PATH_MAX)
702  {
703  DLT_LOG(filetransferContext, DLT_LOG_ERROR,
704  DLT_STRING("dlt-system-filetransfer: Very long path for file transfer. Cancelling transfer! Length is: "),DLT_INT(length));
705  return -1;
706  }
707  char *tosend = malloc(length);
708  snprintf(tosend,length, "%s/%s", opts->Directory[j], ie->name);
709  send_one(tosend, opts, j);
710  free(tosend);
711  }
712  }
713  }
714  }
715  i += INOTIFY_SZ + ie->len;
716  }
717 #endif
718  return 0;
719 }
720 
721 void filetransfer_thread(void *v_conf)
722 {
723  DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, in thread."));
725  DLT_REGISTER_CONTEXT(filetransferContext, conf->Filetransfer.ContextId,
726  "File transfer manager.");
727 
728  sleep(conf->Filetransfer.TimeStartup);
729 
730  if(init_filetransfer_dirs(&(conf->Filetransfer)) < 0)
731  return;
732 
733  while(!threads.shutdown)
734  {
735  if(wait_for_files(&(conf->Filetransfer)) < 0)
736  {
737  DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while waiting files. File transfer shutdown."));
738  return;
739  }
740  sleep(conf->Filetransfer.TimeDelay);
741  }
742 }
743 
745 {
746  DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, start."));
747  static pthread_attr_t t_attr;
748  static pthread_t pt;
749  pthread_create(&pt, &t_attr, (void *)filetransfer_thread, conf);
750  threads.threads[threads.count++] = pt;
751 }
#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)
#define SUBDIR_TOSEND
#define DLT_INT(INT_VAR)
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]
Definition: dlt-system.h:125
#define DLT_SYSTEM_LOG_DIRS_MAX
Definition: dlt-system.h:66
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)
#define Z_CHUNK_SZ
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.
int dlt_get_log_state()
Definition: dlt_user.c:1276
char * Directory[DLT_SYSTEM_LOG_DIRS_MAX]
Definition: dlt-system.h:126
uint32_t getFileSerialNumber(const char *file, int *ok)
Get some information about the file serial number of a file.
FiletransferOptions Filetransfer
Definition: dlt-system.h:157
int Compression[DLT_SYSTEM_LOG_DIRS_MAX]
Definition: dlt-system.h:124
#define DLT_DECLARE_CONTEXT(CONTEXT)
DltReturnValue dlt_user_check_buffer(int *total_size, int *used_size)
Definition: dlt_user.c:4512
#define DLT_STRING(TEXT)
#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 SUBDIR_COMPRESS
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
DltSystemThreads threads
DLT_IMPORT_CONTEXT(dltsystem)
#define MALLOC_ASSERT(x)
Definition: dlt-dbus.h:40
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]
Definition: dlt-system.h:163
char * unique_name(char *src)
DltReturnValue dlt_user_log_resend_buffer(void)
Definition: dlt_user.c:4321
#define NULL
Definition: dlt_common.h:232