// don't bother with log messages, this is only used before the logging is started int deleteFile(const char *path) { FS_Path filePath = fsMakePath(PATH_ASCII, path); if(R_FAILED(FSUSER_DeleteFile(sdmcArchive, filePath))) { return(-1); } return(0); }
// ================================================== Result FS_deleteFile(char* path, Handle* fsHandle, FS_archive* fsArchive) // -------------------------------------------------- { if (!path || !fsHandle || !fsArchive) return -1; Result ret; ret = FSUSER_DeleteFile(fsHandle, *fsArchive, FS_makePath(PATH_CHAR, path)); return ret; }
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; }
Result FS_DeleteFile(const char* path, FS_Archive archive) { if (!path || !archive) return -1; Result ret; debug_print("FS_DeleteFile:\n"); ret = FSUSER_DeleteFile(archive, fsMakePath(PATH_ASCII, path)); r(" > FSUSER_DeleteFile: %lx\n", ret); return ret; }
static bool _vd3dDeleteFile(struct VDir* vd, const char* path) { struct VDir3DS* vd3d = (struct VDir3DS*) vd; if (!path) { return 0; } const char* dir = vd3d->path; char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2)); sprintf(combined, "%s/%s", dir, path); uint16_t utf16Path[PATH_MAX + 1]; ssize_t units = utf8_to_utf16(utf16Path, (const uint8_t*) combined, PATH_MAX); utf16Path[units] = 0; FS_Path newPath = fsMakePath(PATH_UTF16, utf16Path); bool ret = !FSUSER_DeleteFile(sdmcArchive, newPath); free(combined); 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; }
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; }
static Result action_paste_files_open_dst(void* data, u32 index, void* initialReadBlock, u64 size, u32* handle) { paste_files_data* pasteData = (paste_files_data*) data; Result res = 0; char dstPath[FILE_PATH_MAX]; action_paste_files_get_dst_path(pasteData, index, dstPath); FS_Path* fsPath = util_make_path_utf8(dstPath); if(fsPath != NULL) { Handle currHandle; if(R_SUCCEEDED(FSUSER_OpenFile(&currHandle, pasteData->target->archive, *fsPath, FS_OPEN_READ, 0))) { FSFILE_Close(currHandle); if(R_SUCCEEDED(res = FSUSER_DeleteFile(pasteData->target->archive, *fsPath))) { linked_list_iter iter; linked_list_iterate(pasteData->items, &iter); while(linked_list_iter_has_next(&iter)) { list_item* item = (list_item*) linked_list_iter_next(&iter); file_info* currInfo = (file_info*) item->data; if(strncmp(currInfo->path, dstPath, FILE_PATH_MAX) == 0) { linked_list_iter_remove(&iter); task_free_file(item); } } } } if(R_SUCCEEDED(res) && R_SUCCEEDED(res = FSUSER_CreateFile(pasteData->target->archive, *fsPath, ((file_info*) ((list_item*) linked_list_get(&pasteData->contents, index))->data)->attributes & ~FS_ATTRIBUTE_READ_ONLY, size))) { res = FSUSER_OpenFile(handle, pasteData->target->archive, *fsPath, FS_OPEN_WRITE, 0); } util_free_path_utf8(fsPath); } else { res = R_FBI_OUT_OF_MEMORY; } return res; }
bool installTTP(char* path, u8 mediatype) { // Install a TTP file. (needs libzip and installCIA) Result res; FS_Archive archive = {ARCHIVE_SDMC, {PATH_EMPTY, 0, 0}}; FSUSER_OpenArchive(&archive); FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CreateDirectory(archive, fsMakePath(PATH_ASCII, "/tmp/cias"), 0); FILE* ttp = fopen(path, "rb"); FILE* tmp = fopen("/tmp/cias/ttp.tmp", "wb"); u32 titlesAmount; AM_GetTitleCount(mediatype, &titlesAmount); u64* titleIDs = malloc(sizeof(u64) * titlesAmount); AM_GetTitleIdList(mediatype, titlesAmount, titleIDs); u32 size; fseek(ttp, 0x19, SEEK_SET); fread(&size, 0x4, 1, ttp); fseek(ttp, 0x1D, SEEK_SET); u32 blockAmount = size / 0x160000; // Finds how many blocks of 4MB you have in the file u32 i; char* block = malloc(0x160000); for (i = 0; i < blockAmount; i++) { fread(block, 1, 0x160000, ttp); fwrite(block, 1, 0x160000, tmp); } if (size % 0x160000 != 0) { fread(block, 1, size-0x160000*blockAmount, ttp); fwrite(block, 1, size-0x160000*blockAmount, tmp); } free(block); fclose(ttp); fclose(tmp); FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CreateDirectory(archive, fsMakePath(PATH_ASCII, "/tmp/cias"), 0); Zip *zipHandle = ZipOpen("/tmp/cias/ttp.tmp"); ZipExtract(zipHandle, NULL); ZipClose(zipHandle); Handle ciaDir; FS_Archive fsarchive; u32 actualAmount; FS_DirectoryEntry* entries; FSUSER_DeleteFile(archive, fsMakePath(PATH_ASCII, "/tmp/cias/ttp.tmp")); res = FSUSER_OpenDirectory(&ciaDir, fsarchive, fsMakePath(PATH_ASCII, "/tmp/cias")); if (res != 0) { free(titleIDs); return false; } entries = malloc(256 * sizeof(FS_DirectoryEntry)); res = FSDIR_Read(ciaDir, &actualAmount, 256, entries); if (res != 0) { free(titleIDs); return false; } char* ciaPath; Handle ciaFileHandle; AM_TitleEntry ciaInfo; for (i = 0; i < actualAmount; i++) { ciaPath = malloc(14 + strlen(entries[i].shortName)); strcpy(ciaPath, "/tmp/cias/"); strcat(ciaPath, entries[i].shortName); strcat(ciaPath, ".cia"); FSUSER_OpenFile(&ciaFileHandle, archive, fsMakePath(PATH_ASCII, ciaPath), FS_OPEN_READ, 0); AM_GetCiaFileInfo(mediatype, &ciaInfo, ciaFileHandle); FSFILE_Close(ciaFileHandle); if (ciaInfo.titleID == 0x0004013800000002LL || ciaInfo.titleID == 0x0004013820000002LL) { if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) // Tries to install the CIA 3 times then give up. If it has to give up, that probably means brick. if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) return false; break; } free(ciaPath); } for (i = 0; i < actualAmount; i++) { ciaPath = malloc(14 + strlen(entries[i].shortName)); strcpy(ciaPath, "/tmp/cias/"); strcat(ciaPath, entries[i].shortName); strcat(ciaPath, ".cia"); if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) if (!installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName)) // Tries to install the CIA 3 times then give up. If it has to give up, that probably means brick. installCIA(ciaPath, mediatype, titleIDs, entries[i].shortName); free(ciaPath); } FSUSER_DeleteDirectoryRecursively(archive, fsMakePath(PATH_ASCII, "/tmp/cias")); FSUSER_CloseArchive(&archive); free(titleIDs); return true; }