Exemple #1
0
static void check_csync_vio_utimes(void **state)
{
    CSYNC *csync = *state;
    csync_stat_t sb;
    struct timeval times[2];
    long modtime = 0;
    mbchar_t *file = c_utf8_to_locale(CSYNC_TEST_FILE);
    int rc;

    rc = _tstat(file, &sb);
    assert_int_equal(rc, 0);
    modtime = sb.st_mtime + 10;

    times[0].tv_sec = modtime;
    times[0].tv_usec = 0;

    times[1].tv_sec = modtime;
    times[1].tv_usec = 0;

    rc = csync_vio_utimes(csync, CSYNC_TEST_FILE, times);
    assert_int_equal(rc, 0);

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

    assert_int_equal(modtime, sb.st_mtime);

    c_free_locale_string(file);
}
Exemple #2
0
int c_mkdirs(const char *path, mode_t mode) {
  int tmp;
  csync_stat_t sb;
  mbchar_t *wpath = c_utf8_to_locale(path);
  mbchar_t *swpath = NULL;

  if (path == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (_tstat(wpath, &sb) == 0) {
    if (! S_ISDIR(sb.st_mode)) {
      errno = ENOTDIR;
      c_free_locale_string(wpath);
      return -1;
    }
  }

  tmp = strlen(path);
  while(tmp > 0 && path[tmp - 1] == '/') --tmp;
  while(tmp > 0 && path[tmp - 1] != '/') --tmp;
  while(tmp > 0 && path[tmp - 1] == '/') --tmp;

  if (tmp > 0) {
    char subpath[tmp + 1];
    memcpy(subpath, path, tmp);
    subpath[tmp] = '\0';
    swpath = c_utf8_to_locale(subpath);
    if (_tstat(swpath, &sb) == 0) {
      if (! S_ISDIR(sb.st_mode)) {
	c_free_locale_string(swpath);
	c_free_locale_string(wpath);
        errno = ENOTDIR;
        return -1;
      }
    } else if (errno != ENOENT) {
      c_free_locale_string(wpath);
      c_free_locale_string(swpath);
      return -1;
    } else if (c_mkdirs(subpath, mode) < 0) {
      c_free_locale_string(swpath);
      c_free_locale_string(wpath);
      return -1;
    }
  }
  tmp = _tmkdir(wpath, mode);
  c_free_locale_string(swpath);
  c_free_locale_string(wpath);

  if ((tmp < 0) && (errno == EEXIST)) {
    return 0;
  }
  return tmp;
}
Exemple #3
0
int c_mkdirs(const char *path, mode_t mode) {
  int tmp;
  csync_stat_t sb;
  const _TCHAR *wpath = c_multibyte(path);
  const _TCHAR *swpath = NULL;
  
  if (path == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (_tstat(wpath, &sb) == 0) {
    if (! S_ISDIR(sb.st_mode)) {
      errno = ENOTDIR;
      return -1;
    }
  }
  
  tmp = strlen(path);
  while(tmp > 0 && path[tmp - 1] == '/') --tmp;
  while(tmp > 0 && path[tmp - 1] != '/') --tmp;
  while(tmp > 0 && path[tmp - 1] == '/') --tmp;

  if (tmp > 0) {
    char subpath[tmp + 1];
    memcpy(subpath, path, tmp);
    subpath[tmp] = '\0';
    swpath = c_multibyte(subpath);
    if (_tstat(swpath, &sb) == 0) {
      if (! S_ISDIR(sb.st_mode)) {
        errno = ENOTDIR;
        return -1;
      }
    } else if (errno != ENOENT) {
      c_free_multibyte(swpath);
      return -1;
    } else if (c_mkdirs(subpath, mode) < 0) {
      c_free_multibyte(swpath);
      return -1;
    }
  }
#ifdef _WIN32
  tmp = _tmkdir(wpath);
#else
  tmp = _tmkdir(wpath, mode);
#endif
  c_free_multibyte(swpath);
  c_free_multibyte(wpath);

  if ((tmp < 0) && (errno == EEXIST)) {
    return 0;
  }
  return tmp;
}
Exemple #4
0
// Exported hook functions
int my_stat( const char *path, struct my_stat *st )
{
	DISABLE_ERRORS;

	auto tpath = tstr(path);
	int result;

	if(*tpath.get() == 0) {
		/// virtual root
		memset( st, 0, sizeof(struct my_stat) );
		st->st_mode = _S_IFDIR;
		result = 0;
		my_errno = 0;
	} else {
		result = _tstat( MRP(tpath.get()), (struct _stat *)st );
		if(result < 0) {
			my_errno = errno;
		} else {
			my_errno = 0;
		}
	}

	D(bug(TEXT("stat(%s,%s) = %d\n"), tpath.get(), MRP(tpath.get()), result));
	if(result >= 0) dump_stat( st );
	RESTORE_ERRORS;
	return result;
}
Exemple #5
0
int Synchronizer::RebuildIndex()
{
    indexDiscarded = false;
    // save sync file timestamp
    _tstat(syncfilepath, &syncfileTimestamp);
    return PDFSYNCERR_SUCCESS;
}
Exemple #6
0
static void check_logging(void **state)
{
    int rc;
    csync_stat_t sb;
    mbchar_t *path;
    path = c_utf8_path_to_locale("/tmp/check_csync1/cb_called");

    (void) state; /* unused */

    assert_non_null(path);

    rc = csync_set_log_level(1);
    assert_int_equal(rc, 0);

    rc = csync_set_log_callback(check_log_callback);
    assert_int_equal(rc, 0);

    csync_log(1, __func__, "rc = %d", rc);

    rc = _tstat(path, &sb);

    c_free_locale_string(path);

    assert_int_equal(rc, 0);
}
Exemple #7
0
static void check_csync_vio_rename_dir(void **state)
{
    CSYNC *csync = *state;
    csync_stat_t sb;
    int rc;

    mbchar_t *dir = c_utf8_to_locale("test");
    mbchar_t *dir2 = c_utf8_to_locale("test2");

    assert_non_null(dir);
    assert_non_null(dir2);

    rc = _tmkdir(dir, MKDIR_MASK);
    assert_int_equal(rc, 0);

    rc = csync_vio_rename(csync, "test", "test2");
    assert_int_equal(rc, 0);


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

    c_free_locale_string(dir);
    c_free_locale_string(dir2);
}
Exemple #8
0
static void stat_local_file( csync_stat_t *sb, const char *file )
{
    const _TCHAR *mpath = NULL;
    mpath = c_multibyte(file);
    assert_int_not_equal(_tstat(mpath, sb), -1);
    c_free_multibyte(mpath);
}
Exemple #9
0
int
our_stat(char *filename, struct stat *sbuf)
{
#ifdef _WINDOWS
    LPTSTR f = NULL;
    int    ret = -1;
    struct _stat s;

    f = utf8_to_lptstr((LPSTR) filename);
    if(f){
	ret = _tstat(f, &s);

	sbuf->st_dev = s.st_dev;
	sbuf->st_ino = s.st_ino;
	sbuf->st_mode = s.st_mode;
	sbuf->st_nlink = s.st_nlink;
	sbuf->st_uid = s.st_uid;
	sbuf->st_gid = s.st_gid;
	sbuf->st_rdev = s.st_rdev;
	sbuf->st_size = s.st_size;
	sbuf->st_atime = (time_t) s.st_atime;
	sbuf->st_mtime = (time_t) s.st_mtime;
	sbuf->st_ctime = (time_t) s.st_ctime;

	fs_give((void **) &f);
    }

    return ret;
#else /* UNIX */
    return(stat(fname_to_locale(filename), sbuf));
#endif /* UNIX */
}
Exemple #10
0
//-----------------------------------------------------------------------------
// 設定視窗標題
void SetConsoleWindowTile(IN const nstring &szTitle, IN const nstring &szVer, IN const nstring &szFile)
{
    nstring szWindowTile;

    if(szTitle.empty() == false)
        szWindowTile += nsoutf(__T("{} ")) << szTitle;

    if(szVer.empty() == false)
        szWindowTile += nsoutf(__T("[Ver:{}]")) << szVer;

    if(szFile.empty() == false)
    {
        struct _stat sStat;
        struct tm sTMTime;
        TCHAR szTime[32];

        _tstat(szFile.c_str(), &sStat);
        localtime_s(&sTMTime, &sStat.st_mtime);
        _tasctime_s(szTime, _countof(szTime), &sTMTime);

        szWindowTile += nsoutf(__T("[FileTime:{}]")) << szTime;
    }//if

    szWindowTile += nsoutf(__T("[PID:{}]")) << _getpid();

    SetConsoleTitle(szWindowTile.c_str());
}
Exemple #11
0
	static BOOL EnumProfilesForList(TCHAR *tszFullPath, TCHAR *profile, LPARAM lParam)
	{
		ProfileEnumData *ped = (ProfileEnumData*)lParam;
		CCtrlListView &list = ped->list;

		TCHAR sizeBuf[64];
		bool bFileLocked = true;

		TCHAR *p = _tcsrchr(profile, '.');
		mir_tstrcpy(sizeBuf, _T("0 KB"));
		if (p != NULL) *p = 0;

		LVITEM item = { 0 };
		item.mask = LVIF_TEXT | LVIF_IMAGE;
		item.pszText = profile;
		item.iItem = 0;

		struct _stat statbuf;
		if (_tstat(tszFullPath, &statbuf) == 0) {
			if (statbuf.st_size > 1000000) {
				mir_sntprintf(sizeBuf, _countof(sizeBuf), _T("%.3lf"), (double)statbuf.st_size / 1048576.0);
				mir_tstrcpy(sizeBuf + 5, _T(" MB"));
			}
			else {
				mir_sntprintf(sizeBuf, _countof(sizeBuf), _T("%.3lf"), (double)statbuf.st_size / 1024.0);
				mir_tstrcpy(sizeBuf + 5, _T(" KB"));
			}
			bFileLocked = !fileExist(tszFullPath);
		}

		DATABASELINK *dblink;
		switch (touchDatabase(tszFullPath, &dblink)) {
		case ERROR_SUCCESS:
			item.iImage = bFileLocked;
			break;

		case EGROKPRF_OBSOLETE:
			item.iImage = 2;
			break;

		default:
			item.iImage = 3;
		}

		int iItem = list.InsertItem(&item);
		if (mir_tstrcmpi(ped->szProfile, tszFullPath) == 0)
			list.SetItemState(iItem, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

		list.SetItemText(iItem, 2, sizeBuf);

		if (dblink != NULL) {
			if (bFileLocked) // file locked
				list.SetItemText(iItem, 1, TranslateT("<In use>"));
			else
				list.SetItemText(iItem, 1, TranslateTS(dblink->szFullName));
		}
		else list.SetItemText(iItem, 1, TranslateT("<Unknown format>"));

		return TRUE;
	}
Exemple #12
0
bool BrainUtil::DoesLocalFolderExist(const CString& folderFullName)
{
    CString tmpFolderFullName = folderFullName;

    struct _stat stFileInfo;
    bool blnReturn;
    int intStat;

    // Attempt to get the file attributes
    intStat = _tstat(tmpFolderFullName.GetBuffer(), &stFileInfo);
    if(intStat == 0 && (stFileInfo.st_mode & S_IFDIR) != 0) {
        // We were able to get the file attributes
        // so the file obviously exists.
        blnReturn = true;
    } else {
        // We were not able to get the file attributes.
        // This may mean that we don't have permission to
        // access the folder which contains this file. If you
        // need to do that level of checking, lookup the
        // return values of stat which will give you
        // more details on why stat failed.
        blnReturn = false;
    }

    return(blnReturn); 
}
Exemple #13
0
bool CProperties::CreatePathToFile(LPCTSTR pszDir) 
{
  // Create intermediate directories
#ifdef _WIN32
  const TCHAR cSep='\\';
#else // UNIX
  const TCHAR cSep='/';
#endif
  for(LPCTSTR c=_tcschr(pszDir,cSep);c;c=_tcschr(c+1,cSep)){
#ifdef _WIN32
    if(c==pszDir+2 && _istalpha(pszDir[0]) && _TCHAR(':')==pszDir[1]){
      continue; // don't attempt to create "C:"
    }
#endif
    String strDir(pszDir,c-pszDir);
    struct _stat buf;
    if(!(0==_tstat(strDir,&buf) && (S_IFDIR&buf.st_mode))){
      // Need to create directory
      bool b=(0==_tmkdir(strDir));
      TRACE(_T("Create directory %s rc=%d\n"),(LPCTSTR)strDir,b);
      if(!b){
        return false;
      }
    }
  }
  return true;
}
Exemple #14
0
void
RunDisconnectScript(connection_t *c, int run_as_service)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    TCHAR cmdline[256];
    DWORD exit_code;
    struct _stat st;
    int i;

    /* Cut off extention from config filename and add "_down.bat" */
    int len = _tcslen(c->config_file) - _tcslen(o.ext_string) - 1;
    _sntprintf_0(cmdline, _T("%s\\%.*s_down.bat"), c->config_dir, len, c->config_file);

    /* Return if no script exists */
    if (_tstat(cmdline, &st) == -1)
        return;

    if (!run_as_service)
        SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONN_SCRIPT));

    CLEAR(si);
    CLEAR(pi);

    /* fill in STARTUPINFO struct */
    GetStartupInfo(&si);
    si.cb = sizeof(si);
    si.dwFlags = 0;
    si.wShowWindow = SW_SHOWDEFAULT;
    si.hStdInput = NULL;
    si.hStdOutput = NULL;

    /* make an env array with confg specific env appended to the process's env */
    WCHAR *env = c->es ? merge_env_block(c->es) : NULL;
    DWORD flags = CREATE_UNICODE_ENVIRONMENT;

    if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,
                       (o.show_script_window ? flags|CREATE_NEW_CONSOLE : flags|CREATE_NO_WINDOW),
                       NULL, c->config_dir, &si, &pi))
    {
        free(env);
        return;
    }

    for (i = 0; i <= (int) o.disconnectscript_timeout; i++)
    {
        if (!GetExitCodeProcess(pi.hProcess, &exit_code))
            goto out;

        if (exit_code != STILL_ACTIVE)
            goto out;

        Sleep(1000);
    }
