Exemple #1
0
void		just_print_files(char **files, char *flags, char *folder)
{
	struct stat	*f_t;
	int			i;

	if (!files || !files[0] || !ft_strlen(files[0]))
		return ;
	i = 0;
	while (files[i])
	{
		if (ft_strchr(flags, 'G'))
		{
			if (!(f_t = get_file_stat(files[i], f_t, folder)))
				return ;
			if (!print_name_by_extension(files[i]))
				if (!print_name_by_permissions(get_permissions(f_t), files[i]))
					ft_putstr(files[i]);
			free(f_t);
		}
		else
			ft_putstr(files[i]);
		ft_putstr("\n");
		i++;
	}
}
Exemple #2
0
void		print_recursive(char **files, char *flags, char *folder, int i)
{
	char			**path;
	struct stat		*file_stat;
	int				boolean;

	if (!files || !files[0] || !ft_strlen(files[0]))
		return ;
	boolean = 0;
	if (!(path = malloc(sizeof(char*) * ALL_RECURSIVE_POSSIBILITY)))
		return ;
	path = ft_set_null(path);
	print_total(files, flags, folder);
	while (files[i])
	{
		if (!(file_stat = get_file_stat(files[i], file_stat, folder)))
			return ;
		print_by_flags(file_stat, flags, files, i);
		if (S_ISDIR(file_stat->st_mode) && files[i][0] != '.' && (boolean += 1))
			path = new_dir(path, files[i]);
		free(file_stat);
		i++;
	}
	ft_putstr("\n");
	free_files(files);
	(boolean) ? recursive_if(path, folder, flags) : free_files(path);
}
Exemple #3
0
void		recursive_if(char **path, char *folder, char *flags)
{
	int				i;
	char			*rl;
	struct stat		*file_stat;

	i = 0;
	rl = ft_strnew(DEFAULT_BUFFER);
	while (path[i])
	{
		ft_bzero(rl, DEFAULT_BUFFER);
		rl = realpath(folder, rl);
		rl = ft_strcat(rl, "/");
		rl = ft_strcat(rl, path[i]);
		rl = ft_strcat(rl, "/");
		if (!(file_stat = get_file_stat(path[i], file_stat, folder)))
			return ;
		ft_putstr(rl);
		ft_putstr(":\n");
		print_recursive(get_specified_dir(rl, flags), flags, rl, 0);
		free(file_stat);
		i++;
	}
	free(rl);
	free_files(path);
}
Exemple #4
0
    int32_t FileLogDevice::open(void* param) {
        char buffer[BUFSIZ];

        if (false == is_opened()) {
            if (NULL != param) {
                strncat(_file_path, (char*)param, MAX_FILE_PATH_LEN);
            }

            snprintf(buffer, BUFSIZ, "%s", _file_path);
            char* p = strrchr(buffer, '/');
            if (NULL != p) {
                *p = '\0';
                if (0 != FileUtil::create_directories(buffer)) {
                    return -1;
                }
            }

            _fp = fopen(_file_path, "a");
            if (NULL == _fp) {
                return errno;
            }
            struct stat state;
            get_file_stat(state);
            _file_size = state.st_size;

			TimeUtil::gettimeofday(&_create_time, NULL);
        }

		snprintf(buffer, BUFSIZ, "%s", _file_path);
		struct log_message_t log_message = {
			BGCC_LOGLEVEL_NOTICE, 
			"BGCC Version:"VERSION, "=ver=", 
			__FILE__, 
#ifndef _WIN32			
			STR(__LINE__), 
#else
			itoa(__LINE__, buffer, 10),
#endif
            __FUNCTION__,
			_create_time, bgcc::ThreadUtil::self_id()
		};

		write(log_message);

        return 0;
    }
