@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final Data data = checkData(qc); final String path = path(1, qc); final Item item = toItem(exprs[2], qc); final Options opts = toOptions(3, Q_OPTIONS, new Options(), qc); final Updates updates = qc.resources.updates(); final IntList docs = data.resources.docs(path); int d = 0; // delete binary resources final IOFile bin = data.meta.binary(path); if (bin == null || bin.isDir()) throw BXDB_REPLACE_X.get(info, path); if (item instanceof Bin) { updates.add(new DBStore(data, path, item, info), qc); } else { if (bin.exists()) updates.add(new DBDelete(data, path, info), qc); final NewInput input = checkInput(item, token(path)); if (docs.isEmpty() || docs.get(0) == 0) { // no replacement of first document (because of TableDiskAccess#insert, used > 0, pre = 0) updates.add(new DBAdd(data, input, opts, qc, info), qc); } else { updates.add(new ReplaceDoc(docs.get(0), data, input, opts, qc, info), qc); d = 1; } } // delete old documents final int ds = docs.size(); for (; d < ds; d++) updates.add(new DeleteNode(docs.get(d), data, info), qc); return null; }
/** Closes and deletes the input files. */ private void close() { str.close(); dat.close(); files.delete(); filed.delete(); sizes.delete(); }
/** * Stores the specified source to the specified file. * * @param in input source * @param file target file * @throws IOException I/O exception */ public static void store(final InputSource in, final IOFile file) throws IOException { // add directory if it does not exist anyway file.dir().md(); final PrintOutput po = new PrintOutput(file.path()); try { final Reader r = in.getCharacterStream(); final InputStream is = in.getByteStream(); final String id = in.getSystemId(); if (r != null) { for (int c; (c = r.read()) != -1; ) po.utf8(c); } else if (is != null) { for (int b; (b = is.read()) != -1; ) po.write(b); } else if (id != null) { final BufferInput bi = new BufferInput(IO.get(id)); try { for (int b; (b = bi.read()) != -1; ) po.write(b); } finally { bi.close(); } } } finally { po.close(); } }
@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())); }
/** * Returns a list of all databases. * * @param ctx database context * @return list of databases */ public static StringList list(final Context ctx) { final StringList db = new StringList(); for (final IOFile f : ctx.mprop.dbpath().children()) { final String name = f.name(); if (f.isDir() && !name.startsWith(".")) db.add(name); } return db.sort(false); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final Data data = checkData(qc); final String path = path(1, qc); if (data.inMemory()) return Bln.FALSE; final IOFile io = data.meta.binary(path); return Bln.get(io.exists() && !io.isDir()); }
/** * 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; } }
/** * Refreshes the list of recent query files and updates the query path. * * @param file new file */ void refreshHistory(final IOFile file) { final StringList sl = new StringList(); String path = null; if (file != null) { path = file.path(); gui.gprop.set(GUIProp.WORKPATH, file.dirPath()); sl.add(path); tabs.setToolTipTextAt(tabs.getSelectedIndex(), path); } final String[] qu = gui.gprop.strings(GUIProp.EDITOR); for (int q = 0; q < qu.length && q < 19; q++) { final String f = qu[q]; if (!f.equalsIgnoreCase(path) && IO.get(f).exists()) sl.add(f); } // store sorted history gui.gprop.set(GUIProp.EDITOR, sl.toArray()); hist.setEnabled(!sl.isEmpty()); }
/** * Saves the specified editor contents. * * @param file file to write * @return {@code false} if confirmation was canceled */ private boolean save(final IOFile file) { try { final EditorArea edit = getEditor(); file.write(edit.getText()); edit.file(file); return true; } catch (final IOException ex) { BaseXDialog.error(gui, FILE_NOT_SAVED); return false; } }
@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); } }
/** * Lists all databases. * * @return success flag * @throws IOException I/O exception */ private boolean list() throws IOException { final Table table = new Table(); table.description = DATABASES_X; final boolean create = context.user.has(Perm.CREATE); table.header.add(T_NAME); table.header.add(RESOURCES); table.header.add(SIZE); if (create) table.header.add(INPUT_PATH); for (final String name : context.databases.listDBs()) { String file = null; long size = 0; int docs = 0; final MetaData meta = new MetaData(name, context); try { meta.read(); size = meta.dbsize(); docs = meta.ndocs; if (context.perm(Perm.READ, meta)) file = meta.original; } catch (final IOException ex) { file = ERROR; } // count number of raw files final IOFile dir = new IOFile(mprop.dbpath(name), M_RAW); final int bin = dir.descendants().size(); // create entry if (file != null) { final TokenList tl = new TokenList(4); tl.add(name); tl.add(docs + bin); tl.add(size); if (create) tl.add(file); table.contents.add(tl); } } out.println(table.sort().finish()); return true; }
@Override protected boolean run() { final boolean create = context.user.has(Perm.CREATE); String path = MetaData.normPath(args[0]); if (path == null || path.endsWith(".")) return error(NAME_INVALID_X, args[0]); if (in == null) { final IO io = IO.get(args[1]); if (!io.exists() || io.isDir()) return error(RES_NOT_FOUND_X, create ? io : args[1]); in = io.inputSource(); // set/add name of document if ((path.isEmpty() || path.endsWith("/")) && !(io instanceof IOContent)) path += io.name(); } // ensure that the final name is not empty if (path.isEmpty()) return error(NAME_INVALID_X, path); // ensure that the name is not empty and contains no trailing dots final Data data = context.data(); if (data.inMemory()) return error(NO_MAINMEM); final IOFile file = data.meta.binary(path); if (path.isEmpty() || path.endsWith(".") || file == null || file.isDir()) return error(NAME_INVALID_X, create ? path : args[0]); // start update if (!data.startUpdate()) return error(DB_PINNED_X, data.meta.name); try { store(in, file); return info(QUERY_EXECUTED_X_X, "", perf); } catch (final IOException ex) { return error(FILE_NOT_STORED_X, Util.message(ex)); } finally { data.finishUpdate(); } }
/** * Opens the specified query file. * * @param file query file * @return opened editor */ public EditorArea open(final IOFile file) { if (!visible()) GUICommands.C_SHOWEDITOR.execute(gui); EditorArea edit = find(file, true); try { if (edit != null) { // display open file tabs.setSelectedComponent(edit); edit.reopen(true); } else { // get current editor edit = getEditor(); // create new tab if current text is stored on disk or has been modified if (edit.opened() || edit.modified) edit = addTab(); edit.initText(file.read()); edit.file(file); } } catch (final IOException ex) { BaseXDialog.error(gui, FILE_NOT_OPENED); } return edit; }