예제 #1
0
int stat(const char* filename, struct stat* Stat) {
    
    int ret = -1;
    DWORD lpFileSizeHigh    = 0;
    DWORD dwSize            = 0;
    WCHAR* wfile            = NULL;

    if (filename == NULL) {
        LOG.error("wm stat: the filename is null");
        goto finally;
    }    
    WIN32_FILE_ATTRIBUTE_DATA wdata;
    wfile = toWideChar(filename);    
    
    BOOL res = GetFileAttributesEx(wfile, GetFileExInfoStandard, &wdata);
    
    if (res == 0) {
        LOG.error("wm error in GetFileAttributesEx: %i", GetLastError());
        goto finally;
    }

    Stat->st_mode =  (unsigned short)wdata.dwFileAttributes;
    Stat->st_mtime = FileTimeToTime_t(wdata.ftLastWriteTime);  // only write: it is the modification
    Stat->st_atime = FileTimeToTime_t(wdata.ftLastAccessTime); // both read/write
    Stat->st_ctime = FileTimeToTime_t(wdata.ftCreationTime); 
    Stat->st_size = wdata.nFileSizeLow;
    if (Stat->st_size == 0xFFFFFFFF) {
        Stat->st_size = 0;
        LOG.info("wm: the filesize low is 0xFFFFFFFF");
    } 

    ret = 0;

finally:
    
    if (ret == -1) {
        Stat->st_atime = 0;
        Stat->st_ctime = 0;
        Stat->st_mode  = 0;
        Stat->st_mtime = 0;
        Stat->st_size = -1;
    }
    
    delete [] wfile;

    return ret;
}
예제 #2
0
파일: dir.c 프로젝트: dong777/baidupcs
LocalFileInfo *GetLocalFileInfo(const char *file)
{
	LocalFileInfo *info = NULL;
#ifdef WIN32
	WIN32_FIND_DATA fd;
	HANDLE hFind = FindFirstFile(file, &fd);
	FindClose(hFind);
	if (hFind != INVALID_HANDLE_VALUE) {
		if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) /*为目录*/
			info = CreateLocalFileInfo(file, NULL, 1, FileTimeToTime_t(fd.ftLastWriteTime, NULL), 0, NULL);
		else /*为文件*/
			info = CreateLocalFileInfo(file, NULL, 0, FileTimeToTime_t(fd.ftLastWriteTime, NULL), fd.nFileSizeLow, NULL);
	}
#else
	struct stat st;
	if (stat(file, &st))
		return NULL;
	if (S_ISDIR(st.st_mode)) /*为目录*/
		info = CreateLocalFileInfo(file, NULL, 1, st.st_mtime, 0, NULL);
	else if (S_ISREG(st.st_mode)) /*为文件*/
		info = CreateLocalFileInfo(file, NULL, 0, st.st_mtime, st.st_size, NULL);
#endif
	return info;
}