Result FS_WriteFile(const char* path, const void* src, u64 size, FS_Archive archive, u32* bytesWritten) { if (!path || !src || !archive || !bytesWritten) return -1; Result ret; Handle fileHandle; debug_print("FS_WriteFile:\n"); ret = FSUSER_OpenFile(&fileHandle, archive, fsMakePath(PATH_ASCII, path), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE); r(" > FSUSER_OpenFile: %lx\n", ret); if (R_FAILED(ret)) return ret; if (R_SUCCEEDED(ret)) { ret = FSFILE_Write(fileHandle, bytesWritten, 0L, src, size, FS_WRITE_FLUSH); r(" > FSFILE_Write: %lx\n", ret); if (R_FAILED(ret) || *bytesWritten != size) ret = -2; } FSFILE_Close(fileHandle); r(" > FSFILE_Close\n"); return ret; }
Result write_savedata(char* path, u8* data, u32 size) { if(!path || !data || !size)return -1; Handle outFileHandle; u32 bytesWritten; Result ret = 0; int fail = 0; ret = FSUSER_OpenFile(&saveGameFsHandle, &outFileHandle, saveGameArchive, FS_makePath(PATH_CHAR, path), FS_OPEN_CREATE | FS_OPEN_WRITE, FS_ATTRIBUTE_NONE); if(ret){fail = -8; goto writeFail;} ret = FSFILE_Write(outFileHandle, &bytesWritten, 0x0, data, size, 0x10001); if(ret){fail = -9; goto writeFail;} ret = FSFILE_Close(outFileHandle); if(ret){fail = -10; goto writeFail;} ret = FSUSER_ControlArchive(saveGameFsHandle, saveGameArchive); writeFail: if(fail)sprintf(status, "Failed to write to file: %d\n %08X %08X", fail, (unsigned int)ret, (unsigned int)bytesWritten); else sprintf(status, "Successfully wrote to file!\n %08X ", (unsigned int)bytesWritten); return ret; }
void DownloadFile_InternalInstall(void* out, unsigned char* buffer, u32 readSize) { u32 bytesWritten; Handle* handle = (Handle*)out; FSFILE_Write(*handle, &bytesWritten, install_offset, buffer, readSize, 0); install_offset += bytesWritten; }
/*! Write to an open file * * @param[in,out] r newlib reentrancy struct * @param[in,out] fd Pointer to sdmc_file_t * @param[in] ptr Pointer to data to write * @param[in] len Length of data to write * * @returns number of bytes written * @returns -1 for error */ static ssize_t sdmc_write(struct _reent *r, int fd, const char *ptr, size_t len) { Result rc; u32 bytes; u32 sync = 0; u64 offset; /* get pointer to our data */ sdmc_file_t *file = (sdmc_file_t*)fd; /* check that the file was opened with write access */ if((file->flags & O_ACCMODE) == O_RDONLY) { r->_errno = EBADF; return -1; } /* check if this is synchronous or not */ if(file->flags & O_SYNC) sync = 0x10001; /* initialize offset */ offset = file->offset; if(file->flags & O_APPEND) { /* append means write from the end of the file */ rc = FSFILE_GetSize(file->fd, &offset); if(rc != 0) { r->_errno = rc; return -1; } } /* TODO: Copy to internal buffer and write in chunks. * You cannot write from read-only memory. */ /* write the data */ rc = FSFILE_Write(file->fd, &bytes, offset, (u32*)ptr, (u32)len, sync); if(rc == 0) { /* update current file offset; if O_APPEND, this moves it to the * new end-of-file */ file->offset = offset + bytes; return (ssize_t)bytes; } r->_errno = rc; return -1; }
ssize_t _vf3dWrite(struct VFile* vf, const void* buffer, size_t size) { struct VFile3DS* vf3d = (struct VFile3DS*) vf; u32 sizeWritten; Result res = FSFILE_Write(vf3d->handle, &sizeWritten, vf3d->offset, buffer, size, FS_WRITE_FLUSH); if (res) { return -1; } vf3d->offset += sizeWritten; return sizeWritten; }
static bool _vf3dSync(struct VFile* vf, const void* buffer, size_t size) { struct VFile3DS* vf3d = (struct VFile3DS*) vf; if (buffer) { u32 sizeWritten; Result res = FSFILE_Write(vf3d->handle, &sizeWritten, 0, buffer, size, FS_WRITE_FLUSH); if (res) { return false; } } FSFILE_Flush(vf3d->handle); return true; }
Result write_savedata(const char* path, const void* data, size_t size) { if(!path || !data || size == 0) return -1; Result ret = -1; int fail = 0; fsUseSession(save_session); ret = FSUSER_OpenArchive(&save_archive, ARCHIVE_SAVEDATA, (FS_Path){PATH_EMPTY, 1, (u8*)""}); if(R_FAILED(ret)) { fail = -1; goto writeFail; } // delete file FSUSER_DeleteFile(save_archive, fsMakePath(PATH_ASCII, path)); FSUSER_ControlArchive(save_archive, ARCHIVE_ACTION_COMMIT_SAVE_DATA, NULL, 0, NULL, 0); Handle file = 0; ret = FSUSER_OpenFile(&file, save_archive, fsMakePath(PATH_ASCII, path), FS_OPEN_CREATE | FS_OPEN_WRITE, 0); if(R_FAILED(ret)) { fail = -2; goto writeFail; } u32 bytes_written = 0; ret = FSFILE_Write(file, &bytes_written, 0, data, size, FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME); if(R_FAILED(ret)) { fail = -3; goto writeFail; } ret = FSFILE_Close(file); if(R_FAILED(ret)) { fail = -4; goto writeFail; } ret = FSUSER_ControlArchive(save_archive, ARCHIVE_ACTION_COMMIT_SAVE_DATA, NULL, 0, NULL, 0); if(R_FAILED(ret)) fail = -5; writeFail: FSUSER_CloseArchive(save_archive); fsEndUseSession(); if(fail) sprintf(status, "Failed to write to file: %d\n %08lX %08lX", fail, ret, bytes_written); else sprintf(status, "Successfully wrote to file!\n %08lX ", bytes_written); return ret; }
int saveBitmap(const char* filename, u8* buffer, u32 w, u32 h) { u8* temp=(u8*)malloc(w*h*3+sizeof(INFOHEADER)+sizeof(HEADER)); HEADER* header=(HEADER*)temp; INFOHEADER* infoheader=(INFOHEADER*)(temp+sizeof(HEADER)); write16(&header->type, 0x4D42); write32(&header->size, w*h*3+sizeof(INFOHEADER)+sizeof(HEADER)); write32(&header->offset, sizeof(INFOHEADER)+sizeof(HEADER)); write16(&header->reserved1, 0); write16(&header->reserved2, 0); write16(&infoheader->bits, 24); write32(&infoheader->size, sizeof(INFOHEADER)); write32(&infoheader->compression, 0); write32(&infoheader->width, w); write32(&infoheader->height, h); write16(&infoheader->planes, 1); write32(&infoheader->imagesize, w*h*3); write32(&infoheader->xresolution, 0); write32(&infoheader->yresolution, 0); write32(&infoheader->importantcolours, 0); write32(&infoheader->ncolours, 0); int y,x; for(y=0;y<h;y++) { for(x=0;x<w;x++) { temp[((y*w)+x)*3+sizeof(INFOHEADER)+sizeof(HEADER)]=buffer[(x*h+y)*3+0]; temp[((y*w)+x)*3+1+sizeof(INFOHEADER)+sizeof(HEADER)]=buffer[(x*h+y)*3+1]; temp[((y*w)+x)*3+2+sizeof(INFOHEADER)+sizeof(HEADER)]=buffer[(x*h+y)*3+2]; } } Handle file; Result ret=FSUSER_OpenFile(NULL, &file, configuration.sdmc, FS_makePath(PATH_CHAR, filename), FS_OPEN_WRITE|FS_OPEN_CREATE, FS_ATTRIBUTE_NONE); if(ret){svcCloseHandle(file); return -2;} u32 size=w*h*3+sizeof(INFOHEADER)+sizeof(HEADER); u32 bytesWritten=0; ret=FSFILE_Write(file, &bytesWritten, 0, temp, size, FS_WRITE_FLUSH); if(ret || bytesWritten!=size)return -2; FSFILE_Close(file); svcCloseHandle(file); free(temp); return 0; }
void dbg_save(char* path, void* buf, int size) { Handle sram; FS_path sramPath; sramPath.type = PATH_CHAR; sramPath.size = strlen(path) + 1; sramPath.data = (u8*)path; Result res = FSUSER_OpenFile(NULL, &sram, sdmcArchive, sramPath, FS_OPEN_CREATE|FS_OPEN_WRITE, FS_ATTRIBUTE_NONE); if ((res & 0xFFFC03FF) == 0) { u32 byteswritten = 0; FSFILE_Write(sram, &byteswritten, 0, (u32*)buf, size, 0x10001); FSFILE_Close(sram); } }
/*! Write to an open file * * @param[in,out] r newlib reentrancy struct * @param[in,out] fd Pointer to sdmc_file_t * @param[in] ptr Pointer to data to write * @param[in] len Length of data to write * * @returns number of bytes written * @returns -1 for error */ static ssize_t sdmc_write(struct _reent *r, void *fd, const char *ptr, size_t len) { Result rc; u32 bytes; u32 sync = 0; /* get pointer to our data */ sdmc_file_t *file = (sdmc_file_t*)fd; /* check that the file was opened with write access */ if((file->flags & O_ACCMODE) == O_RDONLY) { r->_errno = EBADF; return -1; } /* check if this is synchronous or not */ if(file->flags & O_SYNC) sync = FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME; if(file->flags & O_APPEND) { /* append means write from the end of the file */ rc = FSFILE_GetSize(file->fd, &file->offset); if(R_FAILED(rc)) { r->_errno = sdmc_translate_error(rc); return -1; } } rc = FSFILE_Write(file->fd, &bytes, file->offset, (u32*)ptr, len, sync); if(R_FAILED(rc)) { r->_errno = sdmc_translate_error(rc); return -1; } file->offset += bytes; return bytes; }
static int installCia(Handle ciaFile){ Result failed; Handle outputHandle; u64 fileSize; u64 fileOffset = 0; u32 bytesRead; u32 bytesWritten; u8 transferBuffer[FILE_CHUNK_SIZE]; failed = AM_StartCiaInstall(MEDIATYPE_SD, &outputHandle); if(R_FAILED(failed)) return -1; failed = FSFILE_GetSize(ciaFile, &fileSize); if(R_FAILED(failed)) return -1; while(fileOffset < fileSize){ u64 bytesRemaining = fileSize - fileOffset; failed = FSFILE_Read(ciaFile, &bytesRead, fileOffset, transferBuffer, bytesRemaining < FILE_CHUNK_SIZE ? bytesRemaining : FILE_CHUNK_SIZE); if(R_FAILED(failed)){ AM_CancelCIAInstall(outputHandle); return -1; } failed = FSFILE_Write(outputHandle, &bytesWritten, fileOffset, transferBuffer, bytesRead, 0); if(R_FAILED(failed)){ AM_CancelCIAInstall(outputHandle); if(R_DESCRIPTION(failed) == RD_ALREADY_EXISTS) return 1; return -1; } if(bytesWritten != bytesRead){ AM_CancelCIAInstall(outputHandle); return -1; } fileOffset += bytesWritten; } failed = AM_FinishCiaInstall(outputHandle); if(R_FAILED(failed)) return -1; return 1; }
// ================================================== Result FS_saveFile(char* path, void* src, u64 size, FS_archive* fsArchive, Handle* fsHandle, u32* bytesWritten) // -------------------------------------------------- { if (!path || !src || !fsArchive) return -1; Result ret; Handle fileHandle; ret = FSUSER_OpenFile(fsHandle, &fileHandle, *fsArchive, FS_makePath(PATH_CHAR, path), FS_OPEN_WRITE | FS_OPEN_CREATE, FS_ATTRIBUTE_NONE); if (ret) return ret; ret = FSFILE_Write(fileHandle, bytesWritten, 0, src, size, FS_WRITE_NOFLUSH); if (ret) return ret; FSFILE_Close(fileHandle); return ret; }
Result archive_writefile(Archive archive, char *path, u8 *buffer, u32 size) { Result ret=0; Handle filehandle=0; u32 tmpval=0; FILE *f; char filepath[256]; if(archive==SDArchive) { memset(filepath, 0, 256); strncpy(filepath, path, 255); f = fopen(filepath, "w+"); if(f==NULL)return errno; tmpval = fwrite(buffer, 1, size, f); fclose(f); if(tmpval!=size)return -2; return 0; } FS_path fspath = FS_makePath(PATH_CHAR, path); ret = FSUSER_DeleteFile(NULL, extdata_archive, fspath); ret = FSUSER_CreateFile(NULL, extdata_archive, fspath, size); if(ret!=0)return ret; ret = FSUSER_OpenFile(NULL, &filehandle, extdata_archive, fspath, FS_OPEN_WRITE, 0); if(ret!=0)return ret; ret = FSFILE_Write(filehandle, &tmpval, 0, buffer, size, FS_WRITE_FLUSH); FSFILE_Close(filehandle); if(ret==0 && tmpval!=size)ret=-2; return ret; }
static int lua_download(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 2) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); const char* file = luaL_checkstring(L,2); httpcContext context; Result ret = httpcOpenContext(&context, (char*)url , 0); #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); /*httpcReqStatus loading; httpcGetRequestState(&context, &loading); while (loading == 0x5){ httpcGetRequestState(&context, &loading); }*/ u32 statuscode=0; u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); #ifndef SKIP_ERROR_HANDLING if (statuscode != 200) luaL_error(L, "download request error"); #endif httpcGetDownloadSizeState(&context, NULL, &contentsize); u8* buf = (u8*)malloc(contentsize); memset(buf, 0, contentsize); httpcDownloadData(&context, buf, contentsize, NULL); Handle fileHandle; u32 bytesWritten; FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}}; FS_Path filePath=fsMakePath(PATH_ASCII, file); FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000); FSFILE_Write(fileHandle, &bytesWritten, 0, buf, contentsize,0x10001); FSFILE_Close(fileHandle); svcCloseHandle(fileHandle); free(buf); #ifndef SKIP_ERROR_HANDLING }else luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); return 0; }
void DumpSharedRomFS(u8* archive_binary_lowpath) { std::string output_file = BuildSharedRomFSFilename(archive_binary_lowpath); // Read RomFS bin from SaveDataCheck... Handle romfs_handle; u64 romfs_size = 0; u32 romfs_bytes_read = 0; FS_archive savedatacheck_archive = { 0x2345678a, { PATH_BINARY, 16, archive_binary_lowpath } }; u8 file_binary_lowpath[20] = {}; FS_path romfs_path = { PATH_BINARY, 20, file_binary_lowpath }; print(GFX_TOP, "Dumping SaveDataCheck RomFS (%s)... ", output_file.c_str()); FSUSER_OpenFileDirectly(NULL, &romfs_handle, savedatacheck_archive, romfs_path, FS_OPEN_READ, FS_ATTRIBUTE_NONE); FSFILE_GetSize(romfs_handle, &romfs_size); std::unique_ptr<u8> romfs_data_buffer(new u8[romfs_size]); FSFILE_Read(romfs_handle, &romfs_bytes_read, 0, romfs_data_buffer.get(), romfs_size); FSFILE_Close(romfs_handle); // Dump RomFS bin to SDMC... Handle file_handle; u32 bytes_written = 0; FS_path fs_path = FS_makePath(PATH_CHAR, output_file.c_str()); FS_archive sdmc_archive = (FS_archive) { 0x00000009, { PATH_EMPTY, 1, (u8*) "" } }; FSUSER_OpenArchive(NULL, &sdmc_archive); FSUSER_OpenFile(NULL, &file_handle, sdmc_archive, fs_path, FS_OPEN_CREATE | FS_OPEN_WRITE, FS_ATTRIBUTE_NONE); Result res = FSFILE_Write(file_handle, &bytes_written, 0x0, romfs_data_buffer.get(), romfs_size, FS_WRITE_FLUSH); FSFILE_Close(file_handle); FSUSER_CloseArchive(NULL, &sdmc_archive); // Check result... if (res == 0 && bytes_written == romfs_size) print(GFX_TOP, "Done!\n"); else print(GFX_TOP, "Failed!\n"); }
void InstallTicket(std::string FullPath, std::string TitleId) { Handle hTik; u32 writtenbyte; std::string curr = get_file_contents(FullPath.c_str()); // Remove the ticket incase there was a bad one previously installed char* nTitleId = parse_string(TitleId); u64 titleId = u8_to_u64((u8*)nTitleId, BIG_ENDIAN); free (nTitleId); AM_DeleteTicket(titleId); // Install new ticket AM_InstallTicketBegin(&hTik); FSFILE_Write(hTik, &writtenbyte, 0, curr.c_str(), 0x100000, 0); AM_InstallTicketFinish(hTik); printf("Ticket Installed."); //delete temp ticket, ticket folder still exists... ugly. later stream directly to the handle remove(FullPath.c_str()); }
Result putFile(u8 * buffer, u64 size) { Result ret = 0; Handle filehandle = 0; u32 tmpval = 0; ret = FSUSER_DeleteFile(extdata_archive, verListPath); ret = FSUSER_CreateFile(extdata_archive, verListPath, 0, size); if (ret != 0) return ret; ret = FSUSER_OpenFile(&filehandle, extdata_archive, verListPath, FS_OPEN_WRITE, 0); if (ret != 0) return ret; ret = FSFILE_Write(filehandle, &tmpval, 0, buffer, size, FS_WRITE_FLUSH); FSFILE_Close(filehandle); return ret; }
/*! Write to an open file * * @param[in,out] r newlib reentrancy struct * @param[in,out] fd Pointer to sdmc_file_t * @param[in] ptr Pointer to data to write * @param[in] len Length of data to write * * @returns number of bytes written * @returns -1 for error */ static ssize_t sdmc_write_safe(struct _reent *r, void *fd, const char *ptr, size_t len) { Result rc; u32 bytes, bytesWritten = 0; u32 sync = 0; /* get pointer to our data */ sdmc_file_t *file = (sdmc_file_t*)fd; /* check that the file was opened with write access */ if((file->flags & O_ACCMODE) == O_RDONLY) { r->_errno = EBADF; return -1; } /* check if this is synchronous or not */ if(file->flags & O_SYNC) sync = FS_WRITE_FLUSH | FS_WRITE_UPDATE_TIME; if(file->flags & O_APPEND) { /* append means write from the end of the file */ rc = FSFILE_GetSize(file->fd, &file->offset); if(R_FAILED(rc)) { r->_errno = sdmc_translate_error(rc); return -1; } } /* Copy to internal buffer and write in chunks. * You cannot write from read-only memory. */ static __thread char tmp_buffer[8192]; while(len > 0) { size_t toWrite = len; if(toWrite > sizeof(tmp_buffer)) toWrite = sizeof(tmp_buffer); /* copy to internal buffer */ memcpy(tmp_buffer, ptr, toWrite); /* write the data */ rc = FSFILE_Write(file->fd, &bytes, file->offset, (u32*)tmp_buffer, (u32)toWrite, sync); if(R_FAILED(rc)) { /* return partial transfer */ if(bytesWritten > 0) return bytesWritten; r->_errno = sdmc_translate_error(rc); return -1; } file->offset += bytes; bytesWritten += bytes; ptr += bytes; len -= bytes; } return bytesWritten; }
int installTitleFromCIA(const char *path, PrintConsole *con) { Handle CIAIn, CIAOut; u32 bytesread; u32 byteswritten; u64 totalread; int animpos; LOG_INFO("installTitleFromCIA: Installing CIA from %s.", path); CIAIn = openFileHandle(path, FS_OPEN_READ); if(CIAIn == 0) { LOG_ERROR("installTitleFromCIA: Failed to open %s.", path); printf("Failed to open file %s.\n", path); goto error0; } #ifdef ARMED if(R_FAILED(AM_StartCiaInstall(MEDIATYPE_NAND, &CIAOut))) { LOG_ERROR("installTitleFromCIA: AM_StartCiaInstall failed."); printf("Failed to start CIA install.\n"); goto error1; } #endif bytesread = BUFFERSIZE; totalread = 0; while(bytesread == BUFFERSIZE) { printf("%c", anim[animpos]); stepFrame(); con->cursorX--; animpos = (animpos + 1) % 4; if(R_FAILED(FSFILE_Read(CIAIn, &bytesread, totalread, buffer, BUFFERSIZE))) { printf("\nFailed to read %s around %llu!\n", path, totalread); stepFrame(); goto error2; } #ifdef ARMED if(R_FAILED(FSFILE_Write(CIAOut, &byteswritten, totalread, buffer, bytesread, FS_WRITE_FLUSH))) { printf("\nFailed to write %s around %llu!\n", path, totalread); stepFrame(); goto error2; } if(byteswritten < bytesread) { LOG_ERROR("installTitleFromCIA: Incomplete write around %llu.", totalread); printf("\nIncompelete write around %llu!\n", totalread); stepFrame(); goto error2; } #endif totalread += bytesread; } #ifdef ARMED if(R_FAILED(AM_FinishCiaInstall(MEDIATYPE_NAND, &CIAOut))) { LOG_ERROR("installTitleFromCIA: AM_FinishCiaInstall failed."); printf("Failed to finalize CIA install.\n"); goto error2; } #endif closeFileHandle(CIAIn); LOG_INFO("Successfully installed %s.", path); return(0); error2: #ifdef ARMED if(R_FAILED(AM_CancelCIAInstall(&CIAOut))) { printf("Couldn't cancel unsuccessful CIA install.\n"); } #endif error1: closeFileHandle(CIAIn); error0: LOG_ERROR("Failed installing %s.", path); return(-1); }
static void _vf3dUnmap(struct VFile* vf, void* memory, size_t size) { struct VFile3DS* vf3d = (struct VFile3DS*) vf; u32 sizeWritten; FSFILE_Write(vf3d->handle, &sizeWritten, 0, memory, size, FS_WRITE_FLUSH); mappedMemoryFree(memory, size); }
static int lua_download(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc < 2 || argc > 5) return luaL_error(L, "wrong number of arguments"); #endif const char* url = luaL_checkstring(L,1); const char* file = luaL_checkstring(L,2); const char* headers = (argc >= 3) ? luaL_checkstring(L,3) : NULL; u8 method = (argc >= 4) ? luaL_checkinteger(L,4) : 0; const char* postdata = (argc >= 5) ? luaL_checkstring(L,5) : NULL; httpcContext context; u32 statuscode=0; HTTPC_RequestMethod useMethod = HTTPC_METHOD_GET; if(method <= 3 && method >= 1) useMethod = (HTTPC_RequestMethod)method; do { if (statuscode >= 301 && statuscode <= 308) { char newurl[4096]; httpcGetResponseHeader(&context, (char*)"Location", &newurl[0], 4096); url = &newurl[0]; httpcCloseContext(&context); } Result ret = httpcOpenContext(&context, useMethod, (char*)url, 0); // Just disable SSL verification instead of loading default certs. httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); if(headers != NULL){ char *tokenheader = (char*)malloc(strlen(headers)+1); strcpy(tokenheader, headers); char *toker = tokenheader; char *headername = NULL; char *headervalue = NULL; do { headername = strtok(toker, ":"); if (headername == NULL) break; headervalue = strtok(NULL, "\n"); if (headervalue == NULL) break; if (headervalue[0] == ' ') headervalue++; httpcAddRequestHeaderField(&context, headername, headervalue); toker = NULL; } while (headername != NULL && headervalue != NULL); free(tokenheader); } if (useMethod == HTTPC_METHOD_POST && postdata != NULL) { httpcAddPostDataRaw(&context, (u32*)postdata, strlen(postdata)); } #ifndef SKIP_ERROR_HANDLING if(ret==0){ #endif httpcBeginRequest(&context); u32 contentsize=0; httpcGetResponseStatusCode(&context, &statuscode, 0); if (statuscode == 200){ u32 readSize = 0; long int bytesWritten = 0; u8* buf = (u8*)malloc(0x1000); memset(buf, 0, 0x1000); Handle fileHandle; FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}}; FS_Path filePath=fsMakePath(PATH_ASCII, file); FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000); do { ret = httpcDownloadData(&context, buf, 0x1000, &readSize); FSFILE_Write(fileHandle, NULL, bytesWritten, buf, readSize, 0x10001); bytesWritten += readSize; } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); FSFILE_Close(fileHandle); svcCloseHandle(fileHandle); free(buf); } #ifndef SKIP_ERROR_HANDLING } #endif } while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)); #ifndef SKIP_ERROR_HANDLING if ((statuscode < 200 && statuscode > 226) && statuscode != 304) luaL_error(L, "error opening url"); #endif httpcCloseContext(&context); lua_pushinteger(L, statuscode); return 1; }
AppResult appInstall(MediaType mediaType, FILE* fd, u64 size, std::function<bool(u64 pos, u64 totalSize)> onProgress) { if(!serviceRequire("am")) { return APP_AM_INIT_FAILED; } if(onProgress != NULL) { onProgress(0, size); } Handle ciaHandle; Result startResult = AM_StartCiaInstall(appMediatypeToByte(mediaType), &ciaHandle); if(startResult != 0) { platformSetError(serviceParseError((u32) startResult)); return APP_BEGIN_INSTALL_FAILED; } u32 bufSize = 1024 * 128; // 128KB void* buf = malloc(bufSize); bool cancelled = false; u64 pos = 0; while(platformIsRunning()) { if(onProgress != NULL && !onProgress(pos, size)) { cancelled = true; break; } size_t bytesRead = fread(buf, 1, bufSize, fd); if(bytesRead > 0) { Result writeResult = FSFILE_Write(ciaHandle, NULL, pos, buf, (u32) bytesRead, FS_WRITE_NOFLUSH); if(writeResult != 0) { AM_CancelCIAInstall(&ciaHandle); platformSetError(serviceParseError((u32) writeResult)); return APP_INSTALL_ERROR; } pos += bytesRead; } if((ferror(fd) && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINPROGRESS) || (size != 0 && pos == size)) { break; } } free(buf); if(cancelled) { AM_CancelCIAInstall(&ciaHandle); return APP_OPERATION_CANCELLED; } if(!platformIsRunning()) { AM_CancelCIAInstall(&ciaHandle); return APP_PROCESS_CLOSING; } if(size != 0 && pos != size) { AM_CancelCIAInstall(&ciaHandle); return APP_IO_ERROR; } if(onProgress != NULL) { onProgress(size, size); } Result finishResult = AM_FinishCiaInstall(appMediatypeToByte(mediaType), &ciaHandle); if(finishResult != 0) { platformSetError(serviceParseError((u32) finishResult)); return APP_FINALIZE_INSTALL_FAILED; } return APP_SUCCESS; }
static Result action_install_tickets_write_dst(void* data, u32 handle, u32* bytesWritten, void* buffer, u64 offset, u32 size) { return FSFILE_Write(handle, bytesWritten, offset, buffer, size, 0); }
static Result dumpnand_write_dst(void* data, u32 handle, u32* bytesWritten, void* buffer, u64 offset, u32 size) { return FSFILE_Write(handle, bytesWritten, offset, buffer, size, 0); }