コード例 #1
0
ファイル: gtest-filepath.cpp プロジェクト: wllxyz/study
// Returns true if pathname describes a directory in the file-system
// that exists.
bool FilePath::DirectoryExists() const {
  bool result = false;
#if GTEST_OS_WINDOWS
  // Don't strip off trailing separator if path is a root directory on
  // Windows (like "C:\\").
  const FilePath& path(IsRootDirectory() ? *this :
                                           RemoveTrailingPathSeparator());
#else
  const FilePath& path(*this);
#endif

#if GTEST_OS_WINDOWS_MOBILE
  LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
  const DWORD attributes = GetFileAttributes(unicode);
  delete [] unicode;
  if ((attributes != kInvalidFileAttributes) &&
      (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
    result = true;
  }
#else
  posix::StatStruct file_stat;
  result = posix::Stat(path.c_str(), &file_stat) == 0 &&
      posix::IsDir(file_stat);
#endif  // GTEST_OS_WINDOWS_MOBILE

  return result;
}
コード例 #2
0
ファイル: fsdos.c プロジェクト: RealityFactory/Genesis3D
static	void *	GENESISCC FSDos_Open(
	geVFile *		FS,
	void *			Handle,
	const char *	Name,
	void *			Context,
	unsigned int 	OpenModeFlags)
{
	DosFile *	DosFS;
	DosFile *	NewFile;
	char		Buff[_MAX_PATH];
	int			Length;
	char *		NamePtr;

	DosFS = Handle;

	if	(DosFS && DosFS->IsDirectory != GE_TRUE)
		return NULL;

	NewFile = geRam_Allocate(sizeof(*NewFile));
	if	(!NewFile)
		return NewFile;

	memset(NewFile, 0, sizeof(*NewFile));

	if	(BuildFileName(DosFS, Name, Buff, &NamePtr, sizeof(Buff)) == GE_FALSE)
		goto fail;

	Length = strlen(Buff);
	NewFile->FullPath = geRam_Allocate(Length + 1);
	if	(!NewFile->FullPath)
		goto fail;

	NewFile->Name = NewFile->FullPath + (NamePtr - &Buff[0]);

	memcpy(NewFile->FullPath, Buff, Length + 1);

	if	(OpenModeFlags & GE_VFILE_OPEN_DIRECTORY)
	{
		WIN32_FIND_DATA	FileInfo;
		HANDLE			FindHandle;
		geBoolean		IsDirectory;

		assert(!DosFS || DosFS->IsDirectory == GE_TRUE);

		memset(&FileInfo, 0, sizeof(FileInfo));
		FindHandle = FindFirstFile(NewFile->FullPath, &FileInfo);
		if	(FindHandle != INVALID_HANDLE_VALUE &&
			 FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			IsDirectory = GE_TRUE;
		}
		else
		{
			IsDirectory = IsRootDirectory(NewFile->FullPath);
		}

		FindClose (FindHandle);

		if	(OpenModeFlags & GE_VFILE_OPEN_CREATE)
		{
			if	(IsDirectory == GE_TRUE)
				goto fail;

			if	(CreateDirectory(NewFile->FullPath, NULL) == FALSE)
				goto fail;
		}
		else
		{
			if	(IsDirectory != GE_TRUE)
				goto fail;
		}

		NewFile->IsDirectory = GE_TRUE;
		NewFile->FileHandle = INVALID_HANDLE_VALUE;
	}
	else
	{
		DWORD			ShareMode=0;
		DWORD			CreationMode;
		DWORD			Access=0;
		DWORD			LastError;

		CreationMode = OPEN_EXISTING;

		switch	(OpenModeFlags & (GE_VFILE_OPEN_READONLY |
								  GE_VFILE_OPEN_UPDATE	 |
								  GE_VFILE_OPEN_CREATE))
		{
		case	GE_VFILE_OPEN_READONLY:
			Access = GENERIC_READ;
			ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
			break;

		case	GE_VFILE_OPEN_CREATE:
			CreationMode = CREATE_ALWAYS;
			// Fall through

		case	GE_VFILE_OPEN_UPDATE:
			Access = GENERIC_READ | GENERIC_WRITE;
			ShareMode = FILE_SHARE_READ;
			break;

		default:
			assert(!"Illegal open mode flags");
			break;
		}

		NewFile->FileHandle = CreateFile(NewFile->FullPath,
										 Access,
										 ShareMode,
										 NULL,
										 CreationMode,
										 0,
										 NULL);
		if	(NewFile->FileHandle == INVALID_HANDLE_VALUE)
			{
				LastError = GetLastError();
				goto fail;
			}
	}

	NewFile->Signature = DOSFILE_SIGNATURE;

	return (void *)NewFile;

fail:
	if	(NewFile->FullPath)
		geRam_Free(NewFile->FullPath);
	geRam_Free(NewFile);
	return NULL;
}
コード例 #3
0
ファイル: w95io.c プロジェクト: bringhurst/vbox
PRInt32
_PR_MD_GETFILEINFO64(const char *fn, PRFileInfo64 *info)
{
    HANDLE hFindFile;
    WIN32_FIND_DATA findFileData;
    char pathbuf[MAX_PATH + 1];
    
    if (NULL == fn || '\0' == *fn) {
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
        return -1;
    }

    /*
     * FindFirstFile() expands wildcard characters.  So
     * we make sure the pathname contains no wildcard.
     */
    if (NULL != _mbspbrk(fn, "?*")) {
        PR_SetError(PR_FILE_NOT_FOUND_ERROR, 0);
        return -1;
    }

    hFindFile = FindFirstFile(fn, &findFileData);
    if (INVALID_HANDLE_VALUE == hFindFile) {
        DWORD len;
        char *filePart;

        /*
         * FindFirstFile() does not work correctly on root directories.
         * It also doesn't work correctly on a pathname that ends in a
         * slash.  So we first check to see if the pathname specifies a
         * root directory.  If not, and if the pathname ends in a slash,
         * we remove the final slash and try again.
         */

        /*
         * If the pathname does not contain ., \, and /, it cannot be
         * a root directory or a pathname that ends in a slash.
         */
        if (NULL == _mbspbrk(fn, ".\\/")) {
            _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
            return -1;
        } 
        len = GetFullPathName(fn, sizeof(pathbuf), pathbuf,
                &filePart);
        if (0 == len) {
            _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
            return -1;
        }
        if (len > sizeof(pathbuf)) {
            PR_SetError(PR_NAME_TOO_LONG_ERROR, 0);
            return -1;
        }
        if (IsRootDirectory(pathbuf, sizeof(pathbuf))) {
            info->type = PR_FILE_DIRECTORY;
            info->size = 0;
            /*
             * These timestamps don't make sense for root directories.
             */
            info->modifyTime = 0;
            info->creationTime = 0;
            return 0;
        }
        if (!_PR_IS_SLASH(pathbuf[len - 1])) {
            _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
            return -1;
        } else {
            pathbuf[len - 1] = '\0';
            hFindFile = FindFirstFile(pathbuf, &findFileData);
            if (INVALID_HANDLE_VALUE == hFindFile) {
                _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
                return -1;
            }
        }
    }

    FindClose(hFindFile);

    if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
        info->type = PR_FILE_DIRECTORY;
    } else {
        info->type = PR_FILE_FILE;
    }

    info->size = findFileData.nFileSizeHigh;
    info->size = (info->size << 32) + findFileData.nFileSizeLow;

    _PR_FileTimeToPRTime(&findFileData.ftLastWriteTime, &info->modifyTime);

    if (0 == findFileData.ftCreationTime.dwLowDateTime &&
            0 == findFileData.ftCreationTime.dwHighDateTime) {
        info->creationTime = info->modifyTime;
    } else {
        _PR_FileTimeToPRTime(&findFileData.ftCreationTime,
                &info->creationTime);
    }

    return 0;
}