Exemplo n.º 1
0
void deleteFile ( const char *path )
{
   DIR *dir ;
   dirent *dir_info ;
   char file_path[PATH_MAX] ;
   if ( is_file( path ) )
   {
      remove ( path ) ;
      return ;
   }
   if ( is_dir( path ) )
   {
      if ( ( dir = opendir(path) ) == NULL )
      {
         return ;
      }
      while ( ( dir_info = readdir(dir) ) != NULL)
      {
         get_file_path ( path, dir_info->d_name, file_path ) ;
         if ( is_special_dir( dir_info->d_name ) )
            continue ;
         deleteFile ( file_path ) ;
         rmdir ( file_path ) ;
      }
   }
}
Exemplo n.º 2
0
// recurse to delete path(maybe dir or file)
void delete_files(const char *path)
{
	DIR *dir;
	struct dirent *dir_info;
	char file_path[PATH_MAX];

	if (is_file(path)) {
		remove(path);
		printf("delete file %s\n", path);
		return;
	}

	if (is_dir(path)) {
		if ((dir = opendir(path)) == NULL)
			return;
		while ((dir_info = readdir(dir)) != NULL) {
			get_file_path(path, dir_info->d_name, file_path);
			if (is_special_dir(dir_info->d_name))
				continue;
			delete_files(file_path);
			rmdir(file_path);
		}
	}
	printf("delete dir %s\n", path);
}
Exemplo n.º 3
0
/**
 * Rename a file or directory. If operation is not successfull - return FALSE
 */
bool rename_fr(int sock,const char *from,const char *to) {
	if(is_special_dir(from)) {
		send_repl(sock,REPL_553);
		return FALSE;
	} 
	struct stat s_buff;
	int status = stat(from,&s_buff);
	if(status!=0) {
		send_repl(sock,REPL_553);
		return FALSE;
	} 
	status = stat(to,&s_buff);
	if(status==0) {
		send_repl(sock,REPL_553);
		return FALSE;
	} 
	int b_mask = s_buff.st_mode & S_IFMT;
	if(b_mask == S_IFDIR || b_mask == S_IFREG) {
		int status = rename(from,to);
		if(status!=0) {
			send_repl(sock,REPL_553);
			return FALSE;
		}
	} else {
		send_repl(sock,REPL_553);
		return FALSE;
	}
	send_repl(sock,REPL_250);
	return TRUE;
}
Exemplo n.º 4
0
void state::process_dir(const tstring &fn)
{
    _TDIR *current_dir;
    struct _tdirent *entry;

    if(opt_debug) std::cerr << "*** process_dir(" << global::make_utf8(fn) << ")\n";

    if (have_processed_dir(fn)) {
	ocb.error_filename(fn,"symlink creates cycle");
	return ;
    }
  
    if ((current_dir = _topendir(fn.c_str())) == NULL)   {
	ocb.error_filename(fn,"%s", strerror(errno));
	return ;
    }    

    /* 2011-SEP-15 New logic:
     * 1. Open directory.
     * 2. Get a list of all the dir entries.
     * 3. Close the directory.
     * 4. Process them.
     */
    std::vector<tstring> dir_entries;
    while ((entry = _treaddir(current_dir)) != NULL) {
      // ignore . and ..
      if (is_special_dir(entry->d_name)) 
	continue; 
      
      // compute full path
      // don't append if the DIR_SEPARATOR if there is already one there
      tstring new_file = fn;

      if (0 == new_file.size() || new_file[new_file.size()-1]!=DIR_SEPARATOR)
      {
	new_file.push_back(DIR_SEPARATOR);
      }
      new_file.append(entry->d_name);

#ifdef _WIN32
      /// Windows Junction points
      if (is_junction_point(new_file)){
	continue;
      }
#endif

      dir_entries.push_back(new_file);
      
    }
    _tclosedir(current_dir);		// done with this directory

    processing_dir(fn);			// note that we are now processing a directory
    for(std::vector<tstring>::const_iterator it = dir_entries.begin();it!=dir_entries.end();it++){
	dig_normal(*it);
    }
    done_processing_dir(fn);		// note that we are done with this directory
    return ;
}
Exemplo n.º 5
0
/**
 * Writes the file statistics in a line "line"
 */