out:
    free(env);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}
Exemple #15
0
bool CTestUtils::IsDirectory(CStdString& sPath)
{
	struct _stat s;
	if(_tstat(sPath.c_str(), &s) == -1)
		return false; // not a directory or a file as it doesn't exist

	return (s.st_mode & _S_IFDIR ) != 0;
}
Exemple #16
0
__int64 WizGetFileSize(LPCTSTR lpszFileName)
{
	struct _stat  s;
	if (0 != _tstat(lpszFileName, &s))
		return 0;
	//
	return s.st_size;
}
Exemple #17
0
/**
 * @brief Check if the given file exists.
 * @param [in] file Full path to the file to check.
 * @return true if the file exists, false if file is not found.
 */
bool paths_DoesFileExist(LPCTSTR file)
{
	struct _stat st;
	if (_tstat(file, &st) == 0)
		return true;
	else
		return false;
}
Exemple #18
0
static BOOL IsADir(const CString& strName)
{
    struct _stat statbuf;

    if (_tstat(strName, &statbuf) < 0)
	return FALSE;

    return statbuf.st_mode & _S_IFDIR;
}
Exemple #19
0
bool CUtils::CopyFile(LPCTSTR pszSource,LPCTSTR pszDest)
{
  // Compare the files.  First set rc to the result of the comparison (true if the same)
  bool rc=false;

  struct _stat s1,s2;
  if(-1!=_tstat(pszSource,&s1) && -1!=_tstat(pszDest,&s2) && s1.st_size==s2.st_size){
    // Files both exist and are of equal size
    FILE *f1=_tfopen(pszSource,_T("rb"));
    if(f1){
      FILE *f2=_tfopen(pszDest,_T("rb"));
      if(f2){
        int nSize1,nSize2;
        rc=true;
        do{
          char buf1[4096],buf2[4096];
          nSize1=fread(buf1,1,sizeof buf1,f1);
          nSize2=fread(buf2,1,sizeof buf2,f2);
          if(nSize1!=nSize2 || 0!=memcmp(buf1,buf2,nSize1)){
            rc=false;
            break;
          }
        } while (nSize1>0);
        fclose(f2);
      }
      fclose(f1);
    }
  }

  if(rc){
    // Files are identical
    TRACE(_T("Copy not necessary: '%s' to '%s'\n"),pszSource,pszDest);
  } else {
    rc=TRUE==::CopyFile(pszSource,pszDest,FALSE);
    if(rc){
      TRACE(_T("Copied '%s' to '%s'\n"),pszSource,pszDest);
    } else {
      MessageBoxF(_T("Failed to copy '%s' to '%s' - %s"),pszSource,pszDest,GetLastErrorMessageString());
    }
  }

  return rc;
}
Exemple #20
0
int c_isdir(const char *path) {
  csync_stat_t sb;
  const _TCHAR *wpath = c_multibyte(path);

  if (_tstat (wpath, &sb) == 0 && S_ISDIR(sb.st_mode)) {
    return 1;
  }

  return 0;
}
Exemple #21
0
DWORD CUtilities::PowerGetFileSize(const TCHAR *file)
{
	struct _stat buf;
	int result = 0;
	result = _tstat( file, &buf );
	if( result != 0 )
		return 0;
	else
		return buf.st_size;	
}
int CSmtpClient::CheckAttachmentOK(CString sFileName)
{
  struct _stat st;  

  int nResult = _tstat(sFileName, &st);
  if(nResult != 0)
    return 1;  // File not found.

  return 0;
}
	void ArchiveEditable::update()
	{
		for(ListItr i = list_.begin(); i != list_.end(); ++i) {
			struct _stat st;
			if( 0 == _tstat(i->id.c_str(), &st) && (i->time != st.st_mtime || i->size != st.st_size) && i->attr != EntryAttr::NEW) {
				i->attr = EntryAttr::UPDATE;
				i->time = st.st_mtime; i->size = st.st_size;
			}
		}
	}
