bool Bitmap::LoadFile(const TCHAR *path) { NarrowPathName narrow_path(path); SDL_Surface *original = ::IMG_Load(narrow_path); return original != NULL && Load(original); }
static int dfs_open(const char *path, struct fuse_file_info *fi) { int out; pthread_mutex_lock(&treeMut); path = narrow_path(path); out = _dfs_open(path, fi); pthread_mutex_unlock(&treeMut); return out; }
static int dfs_getattr(const char *path, struct stat *stbuf) { path = narrow_path(path); DfsFile *f = findFile((char *)path); dfs_out("GETATTR: '%s' (%ld)\n", path, f); if (!f) return -ENOENT; *stbuf = f->stat; return 0; }
struct zzip_dir * RasterWeatherStore::OpenArchive() { const auto path = LocalPath(_T(RASP_FILENAME)); NarrowPathName narrow_path(path); if (!narrow_path.IsDefined()) return nullptr; return zzip_dir_open(narrow_path, nullptr); }
int dfs_chmod(const char *path, mode_t mode) { int out; DfsFile *f; pthread_mutex_lock(&treeMut); path = narrow_path(path); out = _dfs_chmod(path, mode); if (out) { pthread_mutex_unlock(&treeMut); return out; } f = findFile((char *)path); logOther(LOG_CHMOD, path, mode, &f->stat); pthread_mutex_unlock(&treeMut); return out; }
int dfs_rmdir(const char *path) { int out; pthread_mutex_lock(&treeMut); path = narrow_path(path); if (remove_delete) { pthread_mutex_unlock(&treeMut); return -EACCES; } out = _dfs_rmdir(path); if (out) { pthread_mutex_unlock(&treeMut); return out; } logOther(LOG_RMDIR, path, 0, NULL); pthread_mutex_unlock(&treeMut); return out; }
int dfs_mkdir(const char *path, mode_t mode) { int out; pthread_mutex_lock(&treeMut); path = narrow_path(path); if (remove_create) { pthread_mutex_unlock(&treeMut); return -EACCES; } out = _dfs_mkdir(path, mode); if (out) { pthread_mutex_unlock(&treeMut); return out; } logOther(LOG_MKDIR, path, mode, NULL); pthread_mutex_unlock(&treeMut); return out; }
int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) { pthread_mutex_lock(&treeMut); path = narrow_path(path); if (remove_create) { pthread_mutex_unlock(&treeMut); return -EACCES; } dfs_out("CREATE: '%s'\n", path); DfsFile *f = findFile((char *)path); DfsFile *dir; char *dname, *fname; if (f) { pthread_mutex_unlock(&treeMut); return -EEXIST; } if (!(fname = strrchr(path, '/'))) { pthread_mutex_unlock(&treeMut); return -EINVAL; } dname = strdup(path); dname[fname - path] = 0; fname++; if (!(dir = findFile(dname))) { free(dname); pthread_mutex_unlock(&treeMut); return -EINVAL; } f = mkNode(path, fname, dir, DEF_FILE_MODE); dfs_out("CREATE OUT, now %d children in '%s'\n", dir->num_children, dname); free(dname); f->version++; logFileVersion(f); pthread_mutex_unlock(&treeMut); return 0; }
static int dfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { size_t len; pthread_mutex_lock(&treeMut); path = narrow_path(path); dfs_out("WRITE: '%s', sz %d, offset %d\n", path, size, offset); if (remove_write) { pthread_mutex_unlock(&treeMut); return -EACCES; } DfsFile *f = findFile((char *)path); if (!f) { pthread_mutex_unlock(&treeMut); return -ENOENT; } if (f->stat.st_mode & S_IFDIR) { pthread_mutex_unlock(&treeMut); return -EISDIR; } // Handles issue where a collision can cause us to fail this check if(!(!f->recipelen || f->data)) { struct fuse_file_info fii; fii.flags = 0; _dfs_open(path, &fii); } //assert(!f->recipelen || f->data); if ((size + offset) > f->len) { f->data = (char *)realloc(f->data, size + offset); f->stat.st_size = f->len = size + offset; } memcpy(f->data + offset, buf, size); f->dirty = 1; f->stat.st_mtime = f->stat.st_atime = time(NULL); pthread_mutex_unlock(&treeMut); return size; }
int _dfs_unlink(const char *path) { /* Assumes treeMut is locked */ path = narrow_path(path); DfsFile *f; dfs_out("Unlink '%s'\n", path); if (!(f = findFile((char *)path))) return -ENOENT; if (f->stat.st_mode & S_IFDIR) return -EISDIR; if (--f->stat.st_nlink) return 0; freeNode(path, f); return 0; }
static int dfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { size_t len; pthread_mutex_lock(&treeMut); path = narrow_path(path); dfs_out("READ: '%s', sz %d, offset %d\n", path, size, offset); DfsFile *f = findFile((char *)path); if (!f) { pthread_mutex_unlock(&treeMut); return -ENOENT; } if (size && !f->len) { pthread_mutex_unlock(&treeMut); return 0; } if (f->stat.st_mode & S_IFDIR) { pthread_mutex_unlock(&treeMut); return -EISDIR; } len = f->len; if (offset < len) { if (offset + size > len) size = len - offset; memcpy(buf, f->data + offset, size); } else size = 0; f->stat.st_atime = time(NULL); pthread_mutex_unlock(&treeMut); return size; }
static int dfs_truncate(const char *path, off_t sz) { DfsFile *f; pthread_mutex_lock(&treeMut); dfs_out("\n\tFUSE TRUNCATE\n\n"); path = narrow_path(path); if (remove_write) { pthread_mutex_unlock(&treeMut); return -EACCES; } if (!(f = findFile((char *)path))) { pthread_mutex_unlock(&treeMut); return -ENOENT; } dfs_out("TRUNCATE to %d, was %d\n", sz, f->len); if (sz < f->len) { f->len = f->stat.st_size = sz; f->dirty = 1; } pthread_mutex_unlock(&treeMut); return 0; }
static int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { pthread_mutex_lock(&treeMut); path = narrow_path(path); DfsFile *f = findFile((char *)path); int i; dfs_out("READDIR: '%s'\n", path); if (!f) { pthread_mutex_unlock(&treeMut); return -ENOENT; } filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); for (i = 0; i < f->num_children; i++) { filler(buf, f->child[i]->name, NULL, 0); } pthread_mutex_unlock(&treeMut); return 0; }
// If file dirty, chunkify, sent extents to extentserver, and send // recipe to recipes server. In all cases, toss local file from hash table. static int dfs_flush(const char *path, struct fuse_file_info *fi) { DfsFile *f; pthread_mutex_lock(&treeMut); path = narrow_path(path); dfs_out("DFS_FLUSH '%s\n", path); if (!(f = findFile((char *)path))) { pthread_mutex_unlock(&treeMut); return -EINVAL; } assert(f); dfs_out("\tflush '%s': len %d, dirty %d, data %x\n", f->path, f->len, f->dirty, f->data); // If no data, nothing to flush... if (!f->data || !f->dirty) { free(f->data); f->data = NULL; pthread_mutex_unlock(&treeMut); return 0; } // Need to create extents. For now, just chop up into 4k blocks. char *buf = f->data, *bufend = buf + f->len; char *sig; Extent *ex, *last; int blocks = f->len / BLOCK_SIZE; if (f->len % BLOCK_SIZE) blocks++; f->recipelen = blocks * A_HASH_SIZE; free(f->recipe); f->recipe = malloc(f->recipelen); char *curr = f->recipe; while (buf < bufend) { int sz = MIN(bufend - buf, BLOCK_SIZE); char *hash; hash = put_extent(buf, sz); assert(hash); dfs_out("PUT extent size %d, '%s'\n", sz, hash); strcpy(curr, hash); curr += A_HASH_SIZE; free(hash); buf += sz; } assert(curr == (f->recipe + f->recipelen)); free(f->data); f->data = NULL; f->version++; f->dirty = 0; logFileVersion(f); pthread_mutex_unlock(&treeMut); return 0; }
void RequestMidiFilename(std::wstring *returned_filename, std::wstring *returned_file_title) { // Grab the filename of the last song we played // and pre-load it into the open dialog wstring last_filename = UserSetting::Get(L"Last File", L""); const static int BufferSize = 512; wchar_t filename[BufferSize] = L""; wchar_t filetitle[BufferSize] = L""; #ifdef WIN32 // Try to populate our "File Open" box with the last file selected if (StringCbCopyW(filename, BufferSize, last_filename.c_str()) == STRSAFE_E_INSUFFICIENT_BUFFER) { // If there wasn't a last file, default to the built-in Music directory filename[0] = L'\0'; } wstring default_dir; bool default_directory = false; if (last_filename.length() == 0) { default_directory = true; default_dir = UserSetting::Get(L"Default Music Directory", L""); if (!SetCurrentDirectory(default_dir.c_str())) { // LOGTODO! // This is non-critical. No action required. } } OPENFILENAME ofn; ZeroMemory(&ofn, sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = 0; ofn.lpstrTitle = L"Piano Game: Choose a MIDI song to play"; ofn.lpstrFilter = L"MIDI Files (*.mid)\0*.mid;*.midi\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = filename; ofn.nMaxFile = BufferSize; ofn.lpstrFileTitle = filetitle; ofn.lpstrInitialDir = default_dir.c_str(); ofn.nMaxFileTitle = BufferSize; ofn.lpstrDefExt = L"mid"; ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; if (GetOpenFileName(&ofn)) { std::wstring filename = WSTRING(ofn.lpstrFile); SetLastMidiFilename(filename); if (returned_file_title) *returned_file_title = WSTRING(filetitle); if (returned_filename) *returned_filename = filename; return; } if (returned_file_title) *returned_file_title = L""; if (returned_filename) *returned_filename = L""; #else OSStatus status; NavDialogCreationOptions options; status = NavGetDefaultDialogCreationOptions(&options); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't create dialog options. Error code: " << static_cast<int>(status))); options.windowTitle = CFSTR("Piano Game: Choose a MIDI song to play"); // TODO: Should clean this up at shut-down static NavObjectFilterUPP navFilterUPP(0); if (navFilterUPP == 0) navFilterUPP = NewNavObjectFilterUPP(NavOpenFilterProc); NavDialogRef navDialog(0); status = NavCreateChooseFileDialog(&options, 0, 0, 0, navFilterUPP, 0, &navDialog); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't create open dialog. Error code: " << static_cast<int>(status))); status = NavDialogRun(navDialog); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't run open dialog. Error code: " << static_cast<int>(status))); NavReplyRecord navReply; status = NavDialogGetReply(navDialog, &navReply); if (status == userCanceledErr || !navReply.validRecord) { NavDisposeReply(&navReply); if (returned_file_title) *returned_file_title = L""; if (returned_filename) *returned_filename = L""; return; } long item_count = 0; status = AECountItems(&navReply.selection, &item_count); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't count resulting items from open dialog. Error code: " << static_cast<int>(status))); for (long i = 1; i <= item_count; i++) { FSRef fsRef; status = AEGetNthPtr(&navReply.selection, i, typeFSRef, 0, 0, &fsRef, sizeof(FSRef), 0); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get FSRef pointer from open dialog. Error code: " << static_cast<int>(status))); CFStringRef file_title; status = LSCopyDisplayNameForRef( &fsRef, &file_title ); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get file title. Error code: " << static_cast<int>(status))); const static int BufferSize(1024); char path_buffer[BufferSize]; status = FSRefMakePath(&fsRef, (UInt8*)path_buffer, BufferSize); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't get file path. Error code: " << static_cast<int>(status))); std::string narrow_path(path_buffer); std::wstring filepath(narrow_path.begin(), narrow_path.end()); if (returned_file_title) *returned_file_title = WideFromMacString(file_title); if (returned_filename) *returned_filename = filepath; CFRelease(file_title); } NavDisposeReply(&navReply); #endif }