Example #1
0
int FileUtils::rmdir(
    const String &dirName )
{
    int res = 0;
    
    String cleanName = FileUtils::properName( dirName );
    
    { // required because the directory would otherwise be in use
#if defined(_WINDOWS) || defined(WIN32)
        FileIterator it(new FileIterImpWin32);
#else
        FileIterator it(new FileIterImpUnix);
#endif
        String deletePattern = cleanName;
        deletePattern += FileUtils::PATH_SEP;
        deletePattern += FileUtils::ALL_FILES_PAT;

        String dataFileName;

        FileIterator::Status stat = it.findFirst( deletePattern, dataFileName );
        while ( (res == 0) && (stat == FileIterator::cFound) )
        {
            // construct the path to the target
            String targetName = cleanName;
            targetName += FileUtils::PATH_SEP;
            targetName += dataFileName;

            // check to see if the target is a directory
            FileAttributes attrs;
            if ( FileUtils::getAttributes( targetName, attrs ) && 
                 attrs.isDirectory() )
            {
                // if this is a directory, then call rmdir to delete
                res = FileUtils::rmdir( targetName );
            }
            else
            {
                // otherwise delete all the file from this directory
                res = FileUtils::fileDelete( targetName ) ? 0 : -1;
            }

            stat = it.findNext( dataFileName );
        }
    }

#if defined(_WINDOWS) || defined(WIN32)
    res = _trmdir( cleanName.c_str() );
#else
    // now delete the target directory
    res = ::rmdir( cleanName.c_str() );
#endif

    return res;
}
Example #2
0
static void check_csync_vio_mkdir(void **state)
{
    CSYNC *csync = *state;
    csync_stat_t sb;
    int rc;
    mbchar_t *dir = c_utf8_to_locale(CSYNC_TEST_DIR);

    rc = csync_vio_mkdir(csync, CSYNC_TEST_DIR, MKDIR_MASK);
    assert_int_equal(rc, 0);

    rc = _tstat(dir, &sb);
    assert_int_equal(rc, 0);

    _trmdir(dir);
    c_free_locale_string(dir);
}
Example #3
0
static void check_csync_vio_mkdirs_some_exist(void **state)
{
    CSYNC *csync = *state;
    csync_stat_t sb;
    mbchar_t *this_dir = c_utf8_to_locale("/tmp/csync_test/this");
    mbchar_t *stat_dir = c_utf8_to_locale(CSYNC_TEST_DIRS);
    int rc;

    rc = _tmkdir(this_dir, MKDIR_MASK);
    assert_int_equal(rc, 0);
    rc = csync_vio_mkdirs(csync, CSYNC_TEST_DIRS, MKDIR_MASK);
    assert_int_equal(rc, 0);

    rc = _tstat(stat_dir, &sb);
    assert_int_equal(rc, 0);

    _trmdir(stat_dir);
    c_free_locale_string(this_dir);
    c_free_locale_string(stat_dir);
}
Example #4
0
bool DeleteFolder(const KString& FolderName, bool bRecursive, bool bSafe)
{
	if(!CleanFolder(FolderName, bRecursive, bSafe))
		return false;

	if(!GetCurrentFolder().CollateNoCase(SlashedFolderName(FolderName)))
		_tchdir(TEXT(".."));

	if(_trmdir(UnslashedFolderName(FolderName)))
	{
		if(!bSafe)
		{
			INITIATE_DEFINED_CODE_FAILURE(	(KString)TEXT("Error deleteing folder \"") +
												FolderName +
												TEXT("\""),
											errno);
		}

		return false;
	}

	return true;
}
Example #5
0
int c_rmdirs(const char *path) {
  _TDIR *d;
  struct _tdirent *dp;
  csync_stat_t sb;
  char *fname = NULL;
  mbchar_t *wfname = NULL;
  mbchar_t *wpath = c_utf8_to_locale(path);
  char *rd_name = NULL;

  if ((d = _topendir(wpath)) != NULL) {
    while( _tstat(wpath, &sb) == 0) {
      /* if we can remove the directory we're done */
      if (_trmdir(wpath) == 0) {
        break;
      }
      switch (errno) {
        case ENOTEMPTY:
        case EEXIST:
        case EBADF:
          break; /* continue */
        default:
          _tclosedir(d);
	  c_free_locale_string(wpath);
          return 0;
      }

      while ((dp = _treaddir(d)) != NULL) {
        size_t len;
	rd_name = c_utf8_from_locale(dp->d_name);
        /* skip '.' and '..' */
	if( c_streq( rd_name, "." ) || c_streq( rd_name, ".." ) ) {
	  c_free_locale_string(rd_name);
          continue;
        }

        len = strlen(path) + strlen(rd_name) + 2;
        fname = c_malloc(len);
        if (fname == NULL) {
          _tclosedir(d);
	  c_free_locale_string(rd_name);
	  c_free_locale_string(wpath);
          return -1;
        }
        snprintf(fname, len, "%s/%s", path, rd_name);
	wfname = c_utf8_to_locale(fname);

        /* stat the file */
        if (_tstat(wfname, &sb) != -1) {
#ifdef __unix__
          if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode)) {
#else
          if (S_ISDIR(sb.st_mode)) {
#endif
            if (_trmdir(wfname) < 0) { /* can't be deleted */
              if (errno == EACCES) {
                _tclosedir(d);
                SAFE_FREE(fname);
		c_free_locale_string(wfname);
		c_free_locale_string(rd_name);
		c_free_locale_string(wpath);
                return -1;
              }
              c_rmdirs(fname);
            }
          } else {
            _tunlink(wfname);
          }
        } /* lstat */
        SAFE_FREE(fname);
	c_free_locale_string(wfname);
	c_free_locale_string(rd_name);
      } /* readdir */

      _trewinddir(d);
    }
  } else {
    c_free_locale_string(wpath);
    return -1;
  }

  c_free_locale_string(wpath);
  _tclosedir(d);
  return 0;
}