Exemple #5
0
int			get_file_nbr(char **file_n_folder)
{
	struct stat		*file_stat;
	int				i;
	int				nbr;

	i = 0;
	nbr = 0;
	while (file_n_folder[i])
	{
		if (!(file_stat = get_file_stat(file_n_folder[i], file_stat, ".")))
			return (-1);
		if (!S_ISDIR(file_stat->st_mode))
			nbr++;
		i++;
		free(file_stat);
	}
	return (nbr);
}
Exemple #6
0
int main( int argc, char **argv )
{
    if(argc < 3) {
        CL_INFO_PRINT("at least two argument!\n");
    }
    /* ************
    filename1  wait sync
    filename2  dest
    **************/
    char *filename1,*filename2;
    int fd1,fd2;
    char * buf;
    uint32 checksum1;
    struct stat st;
    struct chunk_descriptor *chunk_desc;
    struct chunk_modify_descriptor chunk_modify_desc;
    filename1 = argv[1];
    filename2 = argv[2];
    chunk_desc = malloc(sizeof(struct chunk_descriptor));
    if(!chunk_desc) {
        CL_ERR_PRINT("out memory");
        return -1;
    }
    if(get_file_stat(filename1,chunk_desc)!=0) {
        return -1;
    }
    build_hash_table(chunk_desc);
    init_chunk_modify_descriptor(&chunk_modify_desc);
    generate_modify_descriptor(filename2,&chunk_modify_desc,chunk_desc);
    // print_hash_stat();
     // print_chunk_modify(&chunk_modify_desc);
    sync_file(filename1,&chunk_modify_desc);
    cleanup_modify_list(&chunk_modify_desc);
    cleanup_hash_list();

    free(chunk_desc->chunk);
    free(chunk_desc);
    return 0;
}
Exemple #7
0
int		*get_timestamp(char **files, char *folder)
{
	int				*tmp;
	int				i;
	struct stat		*file_stat;

	i = 0;
	while (files[i])
		i++;
	if (!(tmp = malloc(sizeof(int) * MAX_FILES_PER_FOLDER)))
		return (NULL);
	i = 0;
	while (files[i])
	{
		if (!(file_stat = get_file_stat(files[i], file_stat, folder)))
			return (NULL);
		tmp[i] = file_stat->st_mtime;
		free(file_stat);
		i++;
	}
	tmp[i] = 0;
	return (tmp);
}
Exemple #8
0
int main(int argc, char *argv[])
{
  struct stat	ffstat;
  char  *inputstr = NULL;
  char  *inbuf = NULL;
  char  *tracktype_str=NULL;
  char  tmpbuf[4096];
  int   inlen = 0, err=0, c=0;
  int   limit_arg = 0;

  if (argc < 2)
    usage(argv[0]);

  while ((c = getopt(argc, argv,"befjt:To:cl:n:s")) != -1)
  {
    switch (c)
    {
    case 'b':
      print_byte_offset=1;
      break;
    case 'e':
      print_epoch_time=1;
      break;
    case 'f':
      print_filename_only=1;
      break;
    case 'j':
      print_julian_time=1;
      break;
    case 'o':
      logfilename=optarg;
      break;
    case 't':
      tracksrch=1;
      tracktype_str=optarg;
      if (atoi(tracktype_str)==1)
        tracktype1=1;
      else if (atoi(tracktype_str)==2)
        tracktype2=1;
      else
        usage(argv[0]);
      break;
    case 'T':
      tracksrch=1;
      tracktype1=1;
      tracktype2=1;
      break;
    case 'c':
    	print_file_hit_count=1;
    	break;
    case 'l':
    	limit_arg = atoi(optarg);
    	if (limit_arg > 0)
    	  limit_file_results = limit_arg;
    	else
    		usage(argv[0]);
    	break;
    case 'n':
    	exclude_extensions = stolower(optarg);
    	break;
    case 's':
    	newstatus = 1;

    	break;
    case 'h':
    default:
      usage(argv[0]);
      break;
    }
  } 

  //do some cleanup to make sure that invalid options don't get combined
  if (logfilename != NULL)
  {
    if (newstatus == 1)
    {
    	print_file_hit_count = 0;
    }
  }
  else
  {
  	newstatus = 0;
  	print_file_hit_count = 0;
  }

  inputstr = argv[optind];
  if (inputstr == NULL)
    usage(argv[0]);

  inlen = strlen(inputstr) + 1;

  if (open_logfile() < 0)
    exit(-1);

  inbuf = (char *)malloc(inlen + 1);

  memset(inbuf, '\0', inlen+1);
  strncpy(inbuf, inputstr, inlen);

  signal_proc();

  init_time = time(NULL);
  printf("\n%s\n", PROG_VER);
  printf("\nLocal start time: %s\n",ctime((time_t *)&init_time));
  if (check_dir(inbuf)) 
  {
#ifdef WINDOWS
    if ((inbuf[strlen(inbuf) - 1]) != '\\')
      inbuf[strlen(inbuf)] = '\\';
#else
    if ((inbuf[strlen(inbuf) - 1]) != '/')
      inbuf[strlen(inbuf)] = '/';
#endif
    proc_dir_list(inbuf);
  }
  else
  {
    err = get_file_stat(inbuf, &ffstat);
    if (err == -1)
    {
      if (errno == ENOENT)
        fprintf(stderr, "File %s not found, can't stat\n", inbuf);
      else
        fprintf(stderr, "Cannot stat file %s; errno=%d\n", inbuf, errno);
      exit (-1);
    }

    if ((ffstat.st_size > 0) && ((ffstat.st_mode & S_IFMT) == S_IFREG))
    {
      memset(&tmpbuf, '\0', 4096);
      if (escape_space(inbuf, tmpbuf) == 0)
      {
        if (logfilename != NULL)
          if (strstr(inbuf, logfilename) != NULL)
          fprintf(stderr, "main: We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", inbuf);
          else
          {
#ifdef DEBUG
            printf("Processing file %s\n",inbuf);
#endif
            ccsrch(inbuf);
          }
        else
        {
#ifdef DEBUG
          printf("Processing file %s\n",inbuf);
#endif
          ccsrch(inbuf);
        }
      }
    }
    else if ((ffstat.st_mode & S_IFMT) == S_IFDIR)
    {
#ifdef WINDOWS
      if ((inbuf[strlen(inbuf) - 1]) != '\\')
        inbuf[strlen(inbuf)] = '\\';
#else
      if ((inbuf[strlen(inbuf) - 1]) != '/')
        inbuf[strlen(inbuf)] = '/';
#endif
      proc_dir_list(inbuf);
    }
    else
      fprintf(stderr, "main: Unknown mode returned-> %x\n", ffstat.st_mode);
  }

  cleanup_shtuff();
  free(inbuf);

  return (0);
}
Exemple #9
0
int proc_dir_list(char *instr)
{
  DIR            *dirptr;
  struct dirent  *direntptr;
  int             dir_name_len = 0;
  char           *curr_path = NULL;
  struct stat     fstat;
  int             err = 0;
  char            tmpbuf[4096];

  errno = 0;
  dir_name_len = strlen(instr);
  dirptr = opendir(instr);

#ifdef DEBUG
  printf("Checking directory <%s>\n",instr);
#endif

  if (dirptr == NULL)
  {
    fprintf(stderr, "proc_dir_list: Can't open dir %s; errno=%d\n", instr, errno);
    return (1);
  }
  errno = 0;
  curr_path = (char *)malloc(MAXPATH + 1);
  if (curr_path == NULL)
  {
    fprintf(stderr, "proc_dir_list: Can't allocate enough space; errno=%d\n", errno);
    closedir(dirptr);
    return (1);
  }
  memset(curr_path, '\0', MAXPATH+1);
  strncpy(curr_path, instr, MAXPATH);
  errno = 0;
  while ((direntptr = readdir(dirptr)) != NULL)
  {
    /* readdir give us everything and not necessarily in order. This 
       logic is just silly, but it works */
    if (((direntptr->d_name[0] == '.') &&
         (direntptr->d_name[1] == '\0')) ||
        ((direntptr->d_name[0] == '.') &&
         (direntptr->d_name[1] == '.') &&
         (direntptr->d_name[2] == '\0')))
      continue;

    errno = 0;
    strncat(curr_path, direntptr->d_name, MAXPATH);
    err = get_file_stat(curr_path, &fstat);

    if (err == -1)
    {
      if (errno == ENOENT)
        fprintf(stderr, "proc_dir_list: file %s not found, can't stat\n", curr_path);
      else
        fprintf(stderr, "proc_dir_list: Cannot stat file %s; errno=%d\n", curr_path, errno);
      closedir(dirptr);
      return (1);
    }
    if ((fstat.st_mode & S_IFMT) == S_IFDIR)
    {
      strncat(curr_path, "/", MAXPATH);
      proc_dir_list(curr_path);
    } 
    else if ((fstat.st_size > 0) && ((fstat.st_mode & S_IFMT) == S_IFREG))
    {
      memset(&tmpbuf, '\0', 4096);
      if (escape_space(curr_path, tmpbuf) == 0)
      {
        //rest file_hit_count so we can keep track of many hits each file has
        file_hit_count = 0;
        
        if (is_allowed_file_type(curr_path) == 0)
        {
	        /*
	         * kludge, need to clean this up
	         * later else any string matching in the path returns non NULL
	         */
	        if (logfilename != NULL)
	          if (strstr(curr_path, logfilename) != NULL)
	            fprintf(stderr, "We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", curr_path);
	          else
	          {
	            ccsrch(curr_path);
	            if (file_hit_count > 0 && print_file_hit_count == 1)
	              printf("%s: %d hits\n", curr_path, file_hit_count);
	          }
	        else
	        {
	          ccsrch(curr_path);
	        }
        }
      }
    }
#ifdef DEBUG
    else
    {
      if (fstat.st_size > 0)
        fprintf(stderr, "proc_dir_list: Unknown mode returned-> %x for file %s\n", fstat.st_mode,curr_path);
      else
        fprintf(stderr, "Not processing file of size 0 bytes: %s\n", curr_path);
    }
#endif
    curr_path[dir_name_len] = '\0';
  }

  free(curr_path);

  closedir(dirptr);
  return (0);
}