bool WriteFile( const std::string &path, const Buffer &buf ) { s32 fd; int i; u64 written; i = sysFsOpen( path.c_str(), SYS_O_WRONLY | SYS_O_CREAT | SYS_O_TRUNC, &fd, NULL, 0 ); if( i ) { printf("sysFsOpen( %s ): %i\n", path.c_str(), i ); return false; } i = sysFsWrite( fd, (void*)buf.ConstData(), buf.Size(), &written ); if( i ) { sysFsClose( fd ); sysFsUnlink( path.c_str() ); printf("sysFsWrite( %s ): %i\n", path.c_str(), i ); return false; } sysFsClose( fd ); if( written != buf.Size() ) { printf("WriteFile() failed to write all the data to \"%s\"\n", path.c_str() ); sysFsUnlink( path.c_str() ); return false; } return true; }
bool WriteFile( const std::string &path, const u8* stuff, u32 size ) { //hexdump( stuff, size ); s32 fd; int i; u64 written; i = sysFsOpen( path.c_str(), SYS_O_WRONLY | SYS_O_CREAT | SYS_O_TRUNC, &fd, NULL, 0 ); if( i ) { printf("sysFsOpen( %s ): %i\n", path.c_str(), i ); return false; } i = sysFsWrite( fd, (void*)stuff, size, &written ); if( i ) { sysFsClose( fd ); sysFsUnlink( path.c_str() ); printf("sysFsWrite( %s ): %i\n", path.c_str(), i ); return false; } sysFsClose( fd ); if( written != size ) { printf("WriteFile() failed to write all the data to \"%s\"\n", path.c_str() ); sysFsUnlink( path.c_str() ); return false; } return true; }
string recursiveDelete(string direct) { string dfile; DIR *dp; struct dirent *dirp; dp = opendir (direct.c_str()); if (dp != NULL) { while ((dirp = readdir (dp))) { dfile = direct + "/" + dirp->d_name; if ( strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0 && strcmp(dirp->d_name, "") != 0) { //Mess.Dialog(MSG_OK,("Testing: "+dfile).c_str()); if (dirp->d_type == DT_DIR) { //Mess.Dialog(MSG_OK,("Is directory: "+dfile).c_str()); recursiveDelete(dfile); } else { //Mess.Dialog(MSG_OK,("Deleting file: "+dfile).c_str()); if ( sysFsUnlink(dfile.c_str()) != 0) return "Couldn't delete file "+dfile+"\n"+strerror(errno); } } } (void) closedir (dp); } else return "Couldn't open the directory"; //Mess.Dialog(MSG_OK,("Deleting folder: "+direct).c_str()); if ( rmdir(direct.c_str()) != 0) return "Couldn't delete directory "+direct+"\n"+strerror(errno); return ""; }
bool RecurseDeletePath( std::string path, u8 depth ) { bool ret = true; int i; sysFSStat stat; if( sysFsStat( path.c_str(), &stat ) ) return false; //its a directory if( stat.st_mode & S_IFDIR ) { if( path.at( path.size() - 1 ) != '/' ) path += "/"; s32 fd; sysFSDirent entry; u64 read; //open dir i = sysFsOpendir( path.c_str(), &fd ); if( i ) { printf("sysFsOpendir( %s ): %i\n", path.c_str(), i ); return false; } while( !sysFsReaddir( fd, &entry , &read ) && read > 0 ) { if( !entry.d_namlen || !strcmp( entry.d_name, "." ) || !strcmp( entry.d_name, ".." ) ) { continue; } std::string subPath = path + entry.d_name; if( sysFsStat( subPath.c_str(), &stat ) ) continue; if( stat.st_mode & S_IFDIR ) { if( depth < MAX_RECURSE_DEPTH ) { if( !RecurseDeletePath( subPath, depth ) ) { ret = false; break; } } else { printf("RecurseDeletePath( \"%s\" ): max recursion depth reached\n", subPath.c_str() ); ret = false; break; } } else { i = sysFsUnlink( subPath.c_str() ); if( i ) { printf("sysFsUnlink( %s ): %i\n", subPath.c_str(), i ); ret = false; break; } } } sysFsClosedir( fd ); if( ret ) { i = sysFsRmdir( path.c_str() ); if( i ) { printf("sysFsRmdir( %s ): %08x\n", path.c_str(), i ); ret = false; } } } else//its a file { i = sysFsUnlink( path.c_str() ); if( i ) { printf("sysFsUnlink( %s ): %i\n", path.c_str(), i ); ret = false; } } return ret; }