bool _CopyFile(const unicode_t *srcFileName, const unicode_t *desFileName) { // if ( strcmp(srcFileName, desFileName) == 0 ) return false; FILE *srcFile = FSOpenFile(srcFileName); if ( srcFile == NULL ) return false; FILE *desFile = FSSCreateFile(desFileName); if ( desFile == NULL ) { fclose(srcFile); return false; } char buff[8092]; long readSize = 0; while ( 1 ) { readSize = fread(buff, 1, 8092, srcFile); if ( readSize <= 0 ) break; fwrite(buff, 1, readSize, desFile); } fclose(srcFile); fclose(desFile); return true; }
FILE *FSOpenFile(const char *fileName) { #if defined(WIN32) || defined(_WIN32_WCE) if ( fileName == NULL ) return NULL; if ( *fileName == 0 ) return NULL; return fopen(fileName, "rb+"); #elif defined(__APPLE_CPP__) || defined(__APPLE_CC__) UniString uniString(fileName); return FSOpenFile(uniString); #else UniString uniString(fileName); return FSOpenFile(uniString); #endif }
FILE *FSOpenFile(const char *fileName, int mode) { #if defined(WIN32) || defined(_WIN32_WCE) if ( fileName == NULL ) return NULL; if ( *fileName == 0 ) return NULL; char modeStr[4]; int pos = 0; if ( mode & FS_NEW_CREATE ) modeStr[pos] = 'w'; else if ( mode & FS_APPEND ) modeStr[pos] = 'a'; else modeStr[pos] = 'r'; pos++; if ( mode & FS_BINARY ) { modeStr[pos] = 'b'; pos++; } else if ( mode & FS_TEXT ) { modeStr[pos] = 't'; pos++; } if ( mode & FS_READWRITE ) { modeStr[pos] = '+'; pos++; } modeStr[pos] = 0; return fopen(fileName, modeStr); #elif defined(__APPLE_CPP__) || defined(__APPLE_CC__) UniString uniString(fileName); return FSOpenFile(uniString); #else UniString uniString(fileName); return FSOpenFile(uniString); #endif }
FSStatus FSOpenFileAsync(FSClient *client, FSCmdBlock *block, const char *path, const char *mode, be_val<FSFileHandle> *outHandle, uint32_t flags, FSAsyncData *asyncData) { assert(asyncData->callback); auto result = FSOpenFile(client, block, path, mode, outHandle, flags); FSAsyncCallback cb = static_cast<uint32_t>(asyncData->callback); cb(client, block, result, asyncData->param); return result; }
FSStatus SAVEOpenFile(FSClient *client, FSCmdBlock *block, uint8_t account, const char *path, const char *mode, be_val<FSFileHandle> *handle, uint32_t flags) { auto fsPath = internal::getSavePath(account, path); return FSOpenFile(client, block, fsPath.path().c_str(), mode, handle, flags); }
void DoExtFileHashing_Name(tBuffer *bIn, tBuffer *bOut, u_int32_t id) { char *file = BufferGetString(bIn); int status, fd; status = FSOpenFile(file, &fd, O_RDONLY, 0, NULL); DEBUG((MYLOG_DEBUG, "[DoExtFileHashing_Name]File: %s Status: %i", file, status)); if (status == SSH2_FX_OK) { DoExtFileHashing_FD(bIn, bOut, id, fd); xclose(fd); } else SendStatus(bOut, id, status); free(file); }
static int FSDrvOpen(struct _reent* r, void* fileStruct, const char* path, int flags, int mode) { FSFileHandle* handle = (FSFileHandle*) fileStruct; char modeString[3] = {'\0'}; switch(flags) { case O_RDONLY: snprintf(modeString, sizeof(modeString), "r"); break; case O_WRONLY | O_CREAT | O_TRUNC: snprintf(modeString, sizeof(modeString), "w"); break; case O_WRONLY | O_CREAT | O_APPEND: snprintf(modeString, sizeof(modeString), "a"); break; case O_RDWR: snprintf(modeString, sizeof(modeString), "r+"); break; case O_RDWR | O_CREAT | O_TRUNC: snprintf(modeString, sizeof(modeString), "w+"); break; case O_RDWR | O_CREAT | O_APPEND: snprintf(modeString, sizeof(modeString), "a+"); break; default: r->_errno = EINVAL; return -1; } FSStatus err = FSOpenFile(fsClient, fsCmdBlock, path, modeString, handle, FS_RET_ALL_ERROR); if(err != FS_STATUS_OK) { FSDrvReportError(r); return -1; } return 0; }
void _start() { /* Load a good stack */ asm( "lis %r1, 0x1ab5 ;" "ori %r1, %r1, 0xd138 ;" ); /* Get a handle to coreinit.rpl */ uint32_t coreinit_h; OSDynLoad_Acquire("coreinit.rpl", &coreinit_h); /* Memory allocation and FS functions */ void* (*OSAllocFromSystem)(uint32_t size, int align); int (*FSInit)(); int (*FSAddClient)(void *client, int unk1); int (*FSInitCmdBlock)(void *cmd); int (*FSOpenDir)(void *client, void *cmd, char *path, uint32_t *dir_handle, int unk1); int (*FSReadDir)(void *client, void *cmd, uint32_t dir_handle, void *buffer, int unk1); int (*FSGetMountSource)(void *client, void *cmd, int type, mount_source *source, int unk1); int (*FSMount)(void *client, void *cmd, mount_source *source, char *mountpath, uint32_t pathlength, int unk1); int (*FSOpenFile)(void *client, void *cmd, char *filepath, char *amode, uint32_t *file_handle, int unk1); int (*FSReadFile)(void *client, void *cmd, void *buffer, uint32_t size, uint32_t length, uint32_t file_handle, int unk1, int unk2); OSDynLoad_FindExport(coreinit_h, 0, "OSAllocFromSystem", &OSAllocFromSystem); OSDynLoad_FindExport(coreinit_h, 0, "FSInit", &FSInit); OSDynLoad_FindExport(coreinit_h, 0, "FSAddClient", &FSAddClient); OSDynLoad_FindExport(coreinit_h, 0, "FSInitCmdBlock", &FSInitCmdBlock); OSDynLoad_FindExport(coreinit_h, 0, "FSOpenDir", &FSOpenDir); OSDynLoad_FindExport(coreinit_h, 0, "FSReadDir", &FSReadDir); OSDynLoad_FindExport(coreinit_h, 0, "FSGetMountSource", &FSGetMountSource); OSDynLoad_FindExport(coreinit_h, 0, "FSMount", &FSMount); OSDynLoad_FindExport(coreinit_h, 0, "FSOpenFile", &FSOpenFile); OSDynLoad_FindExport(coreinit_h, 0, "FSReadFile", &FSReadFile); FSInit(); /* Set up the client and command blocks */ void *client = OSAllocFromSystem(0x1700, 0x20); void *cmd = OSAllocFromSystem(0xA80, 0x20); if (!(client && cmd)) OSFatal("Failed to allocate client and command block"); FSAddClient(client, 0); FSInitCmdBlock(cmd); // todo: check permissions and throw exception if no mounting permissions available // OSLockMutex - Probably not. It's a single thread, nothing else can access this, Cross-F does this here mount_source m_source; // allocate mount source int ms_result = FSGetMountSource(client, cmd, 0, &m_source, 0); // type 0 = external device if(ms_result != 0) { char buf[256]; __os_snprintf(buf, 256, "FSGetMountSource returned error code %d", ms_result); OSFatal(buf); } char mountPath[128]; // usually /vol/external01 int m_result = FSMount(client, cmd, &m_source, mountPath, sizeof(mountPath), -1); if(m_result != 0) { char buf[256]; __os_snprintf(buf, 256, "FSMount returned error code %d", m_result); OSFatal(buf); } // OSUnlockMutex char defaultMountPath[] = "/vol/external01"; if(!strcmp(mountPath, defaultMountPath)) { char buf[256]; __os_snprintf(buf, 256, "FSMount returned nonstandard mount path: %s", mountPath); OSFatal(buf); } uint32_t file_handle; int open_result = FSOpenFile(client, cmd, "/vol/external01/SMASHD.txt", "r", &file_handle, 0); if(open_result != 0) { char buf[256]; __os_snprintf(buf, 256, "FSOpenFile returned error code %d", open_result); OSFatal(buf); } uint32_t *file_buffer = OSAllocFromSystem(0x200, 0x20); int read_result = FSReadFile(client, cmd, file_buffer, 1, 25, file_handle, 0, -1); // todo: is size correct? one char one byte; read whole file, not just a few bytes if(read_result != 0) { char buf[256]; __os_snprintf(buf, 256, "FSReadFile returned error code %d", read_result); OSFatal(buf); } char *message = (char*)&file_buffer[25]; OSFatal(message); }
static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) { sd_fat_private_t *dev = sd_fat_get_device_data(path); if(!dev) { r->_errno = ENODEV; return -1; } sd_fat_file_state_t *file = (sd_fat_file_state_t *)fileStruct; file->dev = dev; // Determine which mode the file is opened for file->flags = flags; const char *mode_str; if ((flags & 0x03) == O_RDONLY) { file->read = true; file->write = false; file->append = false; mode_str = "r"; } else if ((flags & 0x03) == O_WRONLY) { file->read = false; file->write = true; file->append = (flags & O_APPEND); mode_str = file->append ? "a" : "w"; } else if ((flags & 0x03) == O_RDWR) { file->read = true; file->write = true; file->append = (flags & O_APPEND); mode_str = file->append ? "a+" : "r+"; } else { r->_errno = EACCES; return -1; } int fd = -1; OSLockMutex(dev->pMutex); char *real_path = sd_fat_real_path(path, dev); if(!path) { r->_errno = ENOMEM; OSUnlockMutex(dev->pMutex); return -1; } int result = FSOpenFile(dev->pClient, dev->pCmd, real_path, mode_str, (FSFileHandle*)&fd, -1); free(real_path); if(result == 0) { FSStat stats; result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1); if(result != 0) { FSCloseFile(dev->pClient, dev->pCmd, fd, -1); r->_errno = result; OSUnlockMutex(dev->pMutex); return -1; } file->fd = fd; file->pos = 0; file->len = stats.size; OSUnlockMutex(dev->pMutex); return (int)file; } r->_errno = result; OSUnlockMutex(dev->pMutex); return -1; }