void RemoveDir(const char* oldPath) { char *fullOldPath; SceIoDirent oneDir; int oDir = sceIoDopen(oldPath); if (oDir < 0){ return; } while (1){ memset(&oneDir, 0, sizeof(SceIoDirent)); if (sceIoDread(oDir, &oneDir) <= 0) break; if (!strcmp(oneDir.d_name, ".") || !strcmp(oneDir.d_name, "..")) continue; if (oldPath[strlen(oldPath)-1] != '/'){ fullOldPath = (char *)calloc(strlen(oldPath)+strlen(oneDir.d_name)+2,sizeof(char)); sprintf(fullOldPath,"%s/%s",oldPath,oneDir.d_name); } else { fullOldPath = (char *)calloc(strlen(oldPath)+strlen(oneDir.d_name)+1,sizeof(char)); sprintf(fullOldPath,"%s%s",oldPath,oneDir.d_name); } if (FIO_S_ISDIR(oneDir.d_stat.st_mode)){ RemoveDir(fullOldPath); } else if(FIO_S_ISREG(oneDir.d_stat.st_mode)){ sceIoRemove(fullOldPath); } free(fullOldPath); } sceIoDclose(oDir); sceIoRmdir(oldPath); }
extern dword copy_dir(const char *src, const char *dest, t_copy_cb cb, t_copy_overwritecb ocb, void *data) { int dl = xrIoDopen(src); dword result = 0; SceIoDirent sid; if (dl < 0) { if (cb != NULL) cb(src, dest, false, data); return 0; } xrIoMkdir(dest, 0777); memset(&sid, 0, sizeof(SceIoDirent)); while (xrIoDread(dl, &sid)) { char copysrc[260], copydest[260]; if (sid.d_name[0] == '.') continue; SPRINTF_S(copysrc, "%s/%s", src, sid.d_name); SPRINTF_S(copydest, "%s/%s", dest, sid.d_name); if (FIO_S_ISDIR(sid.d_stat.st_mode)) { result += copy_dir(copysrc, copydest, cb, ocb, data); continue; } if (copy_file(copysrc, copydest, cb, ocb, data)) ++result; memset(&sid, 0, sizeof(SceIoDirent)); } xrIoDclose(dl); return result; }
int has_directories(const char *base, const char *path) { SceIoDirent dir; char buffer[256]; int ret = 0; SceUID fd; sce_paf_private_snprintf(buffer, 256, "%s/%s", base, path); kprintf("checking %s\n", buffer); fd = sceIoDopen(buffer); if(fd >= 0) { sce_paf_private_memset(&dir, 0, sizeof(SceIoDirent)); while(sceIoDread(fd, &dir) > 0) { if (FIO_S_ISDIR(dir.d_stat.st_mode) && dir.d_name[0] != '.') { ret = 1; break; } } sceIoDclose(fd); } else { ret = -1; } return ret; }
extern u32 utils_del_dir(const char *dir) { u32 count = 0; int dl = sceIoDopen(dir); SceIoDirent sid; if (dl < 0) return count; memset(&sid, 0, sizeof(SceIoDirent)); while (sceIoDread(dl, &sid)) { char compPath[260]; if (sid.d_name[0] == '.') continue; SPRINTF_S(compPath, "%s/%s", dir, sid.d_name); if (FIO_S_ISDIR(sid.d_stat.st_mode)) { if (utils_del_dir(compPath)) { count++; } continue; } if (utils_del_file(compPath)) { count++; } memset(&sid, 0, sizeof(SceIoDirent)); } sceIoDclose(dl); sceIoRmdir(dir); return count; }
static bool path_stat(const char *path, enum stat_mode mode) { #if defined(VITA) || defined(PSP) SceIoStat buf; char *tmp = strdup(path); size_t len = strlen(tmp); if (tmp[len-1] == '/') tmp[len-1]='\0'; if (sceIoGetstat(tmp, &buf) < 0) { free(tmp); return false; } free(tmp); #elif defined(__CELLOS_LV2__) CellFsStat buf; if (cellFsStat(path, &buf) < 0) return false; #elif defined(_WIN32) DWORD ret = GetFileAttributes(path); if (ret == INVALID_FILE_ATTRIBUTES) return false; #else struct stat buf; if (stat(path, &buf) < 0) return false; #endif switch (mode) { case IS_DIRECTORY: #if defined(VITA) || defined(PSP) return FIO_S_ISDIR(buf.st_mode); #elif defined(__CELLOS_LV2__) return ((buf.st_mode & S_IFMT) == S_IFDIR); #elif defined(_WIN32) return (ret & FILE_ATTRIBUTE_DIRECTORY); #else return S_ISDIR(buf.st_mode); #endif case IS_CHARACTER_SPECIAL: #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) || defined(_WIN32) return false; #else return S_ISCHR(buf.st_mode); #endif case IS_VALID: return true; } return false; }
void IndexCategories(Category *head[], const char *path, int location) { SceIoDirent dir; SceUID fd; u64 mtime; char full_path[16]; int match; sce_paf_private_strcpy(full_path, path); SET_DEVICENAME(full_path, location); if((fd = sceIoDopen(full_path)) < 0) { return; } kprintf("Indexing categories from %s, loc: %i\n", path, location); match = 0; sce_paf_private_memset(&dir, 0, sizeof(SceIoDirent)); while(1) { if(sceIoDread(fd, &dir) <= 0) { kprintf("End of directory list\n"); sceIoDclose(fd); break; } kprintf("checking [%s], length: %i\n", dir.d_name, sce_paf_private_strlen(dir.d_name)); if (FIO_S_ISDIR(dir.d_stat.st_mode) && dir.d_name[0] != '.') { if(!config.prefix && !is_game_folder(full_path, dir.d_name)) { if(has_directories(full_path, dir.d_name) > 0) { match = 1; } } else if(config.prefix && sce_paf_private_strncmp(dir.d_name, "CAT_", 4) == 0) { if(has_directories(full_path, dir.d_name) > 0) { sce_paf_private_strcpy(dir.d_name, dir.d_name + 4); match = 1; } } if(match) { match = 0; sceRtcGetTick((pspTime *) &dir.d_stat.st_mtime, &mtime); kprintf("Adding category: [%s]\n", dir.d_name); AddCategory(head, dir.d_name, mtime, location); } } } }
static int lua_dir(lua_State *L) { int argc = lua_gettop(L); if (argc != 0 && argc != 1) return luaL_error(L, "System.listDirectory([path]) takes zero or one argument"); const char *path = ""; if (argc == 0) { path = ""; } else { path = luaL_checkstring(L, 1); } int fd = sceIoDopen(path); if (fd < 0) { lua_pushnil(L); /* return nil */ return 1; } lua_newtable(L); int i = 1; while (sceIoDread(fd, &g_dir) > 0) { lua_pushnumber(L, i++); /* push key for file entry */ lua_newtable(L); lua_pushstring(L, "name"); lua_pushstring(L, g_dir.d_name); lua_settable(L, -3); lua_pushstring(L, "size"); lua_pushnumber(L, g_dir.d_stat.st_size); lua_settable(L, -3); lua_pushstring(L, "directory"); lua_pushboolean(L, FIO_S_ISDIR(g_dir.d_stat.st_mode)); lua_settable(L, -3); lua_settable(L, -3); } sceIoDclose(fd); return 1; /* table is already on top */ }
int modex2mode(int modex) { int mode = 0; if (FIO_S_ISLNK(modex)) mode |= FIO_SO_IFLNK; if (FIO_S_ISREG(modex)) mode |= FIO_SO_IFREG; if (FIO_S_ISDIR(modex)) mode |= FIO_SO_IFDIR; /* Convert the file access modes. */ if (modex & (FIO_S_IRUSR | FIO_S_IRGRP | FIO_S_IROTH)) mode |= FIO_SO_IROTH; if (modex & (FIO_S_IWUSR | FIO_S_IWGRP | FIO_S_IWOTH)) mode |= FIO_SO_IWOTH; if (modex & (FIO_S_IXUSR | FIO_S_IXGRP | FIO_S_IXOTH)) mode |= FIO_SO_IXOTH; return mode; }
static bool path_stat(const char *path, enum stat_mode mode, int32_t *size) { #if defined(VITA) || defined(PSP) SceIoStat buf; char *tmp = strdup(path); size_t len = strlen(tmp); if (tmp[len-1] == '/') tmp[len-1]='\0'; if (sceIoGetstat(tmp, &buf) < 0) { free(tmp); return false; } free(tmp); #elif defined(__CELLOS_LV2__) CellFsStat buf; if (cellFsStat(path, &buf) < 0) return false; #elif defined(_WIN32) struct _stat buf; char *path_local; wchar_t *path_wide; DWORD file_info; if (!path || !*path) return false; (void)path_wide; (void)path_local; (void)file_info; #if defined(LEGACY_WIN32) path_local = utf8_to_local_string_alloc(path); file_info = GetFileAttributes(path_local); _stat(path_local, &buf); if (path_local) free(path_local); #else path_wide = utf8_to_utf16_string_alloc(path); file_info = GetFileAttributesW(path_wide); _wstat(path_wide, &buf); if (path_wide) free(path_wide); #endif if (file_info == INVALID_FILE_ATTRIBUTES) return false; #else struct stat buf; if (stat(path, &buf) < 0) return false; #endif if (size) *size = (int32_t)buf.st_size; switch (mode) { case IS_DIRECTORY: #if defined(VITA) || defined(PSP) return FIO_S_ISDIR(buf.st_mode); #elif defined(__CELLOS_LV2__) return ((buf.st_mode & S_IFMT) == S_IFDIR); #elif defined(_WIN32) return (file_info & FILE_ATTRIBUTE_DIRECTORY); #else return S_ISDIR(buf.st_mode); #endif case IS_CHARACTER_SPECIAL: #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) || defined(_WIN32) return false; #else return S_ISCHR(buf.st_mode); #endif case IS_VALID: return true; } return false; }
static bool path_stat(const char *path, enum stat_mode mode, int32_t *size) { #if defined(VITA) || defined(PSP) SceIoStat buf; char *tmp = strdup(path); size_t len = strlen(tmp); if (tmp[len-1] == '/') tmp[len-1]='\0'; if (sceIoGetstat(tmp, &buf) < 0) { free(tmp); return false; } free(tmp); #elif defined(__CELLOS_LV2__) CellFsStat buf; if (cellFsStat(path, &buf) < 0) return false; #elif defined(_WIN32) WIN32_FILE_ATTRIBUTE_DATA file_info; GET_FILEEX_INFO_LEVELS fInfoLevelId = GetFileExInfoStandard; DWORD ret = GetFileAttributesEx(path, fInfoLevelId, &file_info); if (ret == 0) return false; #else struct stat buf; if (stat(path, &buf) < 0) return false; #endif #if defined(_WIN32) if (size) *size = file_info.nFileSizeLow; #else if (size) *size = buf.st_size; #endif switch (mode) { case IS_DIRECTORY: #if defined(VITA) || defined(PSP) return FIO_S_ISDIR(buf.st_mode); #elif defined(__CELLOS_LV2__) return ((buf.st_mode & S_IFMT) == S_IFDIR); #elif defined(_WIN32) return (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); #else return S_ISDIR(buf.st_mode); #endif case IS_CHARACTER_SPECIAL: #if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__) || defined(_WIN32) return false; #else return S_ISCHR(buf.st_mode); #endif case IS_VALID: return true; } return false; }
static err_t EnumDir(filestream* p,const tchar_t* Exts,bool_t ExtFilter,streamdir* Item) { iox_dirent_t Dirent; Item->FileName[0] = 0; Item->DisplayName[0] = 0; if (p->DevNo>=0) { static const tchar_t* const Devices[] = { "mass:", //"cdrom:", //driver doesn't support directory listing "hdd:", //"host:", //driver doesn't support directory listing "cdda:", "cdfs:", "smb:", NULL, }; for (;!Item->FileName[0];++p->DevNo) { int fd; const tchar_t* URL = Devices[p->DevNo]; if (!URL) break; fd = fileXioDopen(URL); if (fd>=0) { fileXioDclose(fd); tcscpy_s(Item->FileName,TSIZEOF(Item->FileName), URL); Item->ModifiedDate = INVALID_DATETIME_T; Item->Type = FTYPE_DIR; Item->Size = INVALID_FILEPOS_T; } } } else { if (p->FindDir<0) return ERR_END_OF_FILE; while (!Item->FileName[0]) { if (fileXioDread(p->FindDir,&Dirent)<=0) break; if (Dirent.name[0]=='.') // skip unix/mac hidden files continue; tcscpy_s(Item->FileName,TSIZEOF(Item->FileName), Dirent.name); Item->ModifiedDate = PS2ToDateTime(Dirent.stat.mtime); if (FIO_S_ISDIR(Dirent.stat.mode)) { Item->Type = FTYPE_DIR; Item->Size = INVALID_FILEPOS_T; } else { Item->Size = Dirent.stat.size; Item->Type = CheckExts(Item->FileName,Exts); if (!Item->Type && ExtFilter) Item->FileName[0] = 0; // skip } } } if (!Item->FileName[0]) { if (p->FindDir>=0) { fileXioDclose(p->FindDir); p->FindDir = -1; } return ERR_END_OF_FILE; } return ERR_NONE; }
int DirGetContents( const char *path, const char *filter, fileInfo_t *fileInfo, int maxItems ) { int c, i; char *ptr; int numRead; int index = 0; if( !path || !fileInfo || !maxItems ) return -1; if( (ptr = strchr( path, '/' )) == NULL ) { printf("DirGetContents : Invalid Path (ptr = NULL)\n"); return -1; } c = ptr - path; // try to read in dir from cd/dvd if( !strncmp( path, "cdfs:/", c ) ) { // just make sure we are initialized CD_Init(); struct TocEntry *tocEntries = (struct TocEntry*) malloc( sizeof(struct TocEntry) * maxItems ); if( !tocEntries ) return -1; CDVD_FlushCache(); numRead = CDVD_GetDir( ptr, NULL, CDVD_GET_FILES_AND_DIRS, tocEntries, maxItems, NULL ); CDVD_Stop(); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); for( i = 0; i < numRead; i++ ) { if( index >= maxItems ) break; if( !strcmp( tocEntries[i].filename, ".." ) || !strcmp( tocEntries[i].filename, "." ) ) continue; // check for filters c = 1; if( filter && !(tocEntries[i].fileProperties & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( tocEntries[i].filename, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, tocEntries[i].filename, sizeof(fileInfo[index].name) ); fileInfo[index].size = tocEntries[i].fileSize; fileInfo[index].flags = tocEntries[i].fileProperties; index++; } } if( ptr ) free(ptr); free(tocEntries); } else if( !strncmp( path, "pfs", 3 ) ) { // try to read in dir from hdd int hDir = fileXioDopen( path ); int nRet; iox_dirent_t dirEntry; if( hDir < 0 ) return -1; index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); do { if( !(nRet = fileXioDread( hDir, &dirEntry )) ) break; if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, "..")) continue; if( index >= maxItems ) break; if( FIO_S_ISDIR(dirEntry.stat.mode) ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry.name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) ); fileInfo[index].size = dirEntry.stat.size; index++; } } while( nRet > 0 ); if( ptr ) free(ptr); fileXioDclose( hDir ); } else if( !strncmp( path, "mc0:/", c ) || !strncmp( path, "mc1:/", c ) ) { // try to read in dir from memory card int nPort; char mcPath[256]; mcTable mcEntries[MAX_DIR_FILES] __attribute__((aligned(64))); if( !strncmp( path, "mc0:/", c ) ) nPort = 0; else nPort = 1; strcpy( mcPath, ptr ); strcat( mcPath, "*" ); mcGetDir( nPort, 0, mcPath, 0, MAX_DIR_FILES, mcEntries ); mcSync( 0, NULL, &numRead ); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); for( i = 0; i < numRead; i++ ) { if( index >= maxItems ) break; if( !strcmp( mcEntries[i].name, "." ) || !strcmp( mcEntries[i].name, "..") ) continue; if( mcEntries[i].attrFile & MC_ATTR_SUBDIR ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(mcEntries[i].attrFile & MC_ATTR_SUBDIR) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( mcEntries[i].name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, mcEntries[i].name, sizeof(fileInfo[index].name) ); fileInfo[index].size = mcEntries[i].fileSizeByte; index++; } } if( ptr ) free(ptr); } else if( !strncmp( path, "mass:/", c ) ) { // try to read in dir from USB device int nRet; fat_dir_record dirEntry; // returns number of entries in directory nRet = usb_mass_getFirstDirentry( ptr, &dirEntry ); index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); // loop through all entries in directory while( nRet > 0 ) { if(!strcmp( dirEntry.name, "." ) || !strcmp( dirEntry.name, "..")) { nRet = usb_mass_getNextDirentry(&dirEntry); continue; } // ignore volume label if( dirEntry.attr & USB_VOLUME ) { nRet = usb_mass_getNextDirentry(&dirEntry); continue; } if( index >= maxItems ) break; if( dirEntry.attr & USB_DIRECTORY ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry.name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry.name, sizeof(fileInfo[index].name) ); fileInfo[index].size = dirEntry.size; index++; } nRet = usb_mass_getNextDirentry( &dirEntry ); } if( ptr ) free(ptr); } else if( !strncmp( path, "smb", 3 ) ) { // read from a samba share int hDir = smbc_opendir( path ); const struct smbc_dirent *dirEntry; if( hDir < 0 ) return -1; index = 0; ptr = NULL; if( filter ) ptr = (char*) malloc( strlen(filter) + 1 ); while( (dirEntry = smbc_readdir( hDir )) != NULL ) { if(!strcmp( dirEntry->name, "." ) || !strcmp( dirEntry->name, "..")) continue; if( index >= maxItems ) break; if( dirEntry->smbc_type == SMBC_DIR ) fileInfo[index].flags = FLAG_DIRECTORY; else fileInfo[index].flags = 0; // check for filters c = 1; if( filter && !(fileInfo[index].flags & FLAG_DIRECTORY) ) { strcpy( ptr, filter ); c = 0; char *token = strtok( ptr, " " ); while( token ) { // found matching extension if( CmpFileExtension( dirEntry->name, token ) ) { c = 1; break; } token = strtok( NULL, " " ); } } if( c == 1 ) { strncpy( fileInfo[index].name, dirEntry->name, sizeof(fileInfo[index].name) ); fileInfo[index].size = 0; // fixme index++; } }; if( ptr ) free(ptr); smbc_closedir( hDir ); } return index; }
/**************************************************************************** * This function will read the contents of a directory and store the * * contents in the folder structure. The ext parameter works as a filter * * to read in only certain file extension types. After reading the * * directory, the function will sort the contents. * ****************************************************************************/ int readDirectory(char *ext, int media) { int ret = 1; iox_dirent_t directory; int size; char folderName[255]; unsigned int numToc, index; char *extcmp; folder.fIndex = 0; struct TocEntry cdDirectory[255]; size = strlen(ext); switch (media) { case 0: { while (ret > 0) { ret = fileXioDread(folder.iDir, &directory); if (ret > 0) { if (FIO_S_ISDIR(directory.stat.mode)) //is a directory { strcpy(folder.object[folder.fIndex].name, ""); strcat(folder.object[folder.fIndex].name, "/"); strcat(folder.object[folder.fIndex].name, directory.name); folder.object[folder.fIndex].type = 0; folder.object[folder.fIndex].count = folder.fIndex; folder.fIndex++; } else if (FIO_S_ISREG(directory.stat.mode)) //is a file { if (size > ret) size = 0; extcmp = &directory.name[ret-size]; if (strcasecmp(extcmp, ext) == 0) { strcpy(folder.object[folder.fIndex].name, directory.name); folder.object[folder.fIndex].type = 1; folder.object[folder.fIndex].count = folder.fIndex; folder.fIndex++; } } } } folder.fMax = folder.fIndex; folder.fIndex = 0; sortFolder(); for (ret = 0; ret < folder.fMax; ret++) { if (folder.object[ret].type == 0) { strcpy(folderName, &folder.object[ret].name[1]); strcpy(folder.object[ret].name, folderName); } } closeDirectory(MODE_HDD); return folder.fMax; } case 1: { numToc = CDVD_GetDir(&folder.directory[5], NULL, CDVD_GET_FILES_AND_DIRS, cdDirectory, 254, NULL); CDVD_Stop(); #define CD_S_ISDIR(x) x & 2 #define CD_S_ISFILE(x) !CD_S_ISDIR(x) for (index=0; index<numToc; index++) { if (CD_S_ISDIR(cdDirectory[index].fileProperties)) //is a folder { strcpy(folder.object[folder.fIndex].name, ""); strcat(folder.object[folder.fIndex].name, "/"); strcat(folder.object[folder.fIndex].name, cdDirectory[index].filename); folder.object[folder.fIndex].type = 0; folder.object[folder.fIndex].count = folder.fIndex; folder.fIndex++; } else { ret = strlen(cdDirectory[index].filename); if (size > ret) size = 0; extcmp = &cdDirectory[index].filename[ret-size]; if (strcasecmp(extcmp, ext) == 0) { strcpy(folder.object[folder.fIndex].name, cdDirectory[index].filename); folder.object[folder.fIndex].type = 1; folder.object[folder.fIndex].count = folder.fIndex; folder.fIndex++; } } } folder.fMax = folder.fIndex; folder.fIndex = 0; sortFolder(); for (ret = 0; ret < folder.fMax; ret++) { if (folder.object[ret].type == 0) { strcpy(folderName, &folder.object[ret].name[1]); strcpy(folder.object[ret].name, folderName); } } return folder.fMax; } } return 0; }
int FileManager::IsNextDir() { return FIO_S_ISDIR(dirent.d_stat.st_mode); }