Exemple #24
0
int c_isdir(const char *path) {
  csync_stat_t sb;
  mbchar_t *wpath = c_utf8_to_locale(path);
  int re = 0;

  if (_tstat (wpath, &sb) == 0 && S_ISDIR(sb.st_mode)) {
   re = 1;
  }
  c_free_locale_string(wpath);
  return re;
}
Exemple #25
0
	void list_OnGetTip(CCtrlListView::TEventInfo *evt)
	{
		if (auto pTip = evt->nmlvit) {
			TCHAR profilename[MAX_PATH], tszFullPath[MAX_PATH];
			struct _stat statbuf;
			m_profileList.GetItemText(pTip->iItem, 0, profilename, _countof(profilename));
			mir_sntprintf(tszFullPath, _countof(tszFullPath), _T("%s\\%s\\%s.dat"), m_pd->ptszProfileDir, profilename, profilename);
			_tstat(tszFullPath, &statbuf);
			mir_sntprintf(pTip->pszText, pTip->cchTextMax, _T("%s\n%s: %s\n%s: %s"), tszFullPath, TranslateT("Created"), rtrimt(NEWTSTR_ALLOCA(_tctime(&statbuf.st_ctime))), TranslateT("Modified"), rtrimt(NEWTSTR_ALLOCA(_tctime(&statbuf.st_mtime))));
		}
	}
	void ArchiveEditable::forceUpdate(const _TCHAR *fn)
	{
		ListItr li = find(fn);
		if(li != list_.end() && li->attr != EntryAttr::NEW) {
			struct _stat st;
			if(0 == _tstat(fn, &st)) {
				li->attr = EntryAttr::UPDATE;
				li->time = st.st_mtime; li->size = st.st_size;
			}
		}
	}
