@Override public void execute(final GUI gui) { final DialogExport dialog = new DialogExport(gui); if (!dialog.ok()) return; final IOFile root = new IOFile(dialog.path()); // check if existing files will be overwritten if (root.exists()) { IO file = null; boolean overwrite = false; final Data d = gui.context.data(); final IntList il = d.resources.docs(); final int is = il.size(); for (int i = 0; i < is; i++) { file = root.merge(Token.string(d.text(il.get(i), true))); if (file.exists()) { if (overwrite) { // more than one file will be overwritten; check remaining tests file = null; break; } overwrite = true; } } if (overwrite) { // show message for overwriting files or directories final String msg = file == null ? FILES_REPLACE_X : FILE_EXISTS_X; if (file == null) file = root; if (!BaseXDialog.confirm(gui, Util.info(msg, file))) return; } } DialogProgress.execute(gui, new Export(root.path())); }
@Override public void startUpdate(final MainOptions opts) throws IOException { if (!table.lock(true)) throw new BaseXException(Text.DB_PINNED_X, meta.name); if (opts.get(MainOptions.AUTOFLUSH)) { final IOFile uf = meta.updateFile(); if (uf.exists()) throw new BaseXException(Text.DB_UPDATED_X, meta.name); if (!uf.touch()) throw Util.notExpected("%: could not create lock file.", meta.name); } }
/** * Checks if the table of the specified database is locked. * * @param db name of database * @param ctx database context * @return result of check */ public static boolean locked(final String db, final Context ctx) { final IOFile table = MetaData.file(ctx.globalopts.dbpath(db), DATATBL); if (!table.exists()) return false; try (final RandomAccessFile file = new RandomAccessFile(table.file(), "rw")) { return file.getChannel().tryLock() == null; } catch (final ClosedChannelException ex) { return false; } catch (final OverlappingFileLockException | IOException ex) { return true; } }
@Override public synchronized void finishUpdate(final MainOptions opts) { // remove updating file final boolean auto = opts.get(MainOptions.AUTOFLUSH); if (auto) { final IOFile uf = meta.updateFile(); if (!uf.exists()) throw Util.notExpected("%: lock file does not exist.", meta.name); if (!uf.delete()) throw Util.notExpected("%: could not delete lock file.", meta.name); } // db:optimize(..., true) will close the database before this function is called if (!closed) { flush(auto); if (!table.lock(false)) throw Util.notExpected("Database '%': could not unlock.", meta.name); } }
@Override protected boolean run() throws IOException { final String path = MetaData.normPath(args[0]); if (path == null) return error(NAME_INVALID_X, args[0]); final IOFile bin = context.data().meta.binary(path); if (bin == null || !bin.exists() || bin.isDir()) return error(RES_NOT_FOUND_X, path); try { final BufferInput bi = new BufferInput(bin); try { for (int b; (b = bi.read()) != -1; ) out.write(b); } finally { bi.close(); } return info(QUERY_EXECUTED_X, perf); } catch (final IOException ex) { return error(FILE_NOT_STORED_X, ex); } }