void ui_menu_control_device_image::test_create(bool &can_create, bool &need_confirm) { astring path; osd_directory_entry *entry; osd_dir_entry_type file_type; /* assemble the full path */ zippath_combine(path, current_directory, current_file); /* does a file or a directory exist at the path */ entry = osd_stat(path); file_type = (entry != NULL) ? entry->type : ENTTYPE_NONE; switch(file_type) { case ENTTYPE_NONE: /* no file/dir here - always create */ can_create = true; need_confirm = false; break; case ENTTYPE_FILE: /* a file exists here - ask for permission from the user */ can_create = true; need_confirm = true; break; case ENTTYPE_DIR: /* a directory exists here - we can't save over it */ machine().ui().popup_time(5, "Cannot save over directory"); can_create = false; need_confirm = false; break; default: fatalerror("Unexpected\n"); can_create = false; need_confirm = false; break; } if (entry != NULL) osd_free(entry); }
static int rmdir_recursive(const char *dir_path) { osd_directory *dir; const osd_directory_entry *ent; osd_directory_entry *ent2; char *newpath; dir = osd_opendir(dir_path); if (dir) { while((ent = osd_readdir(dir)) != NULL) { if (strcmp(ent->name, ".") && strcmp(ent->name, "..")) { newpath = (char*)malloc(strlen(dir_path) + 1 + strlen(ent->name) + 1); if (!newpath) return -1; strcpy(newpath, dir_path); strcat(newpath, PATH_SEPARATOR); strcat(newpath, ent->name); ent2 = osd_stat(newpath); if (ent2) { if (ent2->type == ENTTYPE_DIR) rmdir_recursive(newpath); else osd_rmfile(newpath); free(ent2); } free(newpath); } } osd_closedir(dir); } osd_rmdir(dir_path); return 0; }
void menu_control_device_image::test_create(bool &can_create, bool &need_confirm) { // assemble the full path auto path = util::zippath_combine(m_current_directory, m_current_file); // does a file or a directory exist at the path auto entry = osd_stat(path.c_str()); auto file_type = (entry != nullptr) ? entry->type : osd::directory::entry::entry_type::NONE; switch(file_type) { case osd::directory::entry::entry_type::NONE: // no file/dir here - always create can_create = true; need_confirm = false; break; case osd::directory::entry::entry_type::FILE: // a file exists here - ask for permission from the user can_create = true; need_confirm = true; break; case osd::directory::entry::entry_type::DIR: // a directory exists here - we can't save over it ui().popup_time(5, "%s", _("Cannot save over directory")); can_create = false; need_confirm = false; break; default: can_create = false; need_confirm = false; fatalerror("Unexpected\n"); } }
static file_error zippath_resolve(const char *path, osd_dir_entry_type &entry_type, zip_file *&zipfile, std::string &newpath) { file_error err; osd_directory_entry *current_entry = NULL; osd_dir_entry_type current_entry_type; int went_up = FALSE; int i; newpath.clear(); /* be conservative */ entry_type = ENTTYPE_NONE; zipfile = NULL; std::string apath(path); std::string apath_trimmed; do { /* trim the path of trailing path separators */ i = apath.length(); while (i > 1 && is_path_separator(apath[i - 1])) i--; apath = apath.substr(0, i); apath_trimmed.assign(apath); /* stat the path */ current_entry = osd_stat(apath_trimmed.c_str()); /* did we find anything? */ if (current_entry != NULL) { /* get the entry type and free the stat entry */ current_entry_type = current_entry->type; osd_free(current_entry); current_entry = NULL; } else { /* if we have not found the file or directory, go up */ current_entry_type = ENTTYPE_NONE; went_up = TRUE; std::string parent; apath.assign(zippath_parent(parent, apath.c_str())); } } while (current_entry_type == ENTTYPE_NONE && !is_root(apath.c_str())); /* if we did not find anything, then error out */ if (current_entry_type == ENTTYPE_NONE) { err = FILERR_NOT_FOUND; goto done; } /* is this file a ZIP file? */ if ((current_entry_type == ENTTYPE_FILE) && is_zip_file(apath_trimmed.c_str()) && (zip_file_open(apath_trimmed.c_str(), &zipfile) == ZIPERR_NONE)) { i = strlen(path + apath.length()); while (i > 0 && is_zip_path_separator(path[apath.length() + i - 1])) i--; newpath.assign(path + apath.length(), i); /* this was a true ZIP path - attempt to identify the type of path */ zippath_find_sub_path(zipfile, newpath.c_str(), ¤t_entry_type); if (current_entry_type == ENTTYPE_NONE) { err = FILERR_NOT_FOUND; goto done; } } else { /* this was a normal path */ if (went_up) { err = FILERR_NOT_FOUND; goto done; } newpath.assign(path); } /* success! */ entry_type = current_entry_type; err = FILERR_NONE; done: return err; }