static bool get_file_info(const char *file_name, char *line){
	if(line==NULL)
		return FALSE;
	struct stat s_buff;
	if(is_special_dir(file_name))
		do_nothing();//return FALSE;
	int status = stat(file_name,&s_buff);
	if(status==0) {
		return get_file_info_stat(file_name,line,&s_buff);
	}
	return FALSE;
}
Exemplo n.º 6
0
/**
 * Delete the directory called "removed_dir"
 */
bool remove_dir(int sock,const char *removed_dir) {
	if(is_special_dir(removed_dir)) {
		send_repl(sock,REPL_550);
		return FALSE;
	} 
	struct stat s_buff;
	int status = stat(removed_dir,&s_buff);
	if(status!=0) {
		send_repl(sock,REPL_550);
		return FALSE;
	} 
	int b_mask = s_buff.st_mode & S_IFMT;
	if(b_mask != S_IFDIR) {
		send_repl(sock,REPL_550);
		return FALSE;
	}
	status = rmdir(removed_dir);
	if(status!=0) {
		send_repl(sock,REPL_550);
		return FALSE;
	}
	send_repl(sock,REPL_250);
	return TRUE;
}
Exemplo n.º 7
0
void state::dig_self_test()
{
    struct __stat64 sb;
    std::cerr << "dig_self_test\n";

    memset(&sb,0,sizeof(sb));
    std::cerr << "check stat 1: "
	      << TLSTAT(_T("z:\\simsong on my mac\\md5deep\\branches\\version4\\hashdeep\\md5.cpp"),&sb)
	      << " " << sb << "\n";

    memset(&sb,0,sizeof(sb));
    std::cerr <<  "check stat 2: "
	      <<  TLSTAT(_T("c:\\autoexec.bat"),&sb)
	      << " " << sb << "\n";

    memset(&sb,0,sizeof(sb));
    std::cerr <<  "check stat 3: "
	      <<  TLSTAT(_T("/etc/resolv.conf"),&sb)
	      << " " << sb << "\n";

    tstring fn(_T("this is"));
    fn.push_back(DIR_SEPARATOR);
    fn.push_back(DIR_SEPARATOR);
    fn += _T("a test");

    tstring fn2(fn);
    remove_double_slash(fn2);

    std::cerr << "remove_double_slash(" << fn << ")="<<fn2<<"\n";

    fn = _T("this is");
    fn.push_back(DIR_SEPARATOR);
    fn.push_back('.');
    fn.push_back(DIR_SEPARATOR);
    fn += _T("a test");

    remove_single_dirs(fn2);
    std::cerr << "remove_single_dirs(" << fn << ")="<<fn2<<"\n";

    fn = _T("this is");
    fn.push_back(DIR_SEPARATOR);
    fn += _T("a mistake");
    fn.push_back(DIR_SEPARATOR);
    fn.push_back('.');
    fn.push_back('.');
    fn.push_back(DIR_SEPARATOR);
    fn += _T("a test");
    fn2 = fn;

    remove_double_dirs(fn2);
    std::cerr << "remove_double_dirs(" << fn << ")="<<fn2<<"\n";
    std::cerr << "is_special_dir(.)=" << is_special_dir(_T(".")) << "\n";
    std::cerr << "is_special_dir(..)=" << is_special_dir(_T("..")) << "\n";

    tstring names[] = {_T("dig.cpp"),
		       _T("."),_T("/dev/null"),_T("/dev/tty"),
		       _T("../testfiles/symlinktest/dir1/dir1"),_T("")};

    state s;
    for(int i=0;names[i].size()>0;i++){
	uint64_t stat_bytes;
	timestamp_t ctime;
	timestamp_t mtime;
	timestamp_t atime;
	file_types ft = s.file_type(names[i],&s.ocb,&stat_bytes,&ctime,&mtime,&atime);
	std::cerr << "file_type(" << names[i] << ")="
		  << (int)ft << " size=" << stat_bytes << " ctime=" << ctime << "\n";
    }
}
Exemplo n.º 8
0
void state::dig_win32(const std::wstring &fn)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    if(opt_debug) ocb.status("*** state::dig_win32(%s)",
			     global::make_utf8(fn).c_str());

    if (is_win32_device_file(fn)){
	ocb.hash_file(fn);
	return ;
    }

    // Filenames without wildcards can be processed by the
    // normal recursion code.
    size_t asterisk = fn.find(L'*');
    size_t question  = fn.find(fn,L'?');
    if (asterisk==std::wstring::npos && question==std::wstring::npos){
	dig_normal(fn);
	return;
    }
  
    hFind = FindFirstFile(fn.c_str(), &FindFileData);
    if (INVALID_HANDLE_VALUE == hFind)  {
	ocb.error_filename(fn,"No such file or directory");
	return;
    }
  
