kbool_t knh_exists(CTX ctx, const char *fname) { kbool_t res = 0; if(fname == NULL || fname[0] == 0) return 0; #if defined(K_USING_WINDOWS_) DWORD attr = GetFileAttributesA(fname); res = (attr != -1); #elif defined(K_USING_POSIX_) struct stat buf; res = (stat(fname, &buf) != -1); #else #endif if(res == 0) { DBG_P("'%s' NOTFOUND", fname); } return res; }
bool FSExistDirectory(const char *dirName) { #if defined(_WIN32_WCE) return FSExistDirectory( UniString(dirName) ); #elif defined(WIN32) if ( dirName == NULL ) return false; if ( *dirName == 0 ) return false; DWORD attr = GetFileAttributesA(dirName); return ((attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY)); #else UniString uniString(dirName); return FSExistDirectory(uniString); #endif }
knh_bool_t knh_path_isdir(CTX ctx, knh_path_t *ph) { const char *pname = P_text(ph) + ph->pbody; if(pname[0] == 0) return 0; #if defined(K_USING_WINDOWS) DWORD attr = GetFileAttributesA(pname); if(attr == -1) return 0; return ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY); #elif defined(K_USING_POSIX_) struct stat buf; if(stat(pname, &buf) == -1) return 0; return S_ISDIR(buf.st_mode); #else // avoid "unused variable" warning unused variable (void)phname; return 0; #endif }
/* * __win_fs_exist -- * Return if the file exists. */ static int __win_fs_exist(WT_FILE_SYSTEM *file_system, WT_SESSION *wt_session, const char *name, bool *existp) { WT_DECL_RET; WT_SESSION_IMPL *session; WT_UNUSED(file_system); session = (WT_SESSION_IMPL *)wt_session; if (GetFileAttributesA(name) != INVALID_FILE_ATTRIBUTES) *existp = true; else *existp = false; return (0); }
/* ** Return TRUE if the named file exists. */ int sqlite3WinFileExists(const char *zFilename){ int exists = 0; void *zConverted = convertUtf8Filename(zFilename); if( zConverted==0 ){ return SQLITE_NOMEM; } if( isNT() ){ exists = GetFileAttributesW((WCHAR*)zConverted) != 0xffffffff; }else{ #if OS_WINCE return SQLITE_NOMEM; #else exists = GetFileAttributesA((char*)zConverted) != 0xffffffff; #endif } sqliteFree(zConverted); return exists; }
extern "C" __declspec(dllexport) bool ApplyPatchSuspended(HANDLE hProcess, DWORD) { // Get target file name char szTarget[MAX_PATH]; strncpy(szTarget, GetBWAPITarget().c_str(), MAX_PATH-1); szTarget[MAX_PATH-1] = '\0'; // Check if the file exists, INVALID_FILE_ATTRIBUTES will have this bit set too if ( GetFileAttributesA(GetBWAPITarget().c_str()) & FILE_ATTRIBUTE_DIRECTORY ) return BWAPIError("Unable to find %s.", szTarget); // Get the address for the LoadLibrary procedure HMODULE hKernalModule = GetModuleHandle(L"Kernel32"); if ( !hKernalModule ) return BWAPIError("Unable to get module handle for Kernel32."); LPTHREAD_START_ROUTINE loadLibAddress = (LPTHREAD_START_ROUTINE)GetProcAddress(hKernalModule, "LoadLibraryA" ); if ( !loadLibAddress ) return BWAPIError("Could not get Proc Address for LoadLibraryA."); // Create a remote allocation VAlloc alloc(hProcess, MAX_PATH); if ( !alloc ) return BWAPIError("Could not allocate memory for DLL path."); // Write the DLL path to the allocation if ( !alloc.Write(szTarget, MAX_PATH) ) return BWAPIError("Write process memory failed."); // Create a remote thread for LoadLibrary and pass the DLL path as a parameter RemoteThread thread(hProcess, loadLibAddress, alloc.GetAddress()); if ( !thread ) return BWAPIError("Unable to create remote thread."); // Wait for the thread to finish if ( !thread.Wait() ) return BWAPIError("WaitForSingleObject failed."); // The exit code for LoadLibrary is its return value, if it's NULL then loading failed if ( thread.GetExitCode() == NULL ) return BWAPIError("Injection failed.\nThis is caused when BWAPI crashes before injecting completely."); return true; //everything OK }
/* Check for Existence (or non-existence) of a file or group * When testing for existence of a group, groupName is not needed */ static void CheckFileExistsInProgramGroups(const char *nameToCheck, BOOL shouldExist, BOOL isGroup, const char *groupName, int testParams) { char path[MAX_PATH]; DWORD attributes; int len; lstrcpyA(path, ProgramsDir); len = strlen(path) + strlen(nameToCheck)+1; if (groupName != NULL) { len += strlen(groupName)+1; } ok (len <= MAX_PATH, "Path Too Long.%s\n", GetStringFromTestParams(testParams)); if (len <= MAX_PATH) { if (groupName != NULL) { strcat(path, "\\"); strcat(path, groupName); } strcat(path, "\\"); strcat(path, nameToCheck); attributes = GetFileAttributesA(path); if (!shouldExist) { ok (attributes == INVALID_FILE_ATTRIBUTES , "File exists and shouldn't %s.%s\n", path, GetStringFromTestParams(testParams)); } else { if (attributes == INVALID_FILE_ATTRIBUTES) { ok (FALSE, "Created File %s doesn't exist.%s\n", path, GetStringFromTestParams(testParams)); } else if (isGroup) { ok (attributes & FILE_ATTRIBUTE_DIRECTORY, "%s is not a folder (attr=%x).%s\n", path, attributes, GetStringFromTestParams(testParams)); } else { ok (attributes & FILE_ATTRIBUTE_ARCHIVE, "Created File %s has wrong attributes (%x).%s\n", path, attributes, GetStringFromTestParams(testParams)); } } } }
bool FSExistFile(const char *fileName) { #if defined(_WIN32_WCE) return (GetFileAttributes(UniString(fileName)) != INVALID_FILE_ATTRIBUTES); #elif defined(WIN32) if ( fileName == NULL ) return false; if ( *fileName == 0 ) return false; // return PathFileExistsA(fileName); return (GetFileAttributesA(fileName) != INVALID_FILE_ATTRIBUTES); // return _access(fileName, 0) == 0; #else UniString uniString(fileName); return FSExistFile(uniString); #endif }
BOOL PDLAPI LFile::Exists( __in PCSTR lpszFileName, __in BOOL bIncludeDir) { PDLASSERT(NULL != lpszFileName); #ifndef _WIN32_WCE DWORD dwAttr = GetFileAttributesA(lpszFileName); if (INVALID_FILE_ATTRIBUTES == dwAttr) return FALSE; if (bIncludeDir) return TRUE; else return 0 == (FILE_ATTRIBUTE_DIRECTORY & dwAttr); #else LStringW str = lpszFileName; return Exists(str); #endif // _WIN32_WCE }
//////////////////////////////////////////////////////////////////////////////////// /// /// \param filename The file to check for. /// /// \return True 1 if the file exists, otherwise 0. /// //////////////////////////////////////////////////////////////////////////////////// int FileIO::fileExists(const char * filename) { int result = false; #ifdef WIN32 if (GetFileAttributesA(filename) != /*INVALID_FILE_ATTRIBUTES*/ -1) { result = true; } #else FILE * file = fopen(filename, "rb"); if (file) { fclose(file); result = true; } #endif return result; }
//------------------------------------------// // FileHelper::CheckFolderExists //------------------------------------------// bool FileHelper::CheckFolderExists(const std::string& pDirectory) { //http://stackoverflow.com/questions/8233842/how-to-check-if-directory-exist-using-c-and-winapi #if __WINDOWS__ DWORD ftyp = GetFileAttributesA(pDirectory.c_str()); if (ftyp == INVALID_FILE_ATTRIBUTES) { return false; //something is wrong with your path! } if (ftyp & FILE_ATTRIBUTE_DIRECTORY) { return true; // this is a directory! } #endif return false; }
bool dirExists(const std::string& dirName) { #ifdef _WIN32 DWORD ftyp = GetFileAttributesA(dirName.c_str()); if (ftyp == INVALID_FILE_ATTRIBUTES) return false; //something is wrong with your path! if (ftyp & FILE_ATTRIBUTE_DIRECTORY) return true; // this is a directory! #else struct stat sb; if (stat(dirName.c_str(), &sb)==0 && S_ISDIR(sb.st_mode)) { return true; } #endif return false; }
bool __fastcall mpqapi_set_hidden(char *pszArchive, bool hidden) { char *v2; // edi BOOL v3; // esi DWORD v4; // eax bool result; // al DWORD v6; // esi v2 = pszArchive; v3 = hidden; v4 = GetFileAttributesA(pszArchive); if ( v4 == -1 ) return GetLastError() == ERROR_FILE_NOT_FOUND; v6 = v3 != 0 ? FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN : 0; if ( v4 == v6 ) result = 1; else result = SetFileAttributesA(v2, v6); return result; }
uint GetFileAttr(const char *Name,const wchar *NameW) { #ifdef _WIN_ALL if (WinNT() && NameW!=NULL && *NameW!=0) return(GetFileAttributesW(NameW)); else return(GetFileAttributesA(Name)); #elif defined(_DJGPP) return(_chmod(Name,0)); #else struct stat st; if (stat(Name,&st)!=0) return(0); #ifdef _EMX return(st.st_attr); #else return(st.st_mode); #endif #endif }
// Check if a specified file exist bool af_IsFileExist(const char * szRelativePath) { // we must supply a relative path to GetFilePck function AFilePackage * pPackage = g_AFilePackMan.GetFilePck(szRelativePath); if( pPackage ) { // Get file entry AFilePackage::FILEENTRY FileEntry; int iEntryIndex; if (pPackage->GetFileEntry(szRelativePath, &FileEntry, &iEntryIndex)) return true; } { // not found in package, so test if exist on the disk, here we must use full path char szFullPath[1024]; sprintf(szFullPath, "%s/%s", g_szBaseDir, szRelativePath); #ifdef _WIN32 if( INVALID_FILE_ATTRIBUTES != GetFileAttributesA(szFullPath) ) return true; #else if (access(szFullPath, 0) == 0) return true; #endif } //try backup packages { AFilePackage * pPackage = g_AFilePackMan.GetFilePck(szRelativePath, true); if( pPackage ) { // Get file entry AFilePackage::FILEENTRY FileEntry; int iEntryIndex; if (pPackage->GetFileEntry(szRelativePath, &FileEntry, &iEntryIndex)) return true; } } return false; }
int serve_open(int *clientSocket, http_message *message, char *filepath) { // Check if file exists DWORD dwAttrib = GetFileAttributesA(filepath); BOOL isFile = (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); if (isFile) { int retval = (int) ShellExecuteA(0, "open", filepath, 0, 0, SW_SHOW); if (retval <= 32) return serve_error(clientSocket, "403 Forbidden (process creation error)"); else { // Send OK response const char http_response[] = "HTTP/1.0 201 Created\r\n\r\n201 Created"; return send(*clientSocket, http_response, strlen(http_response), 0); } } else return serve_error(clientSocket, "404 Not found"); }
MIR_CORE_DLL(int) CreateDirectoryTree(const char *szDir) { if (szDir == NULL) return 1; char szTestDir[MAX_PATH]; mir_strncpy(szTestDir, szDir, _countof(szTestDir)); DWORD dwAttributes = GetFileAttributesA(szTestDir); if (dwAttributes != INVALID_FILE_ATTRIBUTES && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) return 0; char *pszLastBackslash = strrchr(szTestDir, '\\'); if (pszLastBackslash == NULL) return 0; *pszLastBackslash = '\0'; CreateDirectoryTree(szTestDir); *pszLastBackslash = '\\'; return (CreateDirectoryA(szTestDir, NULL) == 0) ? GetLastError() : 0; }
kExitCode ExistMask(Arguments* args, char* suffix, uint32_t* p_result) { size_t i; char* filename_buf = (char*)alloca(args->strlen_longest_filename + (suffix == NULL ? 0 : strlen(suffix)) + 1); if (filename_buf == NULL) { return (kFailAlloca); } *p_result = 0; for (i = 0; i < args->num_filenames; ++i) { strcpy(filename_buf, args->p_filenames[i]); if (suffix != NULL) { strcat(filename_buf, suffix); } DWORD attrib = GetFileAttributesA(filename_buf); if (attrib != INVALID_FILE_ATTRIBUTES && !(attrib & FILE_ATTRIBUTE_DIRECTORY)) { *p_result |= (1 << i); } } return (kSuccess); }
bool directory_exists(const string path) { #ifdef _WIN32 DWORD ftyp = GetFileAttributesA(path.c_str()); if (ftyp == INVALID_FILE_ATTRIBUTES) return false; if (ftyp & FILE_ATTRIBUTE_DIRECTORY) return true; #elif __APPLE__ struct stat info; if (stat(path.c_str(), &info) != 0) return false; else if (info.st_mode & S_IFDIR) return true; #endif return false; }
void Update::createDownloadedDir() { pathToSave = FileUtils::getInstance()->getWritablePath(); pathToSave += "tmpdir"; // Create the folder if it doesn't exist #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DIR *pDir = NULL; pDir = opendir (pathToSave.c_str()); if (! pDir) { mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); } #else if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES) { CreateDirectoryA(pathToSave.c_str(), 0); } #endif }
// file system stuff bool exists(const u::string &inputPath, pathType type) { u::string &&path = u::fixPath(inputPath); if (type == kFile) return dir::isFile(path); // type == kDirectory #if defined(_WIN32) const DWORD attribs = GetFileAttributesA(inputPath.c_str()); if (attribs == INVALID_FILE_ATTRIBUTES) return false; if (!(attribs & FILE_ATTRIBUTE_DIRECTORY)) return false; #else struct stat info; if (stat(path.c_str(), &info) != 0) return false; // Couldn't stat directory if (!(info.st_mode & S_IFDIR)) return false; // Not a directory #endif return true; }
bool FileUtils::isDirectoryExistInternal(const std::string& dirPath) const { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) unsigned long fAttrib = GetFileAttributesA(dirPath.c_str()); if (fAttrib != INVALID_FILE_ATTRIBUTES && (fAttrib & FILE_ATTRIBUTE_DIRECTORY)) { return true; } return false; #else struct stat st; if (stat(dirPath.c_str(), &st) == 0) { return S_ISDIR(st.st_mode); } return false; #endif }
luabind::object luabindResolveFile(std::string file, lua_State* L){ std::vector<std::string> paths = { WStr2Str(getCustomFolderPath()), (std::string)GM_FULLDIR, WStr2Str(getModulePath()) + "\\LuaScriptsLib\\", WStr2Str(getModulePath()) + "\\" }; for (std::string nextSearchPath : paths) { std::string nextEntry = nextSearchPath + file; DWORD objectAttributes = GetFileAttributesA(nextEntry.c_str()); if(objectAttributes == INVALID_FILE_ATTRIBUTES) continue; if(objectAttributes & FILTER) return luabind::object(L, nextEntry); } return luabind::object(); }
kbool_t knh_isfile(CTX, const char *phname) { kbool_t res = 1; if(phname[0] == 0) return 0; #if defined(K_USING_WINDOWS_) DWORD attr = GetFileAttributesA(phname); if(attr == -1 || (attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) res = 0; #elif defined(K_USING_POSIX_) struct stat buf; if(stat(phname, &buf) == -1) res = 0; else res = S_ISREG(buf.st_mode); #else FILE* in = fopen(phname,"r"); if(in == NULL) res = 0; else fclose(in); #endif if(res == 0) { DBG_P("isfile='%s' NOTFOUND", phname); } return res; }
S_API bool S_CALL SteamAPI_Init() { if(GetFileAttributesA(".\\steam_api.ini") == INVALID_FILE_ATTRIBUTES) { MessageBoxA(0, "The configuration file \'steam_api.ini\' could not be found in the current directory. " "A new one will be automatically generated once you press OK.", "Warning", MB_OK | MB_ICONWARNING); srand((uint32)time(NULL)); CIniWriter ini(".\\steam_api.ini"); ini.WriteString("Settings", "Nickname", "UnnamedPlayer"); ini.WriteInteger("Settings", "SteamID", rand()); ini.WriteBoolean("Miscellaneous", "EnableDebugConsole", false); } CIniReader ini(".\\steam_api.ini"); g_nickname = ini.ReadString("Settings", "Nickname", "UnnamedPlayer"); g_steamID = ini.ReadInteger("Settings", "SteamID", 0); g_consoleAlloc = ini.ReadBoolean("Miscellaneous", "EnableDebugConsole", false); if(g_consoleAlloc && !g_consoleAllocDone) { if(!AllocConsole()) { MessageBoxA(0, "Could not allocate console.", "Error", MB_OK | MB_ICONERROR); TerminateProcess(GetCurrentProcess(), 0x0000DEAD); return false; } freopen("CON","w",stdout); g_consoleAllocDone = true; } S_LOG(); return true; }
void AssetsUpdateLayer::createDownloadedDir() { m_pathToSave = CCFileUtils::sharedFileUtils()->getWritablePath(); m_pathToSave += "loaddir/"; CCLOG("writable path[%s]", m_pathToSave.c_str()); getAssetsManager()->setStoragePath(m_pathToSave.c_str()); #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DIR *pDir = NULL; pDir = opendir (m_pathToSave.c_str()); if (! pDir) { mkdir(m_pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); } #else if ((GetFileAttributesA(m_pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES) { CreateDirectoryA(m_pathToSave.c_str(), 0); } #endif }
bool CFileHelper::isFileExists(const char* szFilePath) { #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 DWORD dwFileAttr = GetFileAttributesA(szFilePath); if (INVALID_FILE_ATTRIBUTES == dwFileAttr || (dwFileAttr&FILE_ATTRIBUTE_DIRECTORY)) { return false; } #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID bool bfind = true; return bfind; #else struct stat buf; int n = stat(szFilePath, &buf); if ((0 != n) || !(buf.st_mode&S_IFMT)) { return false; } #endif return true; }
static INT_PTR fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv) { HANDLE handle; DWORD dwAccess = 0; DWORD dwShareMode = 0; DWORD dwCreateDisposition = OPEN_EXISTING; dwAccess = GENERIC_READ | GENERIC_WRITE; dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES) dwCreateDisposition = OPEN_EXISTING; else dwCreateDisposition = CREATE_NEW; handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL); ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile); return (INT_PTR)handle; }
DIR *opendir(const char *name) { DWORD attrs = GetFileAttributesA(name); int len; DIR *p; /* check for valid path */ if (attrs == INVALID_FILE_ATTRIBUTES) { errno = ENOENT; return NULL; } /* check if it's a directory */ if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) { errno = ENOTDIR; return NULL; } /* check that the pattern won't be too long for FindFirstFileA */ len = strlen(name); if (is_dir_sep(name[len - 1])) len--; if (len + 2 >= MAX_PATH) { errno = ENAMETOOLONG; return NULL; } p = malloc(sizeof(DIR) + len + 2); if (!p) return NULL; memset(p, 0, sizeof(DIR) + len + 2); strcpy(p->dd_name, name); p->dd_name[len] = '/'; p->dd_name[len+1] = '*'; p->dd_handle = INVALID_HANDLE_VALUE; return p; }
static BOOL create_backup(const char *filename) { HANDLE handle; DWORD rc, attribs; DeleteFileA(filename); handle = OpenEventLogA(NULL, "Application"); rc = BackupEventLogA(handle, filename); if (!rc && GetLastError() == ERROR_PRIVILEGE_NOT_HELD) { skip("insufficient privileges to backup the eventlog\n"); CloseEventLog(handle); return FALSE; } ok(rc, "BackupEventLogA failed, le=%u\n", GetLastError()); CloseEventLog(handle); attribs = GetFileAttributesA(filename); todo_wine ok(attribs != INVALID_FILE_ATTRIBUTES, "Expected a backup file attribs=%#x le=%u\n", attribs, GetLastError()); return TRUE; }