bool SkinnedDialog::fileChanged()
{
	if (filename.size() <= 0)
		return false;

	struct _stat st = {0};
	if (_tstat(filename.c_str(), &st) != 0)
		return false;

	return st.st_mtime > fileChangedTime;
}
Exemple #28
0
int GetContextFileSize(const TCHAR* filePath)
{
	if(!filePath)
	{
		return 0;
	}
	struct _stat info;
	memset(&info,0,sizeof(info));
	_tstat(filePath, &info);
	return info.st_size;;

}
	void ArchiveEditable::add(const _TCHAR *fn)
	{
		ListItr li = find(fn);
		if(li == list_.end()) {
			struct _stat st;
			if( 0 == _tstat(fn, &st) ) {
				if(st.st_mode & _S_IFDIR) { // ディレクトリなら
					TDIR *dir; tdirent *ent;
					dir = topendir(fn);
					while(dir) {
						ent = treaddir(dir);
						if(!ent) break;
						if(!_tcscmp(ent->d_name, _T(".")) && !_tcscmp(ent->d_name, _T(".."))) {
							basic_string<_TCHAR> path(fn); (path += _T("/")) += ent->d_name;
							add(path.c_str());
							PRN(path.c_str());
						}
					}
					tclosedir(dir);
				}
				else {
					// 最後尾に追加
					ArchiveEntry newe(fn);
					newe.time = st.st_mtime;
					newe.size = st.st_size;
					if(list_.size() > 0) newe.pos = list_.back().pos+list_.back().size;
					else newe.pos = 0;
					list_.push_back(EntryAttr(newe, EntryAttr::NEW));
				}
			}
		}
		else {
			struct _stat st;
			if( 0 == _tstat(fn, &st) && (li->time != st.st_mtime || li->size != st.st_size)) {
				li->attr = EntryAttr::UPDATE;
				li->time = st.st_mtime; li->size = st.st_size;
			}
			else if(li->attr == EntryAttr::REMOVE) li->attr = EntryAttr::ALREADY;
		}
	}
static void check_csync_statedb_close(void **state)
{
    CSYNC *csync = *state;
    csync_stat_t sb;
    time_t modtime;
    mbchar_t *testdb = c_utf8_path_to_locale(TESTDB);
    int rc;

    /* statedb not written */
    csync_statedb_load(csync, TESTDB, &csync->statedb.db);

    rc = _tstat(testdb, &sb);
    assert_int_equal(rc, 0);
    modtime = sb.st_mtime;

    rc = csync_statedb_close(csync);
    assert_int_equal(rc, 0);

    rc = _tstat(testdb, &sb);
    assert_int_equal(rc, 0);
    assert_int_equal(modtime, sb.st_mtime);

    csync_statedb_load(csync, TESTDB, &csync->statedb.db);

    rc = _tstat(testdb, &sb);
    assert_int_equal(rc, 0);
    modtime = sb.st_mtime;

    /* wait a sec or the modtime will be the same */
    sleep(1);

    /* statedb written */
    rc = csync_statedb_close(csync);
    assert_int_equal(rc, 0);

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

    c_free_locale_string(testdb);
}