#define FATAL_ERROR_UNK(A) if (NULL == A) fatal_error("%s: %s", progname.c_str(), strerror(errno));
#define FATAL_ERROR_MEM(A) if (NULL == A) fatal_error("%s: Out of memory",progname.c_str());
  
    tstring dirname = win32_dirname(fn);
    if(dirname.size()>0) dirname += _TEXT(DIR_SEPARATOR);
  
    int rc = 1;
    while (rc!=0)  {
	if (!(is_special_dir(FindFileData.cFileName)))    {
	    // The filename we've found doesn't include any path information.
	    // We have to add it back in manually. Thankfully Windows doesn't
	    // allow wildcards in the early part of the path. For example,
	    // we will never see:  c:\bin\*\tools 
	    //
	    // Because the wildcard is always in the last part of the input
	    // (e.g. c:\bin\*.exe) we can use the original dirname, combined
	    // with the filename we've found, to make the new filename. 
      
	    std::wstring new_fn;

	    new_fn = dirname + FindFileData.cFileName;
	    if (!ocb.opt_relative) {
		new_fn = global::get_realpath(new_fn);
	    }
      
	    if (!(is_junction_point(new_fn))) dig_normal(new_fn); 
	}
    
	rc = FindNextFile(hFind, &FindFileData);
    }
  
    // rc now equals zero, we can't find the next file

    if (ERROR_NO_MORE_FILES != GetLastError())  {
	// The Windows API for getting an intelligible error message
	// is beserk. Rather than play their silly games, we 
	// acknowledge that an unknown error occured and hope we
	// can continue.
	ocb.error_filename(fn,"Unknown error while expanding wildcard");
	return;
    }
  
    rc = FindClose(hFind);
    if (0 == rc)  {
	ocb.error_filename(fn,"Unknown error while cleaning up wildcard expansion");
    }
    if(opt_debug) ocb.status("state::dig_win32(%s)",global::make_utf8(fn).c_str());
}
Exemplo n.º 9
0
F_set *
make_list (char *file_user, char *list_user)
{
  struct DIR *dir;
  struct dirent *dir_info;

  F_set *head = NEW (F_set);
  F_set *head1 = head;

  char *mimi = NULL;
  char *pos = NULL;
  int number = 0;

  if (list_user)
    {
      list_user = mmaping (list_user);
      while (list_user != '\0')
	{

	  mimi = (char *) malloc (200 * sizeof (char) + 1);
	  memset (mimi, 0, 200);

	  F_set *fset = NEW (F_set);
	  fset->filename = NULL;
	  fset->number = 0;

	  if ((pos = strchr (list_user, '\n')) != NULL)
	    strncat (mimi, list_user, pos - list_user);
	  else
	    break;

	  fset->filename = mimi;
	  int num = number;
	  fset->number = num;
	  fset->next = head->next;
	  head->next = fset;
	  head = head->next;
	  list_user = pos + 1;
	  number++;
	}
    }

  else if (is_file (file_user))
    {

      F_set *fset = NEW (F_set);
      fset->filename = file_user;
      fset->next = head->next;
      head->next = fset;
      head = head->next;
      head->next = NULL;
    }

  else if (is_dir (file_user))
    {
      dir = opendir (file_user);
      if (dir == NULL)
	{
	  perror ("Empty dir\n");
	  exit (-1);
	}
      while ((dir_info = readdir (dir)) != NULL)
	{
	  if (is_special_dir (dir_info->d_name))
	    continue;

	  if (!strstr (dir_info->d_name, ".bloom"))
	    continue;

	  F_set *fset = NEW (F_set);
	  fset->filename = get_file_path (file_user, dir_info->d_name);
	  fset->number = &number;
	  printf ("fset->%d\n", fset->number);
	  fset->next = head->next;
	  head->next = fset;
	  head = head->next;
	  number++;
	}
    }
  else
    {
      perror (file_user);
      exit (-1);
    }
  //free(file_path);
  //free(mimi);
  head1->next->reads_num = 0;
  head1->next->reads_contam = 0;
  return head1->next;

}