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; pasteData->currExists = R_SUCCEEDED(FSUSER_OpenFile(&currHandle, pasteData->target->archive, *fsPath, FS_OPEN_READ, 0)); if(pasteData->currExists) { FSFILE_Close(currHandle); } else { res = FSUSER_CreateFile(pasteData->target->archive, *fsPath, 0, size); } if(R_SUCCEEDED(res)) { 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; }
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; }
/*! Open a file * * @param[in,out] r newlib reentrancy struct * @param[out] fileStruct Pointer to file struct to fill in * @param[in] path Path to open * @param[in] flags Open flags from open(2) * @param[in] mode Permissions to set on create * * @returns 0 for success * @returns -1 for error */ static int sdmc_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) { Handle fd; Result rc; u32 sdmc_flags = 0; u32 attributes = 0; FS_Path fs_path; fs_path = sdmc_utf16path(r, path); if(fs_path.data == NULL) return -1; /* get pointer to our data */ sdmc_file_t *file = (sdmc_file_t*)fileStruct; /* check access mode */ switch(flags & O_ACCMODE) { /* read-only: do not allow O_APPEND */ case O_RDONLY: sdmc_flags |= FS_OPEN_READ; if(flags & O_APPEND) { r->_errno = EINVAL; return -1; } break; /* write-only */ case O_WRONLY: sdmc_flags |= FS_OPEN_WRITE; break; /* read and write */ case O_RDWR: sdmc_flags |= (FS_OPEN_READ | FS_OPEN_WRITE); break; /* an invalid option was supplied */ default: r->_errno = EINVAL; return -1; } /* create file */ if(flags & O_CREAT) sdmc_flags |= FS_OPEN_CREATE; /* Test O_EXCL. */ if((flags & O_CREAT) && (flags & O_EXCL)) { rc = FSUSER_CreateFile(sdmcArchive, fs_path, attributes, 0); if(R_FAILED(rc)) { r->_errno = sdmc_translate_error(rc); return -1; } } /* set attributes */ /*if(!(mode & S_IWUSR)) attributes |= FS_ATTRIBUTE_READONLY;*/ /* open the file */ rc = FSUSER_OpenFile(&fd, sdmcArchive, fs_path, sdmc_flags, attributes); if(R_SUCCEEDED(rc)) { if((flags & O_ACCMODE) != O_RDONLY && (flags & O_TRUNC)) { rc = FSFILE_SetSize(fd, 0); if(R_FAILED(rc)) { FSFILE_Close(fd); r->_errno = sdmc_translate_error(rc); return -1; } } file->fd = fd; file->flags = (flags & (O_ACCMODE|O_APPEND|O_SYNC)); file->offset = 0; return 0; } r->_errno = sdmc_translate_error(rc); return -1; }