fs_file fs_open(const char *path, const char *mode) { fs_file fh; assert((mode[0] == 'r' && !mode[1]) || (mode[0] == 'w' && (!mode[1] || mode[1] == '+'))); if ((fh = malloc(sizeof (*fh)))) { switch (mode[0]) { case 'r': fh->handle = PHYSFS_openRead(path); break; case 'w': fh->handle = (mode[1] == '+' ? PHYSFS_openAppend(path) : PHYSFS_openWrite(path)); break; } if (fh->handle) { PHYSFS_setBuffer(fh->handle, 0x2000); } else { free(fh); fh = NULL; } } return fh; }
static void *file_phys_fopen(const char *filename, const char *mode) { PHYSFS_file *phys; ALLEGRO_FILE_PHYSFS *fp; /* XXX handle '+' modes */ /* It might be worth adding a function to parse mode strings, to be * shared amongst these kinds of addons. */ if (streq(mode, "r") || streq(mode, "rb")) phys = PHYSFS_openRead(filename); else if (streq(mode, "w") || streq(mode, "wb")) phys = PHYSFS_openWrite(filename); else if (streq(mode, "a") || streq(mode, "ab")) phys = PHYSFS_openAppend(filename); else phys = NULL; if (!phys) { phys_set_errno(NULL); return NULL; } fp = al_malloc(sizeof(*fp)); if (!fp) { al_set_errno(ENOMEM); PHYSFS_close(phys); return NULL; } fp->phys = phys; fp->error_indicator = false; return fp; }
File::File(const MountedFilePath& mountedFilePath, DataStream::OpenMode openMode) : impl(std::make_unique<File_Impl>()), mountedFilePath(mountedFilePath) { const char* path = mountedFilePath.path.c_str(); int fileExists = PHYSFS_exists(path); if (fileExists == 0) { throw Exception(std::string("File ") + path + " does not exist in the search path"); } switch (openMode) { case OpenMode::Read: impl->physfsFileHandle = PHYSFS_openRead(path); break; case OpenMode::Write: impl->physfsFileHandle = PHYSFS_openWrite(path); break; case OpenMode::Append: impl->physfsFileHandle = PHYSFS_openAppend(path); break; } if (impl->physfsFileHandle == nullptr) { throw Exception(std::string("Failed to open file ") + path + "\n" + PHYSFS_getLastError()); } }
Lux::Core::OpenedFile* Lux::Core::FileHandler::OpenFile(const String a_File, FileOpenMode a_OpenMode) { OpenedFile* retFile = nullptr; switch (a_OpenMode) { case Lux::Core::FileHandler::FILE_OPEN_READ: retFile = PHYSFS_openRead(a_File.c_str()); break; case Lux::Core::FileHandler::FILE_OPEN_WRITE: retFile = PHYSFS_openWrite(a_File.c_str()); break; case Lux::Core::FileHandler::FILE_OPEN_APPEND: retFile = PHYSFS_openAppend(a_File.c_str()); break; default: Utility::ThrowError("Unsupported file open mode."); break; } if (retFile == nullptr) { Utility::ThrowError("Could not open specified file: " + a_File + " . " + PHYSFS_getLastError()); } return retFile; }
FileStreamPtr ResourceManager::appendFile(const std::string& fileName) { PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); if(!file) stdext::throw_exception(stdext::format("failed to append file '%s': %s", fileName, PHYSFS_getLastError())); return FileStreamPtr(new FileStream(fileName, file, true)); }
ResourceWO * Resources::RetrieveAppend(PString & filename) { PHYSFS_file* file = PHYSFS_openAppend(filename); if(!file) { PError << "couldn't open file '" << filename << "' for writing(append): %s" << PHYSFS_getLastError() << endl; return NULL; }; return new ResourceWO(file); }
WriteFile* openAppend(const char* filename) { PHYSFS_file* file = PHYSFS_openAppend(filename); if(!file) throw Exception("couldn't open file '%s' for writing(append): %s", filename, PHYSFS_getLastError()); return new WriteFile(file); }
stream_t *file_open(const char *path, stream_mode_t mode, allocator_t *alloc) { PHYSFS_File *file = NULL; stream_t *stream = NULL; char *pathcopy; size_t pathsize; if (!alloc) alloc = g_default_allocator; if (!path) { s_log_error("NULL path for file."); return NULL; } stream = stream_alloc(mode, alloc); if (stream) { switch (mode) { case STREAM_READ: file = PHYSFS_openRead(path); break; case STREAM_WRITE: file = PHYSFS_openWrite(path); break; case STREAM_APPEND: file = PHYSFS_openAppend(path); break; default: /* cannot reach */ break; } // if r+ failed because the file doesn't exist, open again with w if (file == NULL) { s_log_error("Failed to open file '%s' with mode %d. Error: %s.", path, mode, pfs_get_error()); stream_close(stream); return NULL; } stream->read = file_read; stream->write = file_write; stream->seek = file_seek; stream->eof = file_eof; stream->close = file_close; // copy path string pathsize = strlen(path) + 1; pathcopy = com_malloc(alloc, pathsize); strncpy(pathcopy, path, pathsize); stream->context.pfs.file = file; stream->context.pfs.file_path = pathcopy; } return stream; }
PHYSFS_File* openWithMode(char const * filename, mode openMode) { switch (openMode) { case WRITE: return PHYSFS_openWrite(filename); case APPEND: return PHYSFS_openAppend(filename); case READ: return PHYSFS_openRead(filename); } }
PHYSFS_File* vfs::openRaw(std::string const& name, VFile::OpenMode mode) { PHYSFS_File* f; switch (mode) { case VFile::openR: f = PHYSFS_openRead(name.c_str()); break; case VFile::openW: f = PHYSFS_openWrite(name.c_str()); break; case VFile::openA: f = PHYSFS_openAppend(name.c_str()); break; default: throw Error("invalid openmode", false); } if (!f) throw Error("opening file \"" + name + "\" failed"); return f; }
FilePtr File::Open(std::string path, FileMode mode) { PHYSFS_File* file = NULL; switch (mode) { case FileMode::Read: file = PHYSFS_openRead(path.c_str()); break; case FileMode::Write: file = PHYSFS_openWrite(path.c_str()); break; case FileMode::Append: file = PHYSFS_openAppend(path.c_str()); break; } return new PhysFSFile(file, mode); }
bool physfsFile::prepareWrite() { const char *wdir = PHYSFS_getWriteDir(); if (wdir == NULL) { LOG_MSG("PHYSFS could not fulfill write request: no write directory set."); return false; } //LOG_MSG("Goto write (%s at %i)",pname,PHYSFS_tell(fhandle)); const char *fdir = PHYSFS_getRealDir(pname); PHYSFS_uint64 pos = PHYSFS_tell(fhandle); char *slash = strrchr(pname,'/'); if (slash && slash != pname) { *slash = 0; PHYSFS_mkdir(pname); *slash = '/'; } if (strcmp(fdir,wdir)) { /* we need COW */ //LOG_MSG("COW",pname,PHYSFS_tell(fhandle)); PHYSFS_file *whandle = PHYSFS_openWrite(pname); if (whandle == NULL) { LOG_MSG("PHYSFS copy-on-write failed: %s.",PHYSFS_getLastError()); return false; } char buffer[65536]; PHYSFS_sint64 size; PHYSFS_seek(fhandle, 0); while ((size = PHYSFS_read(fhandle,buffer,1,65536)) > 0) { if (PHYSFS_write(whandle,buffer,1,(PHYSFS_uint32)size) != size) { LOG_MSG("PHYSFS copy-on-write failed: %s.",PHYSFS_getLastError()); PHYSFS_close(whandle); return false; } } PHYSFS_seek(whandle, pos); PHYSFS_close(fhandle); fhandle = whandle; } else { // megayuck - physfs on posix platforms uses O_APPEND. We illegally access the fd directly and clear that flag. //LOG_MSG("noCOW",pname,PHYSFS_tell(fhandle)); PHYSFS_close(fhandle); fhandle = PHYSFS_openAppend(pname); #ifndef WIN32 fcntl(**(int**)fhandle->opaque,F_SETFL,0); #endif PHYSFS_seek(fhandle, pos); } return true; }
static int cmd_append(char *args) { PHYSFS_File *f; if (*args == '\"') { args++; args[strlen(args) - 1] = '\0'; } /* if */ f = PHYSFS_openAppend(args); if (f == NULL) printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError()); else { size_t bw; PHYSFS_sint64 rc; if (do_buffer_size) { if (!PHYSFS_setBuffer(f, do_buffer_size)) { printf("failed to set file buffer. Reason: [%s].\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ bw = strlen(WRITESTR); rc = PHYSFS_write(f, WRITESTR, 1, bw); if (rc != (int)bw) { printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", (int) rc, (int) bw, PHYSFS_getLastError()); } /* if */ else { printf("Successful.\n"); } /* else */ PHYSFS_close(f); } /* else */ return(1); } /* cmd_append */
FileDevice::FileDevice(const std::string& path, OpenMode om) { switch (om) { case OM_READ: file = PHYSFS_openRead(path.c_str()); break; case OM_WRITE: file = PHYSFS_openWrite(path.c_str()); break; case OM_APPEND: file = PHYSFS_openAppend(path.c_str()); break; } if (file == 0) throw Exception(PHYSFS_getLastError()); }
bool File::open(Mode mode) { if (mode == CLOSED) return true; // File must exist if read mode. if ((mode == READ) && !PHYSFS_exists(filename.c_str())) throw love::Exception("Could not open file %s. Does not exist.", filename.c_str()); // Check whether the write directory is set. if ((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory()) throw love::Exception("Could not set write directory."); // File already open? if (file != 0) return false; this->mode = mode; switch (mode) { case READ: file = PHYSFS_openRead(filename.c_str()); break; case APPEND: file = PHYSFS_openAppend(filename.c_str()); break; case WRITE: file = PHYSFS_openWrite(filename.c_str()); break; default: break; } if (file != 0 && !setBuffer(bufferMode, bufferSize)) { // Revert to buffer defaults if we don't successfully set the buffer. bufferMode = BUFFER_NONE; bufferSize = 0; } return (file != 0); }
OSFILE *OSBasics::open(String filename, String opts) { OSFILE *retFile = NULL; if(PHYSFS_exists(filename.c_str())) { if(!PHYSFS_isDirectory(filename.c_str())) { retFile = new OSFILE; retFile->fileType = OSFILE::TYPE_ARCHIVE_FILE; if(opts.find("a") !=string::npos) { retFile->physFSFile = PHYSFS_openAppend(filename.c_str()); if(!retFile->physFSFile){ printf("Error opening file from archive (%s)\n", filename.c_str()); return NULL; } } else if(opts.find("w") !=string::npos) { retFile->physFSFile = PHYSFS_openWrite(filename.c_str()); if(!retFile->physFSFile){ printf("Error opening file from archive (%s)\n", filename.c_str()); return NULL; } } else { retFile->physFSFile = PHYSFS_openRead(filename.c_str()); if(!retFile->physFSFile){ printf("Error opening file from archive (%s)\n", filename.c_str()); return NULL; } } return retFile; } } else { // Logger::log("File doesn't exist in archive (%s)\n", filename.c_str()); } FILE *file = fopen(filename.c_str(), opts.c_str()); if(file) { retFile = new OSFILE; retFile->fileType = OSFILE::TYPE_FILE; retFile->file = file; return retFile; } return NULL; }
File::File(const Path & file, EFileMode mode) { m_mode = mode; auto filePath = MakePosix(file).generic_string(); auto file_cstr = filePath.c_str(); if (mode & EFM_READ) { if (mode & EFM_APPEND) { m_fileHandle = PHYSFS_openAppend(file_cstr); if (m_fileHandle == nullptr) { printf("File open append failed: %s %s\n", PHYSFS_getLastError(), file_cstr); } } else { m_fileHandle = PHYSFS_openRead(file_cstr); if (m_fileHandle == nullptr) { printf("File open read failed: %s %s\n", PHYSFS_getLastError(), file_cstr); } } } else if (mode & EFM_WRITE) { m_fileHandle = PHYSFS_openWrite(file_cstr); if (m_fileHandle == nullptr) { printf("File open write failed: %s %s\n", PHYSFS_getLastError(), file_cstr); } } else m_fileHandle = nullptr; }
bool File::open() { // Check whether the write directory is set. if((mode == love::FILE_APPEND || mode == love::FILE_WRITE) && (PHYSFS_getWriteDir() == 0)) if(!setupWriteDirectory()) return false; switch(mode) { case love::FILE_READ: file = PHYSFS_openRead(filename.c_str()); break; case love::FILE_APPEND: file = PHYSFS_openAppend(filename.c_str()); break; case love::FILE_WRITE: file = PHYSFS_openWrite(filename.c_str()); break; } return (file != 0); }
void PhysFSLogFile::Open(const std::string& filename) { /*if (PHYSFS_exists(filename.c_str())) m_Filename = filename.substr(0, filename.find_last_of('.')) + "-" + */ m_File = PHYSFS_openAppend(filename.c_str()); if (m_File != NULL) { m_StartingLength = PHYSFS_tell(m_File); if (m_StartingLength > m_MaxLength) { PHYSFS_close(m_File); m_File = NULL; m_File = PHYSFS_openWrite(filename.c_str()); m_StartingLength = 0; } } m_Open = m_File == NULL ? false : true; m_CurrentLength = m_StartingLength; m_SessionLength = 0; m_Filename = filename; }
bool File::open(Mode mode) { if(mode == CLOSED) return true; // File must exist if read mode. if((mode == READ) && !PHYSFS_exists(filename.c_str())) throw love::Exception("Could not open file %s. Does not exist.", filename.c_str()); // Check whether the write directory is set. if((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory()) throw love::Exception("Could not set write directory."); // File already open? if(file != 0) return false; this->mode = mode; switch(mode) { case READ: file = PHYSFS_openRead(filename.c_str()); break; case APPEND: file = PHYSFS_openAppend(filename.c_str()); break; case WRITE: file = PHYSFS_openWrite(filename.c_str()); break; default: break; } return (file != 0); }
PHYSFS_file *openAppend(const char *const filename) { return PHYSFS_openAppend(filename); }
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname) { return create_rwops(PHYSFS_openAppend(fname)); } /* PHYSFSRWOPS_openAppend */
ex_file_t *ex_fsys_fopen_a ( const char *_filename ) { return PHYSFS_openAppend(_filename); }