/** * Create the directory and all required parent directories. * * @param dir the directory name */ public static void createDirectories(String dir) { if (dir != null) { if (exists(dir)) { if (!isDirectory(dir)) { // this will fail createDirectory(dir); } } else { String parent = getParent(dir); createDirectories(parent); createDirectory(dir); } } }
/** * Delete a directory or file and all subdirectories and files. * * @param path the path * @param tryOnly whether errors should be ignored */ public static void deleteRecursive(String path, boolean tryOnly) { if (exists(path)) { if (isDirectory(path)) { for (String s : newDirectoryStream(path)) { deleteRecursive(s, tryOnly); } } if (tryOnly) { tryDelete(path); } else { delete(path); } } }
FileUtils.createDirectories(FileUtils.getParent(fileName)); if (!FileUtils.createFile(fileName)) { waitUntilOld(); long read = FileUtils.lastModified(fileName); Properties p2 = load(); String m2 = p2.getProperty("method", SOCKET); if (read != FileUtils.lastModified(fileName)) { throw getExceptionFatal("Concurrent update", null); FileUtils.delete(fileName); if (!FileUtils.createFile(fileName)) { throw getExceptionFatal("Another process was faster", null);
private static void process(String fileName, boolean quiet) { if (FileUtils.isDirectory(fileName)) { // only delete empty directories FileUtils.tryDelete(fileName); } else if (quiet || fileName.endsWith(Constants.SUFFIX_TEMP_FILE) || fileName.endsWith(Constants.SUFFIX_TRACE_FILE)) { FileUtils.tryDelete(fileName); } else { FileUtils.delete(fileName); } }
private boolean openWriter() { if (printWriter == null) { try { FileUtils.createDirectories(FileUtils.getParent(fileName)); if (FileUtils.exists(fileName) && !FileUtils.canWrite(fileName)) { // read only database: don't log error if the trace file // can't be opened return false; } fileWriter = IOUtils.getBufferedWriter( FileUtils.newOutputStream(fileName, true)); printWriter = new PrintWriter(fileWriter, true); } catch (Exception e) { logWritingError(e); return false; } } return true; }
KEYSTORE_PASSWORD), KEYSTORE_PASSWORD); boolean needWrite = true; if (FileUtils.exists(fileName) && FileUtils.size(fileName) == data.length) { InputStream fin = FileUtils.newInputStream(fileName); byte[] now = IOUtils.readBytesAndClose(fin, 0); if (now != null && Arrays.equals(data, now)) { OutputStream out = FileUtils.newOutputStream(fileName, false); out.write(data); out.close(); String absolutePath = FileUtils.toRealPath(fileName); System.setProperty(KEYSTORE_KEY, absolutePath);
FileUtils.move(data, backupData); FileUtils.move(index, backupIndex); if (FileUtils.exists(lobs)) { FileUtils.move(lobs, backupLobs); conn.close(); if (deleteOldDb) { FileUtils.delete(backupData); FileUtils.delete(backupIndex); FileUtils.deleteRecursive(backupLobs, false); if (FileUtils.exists(backupData)) { FileUtils.move(backupData, data); if (FileUtils.exists(backupIndex)) { FileUtils.move(backupIndex, index); if (FileUtils.exists(backupLobs)) { FileUtils.move(backupLobs, lobs); FileUtils.delete(name + ".h2.db"); throw DbException.toSQLException(e); } finally { if (script != null) { FileUtils.delete(script);
boolean allFiles = db != null && db.length() == 0; if (allFiles) { list = FileUtils.newDirectoryStream(directory); } else { list = FileLister.getDatabaseFiles(directory, db, true); zipFileName = FileUtils.toRealPath(zipFileName); FileUtils.delete(zipFileName); OutputStream fileOut = null; try { fileOut = FileUtils.newOutputStream(zipFileName, false); try (ZipOutputStream zipOut = new ZipOutputStream(fileOut)) { String base = ""; fileName.endsWith(Constants.SUFFIX_PAGE_FILE) || fileName.endsWith(Constants.SUFFIX_MV_FILE)) { base = FileUtils.getParent(fileName); break; String f = FileUtils.toRealPath(fileName); if (!f.startsWith(base)) { DbException.throwInternalError(f + " does not start with " + base); if (FileUtils.isDirectory(fileName)) { continue; InputStream in = null; try { in = FileUtils.newInputStream(fileName);
/** * Clean up if needed, in a case a compact operation was interrupted due to * killing the process or a power failure. This will delete temporary files * (if any), and in case atomic file replacements were not used, rename the * new file. * * @param fileName the file name */ public static void compactCleanUp(String fileName) { String tempName = fileName + Constants.SUFFIX_MV_STORE_TEMP_FILE; if (FileUtils.exists(tempName)) { FileUtils.delete(tempName); } String newName = fileName + Constants.SUFFIX_MV_STORE_NEW_FILE; if (FileUtils.exists(newName)) { if (FileUtils.exists(fileName)) { FileUtils.delete(newName); } else { FileUtils.move(newName, fileName); } } }
/** * Compress the store by creating a new file and copying the live pages * there. Temporarily, a file with the suffix ".tempFile" is created. This * file is then renamed, replacing the original file, if possible. If not, * the new file is renamed to ".newFile", then the old file is removed, and * the new file is renamed. This might be interrupted, so it's better to * compactCleanUp before opening a store, in case this method was used. * * @param fileName the file name * @param compress whether to compress the data */ public static void compact(String fileName, boolean compress) { String tempName = fileName + Constants.SUFFIX_MV_STORE_TEMP_FILE; FileUtils.delete(tempName); compact(fileName, tempName, compress); try { FileUtils.moveAtomicReplace(tempName, fileName); } catch (DbException e) { String newName = fileName + Constants.SUFFIX_MV_STORE_NEW_FILE; FileUtils.delete(newName); FileUtils.move(tempName, newName); FileUtils.delete(fileName); FileUtils.move(newName, fileName); } }
private void copy(String fileName, boolean quiet) throws IOException { if (FileUtils.isDirectory(fileName)) { return; } String temp = directory + "/temp.db"; try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)){ try(InputStream inStream = new FileChannelInputStream(fileIn, true)) { FileUtils.delete(temp); try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey), true)) { byte[] buffer = new byte[4 * 1024]; long remaining = fileIn.size(); long total = remaining; long time = System.nanoTime(); while (remaining > 0) { if (!quiet && System.nanoTime() - time > TimeUnit.SECONDS.toNanos(1)) { out.println(fileName + ": " + (100 - 100 * remaining / total) + "%"); time = System.nanoTime(); } int len = (int) Math.min(buffer.length, remaining); len = inStream.read(buffer, 0, len); outStream.write(buffer, 0, len); remaining -= len; } } } } FileUtils.delete(fileName); FileUtils.move(temp, fileName); }
private void deleteOldTempFiles() { String path = FileUtils.getParent(databaseName); for (String name : FileUtils.newDirectoryStream(path)) { if (name.endsWith(Constants.SUFFIX_TEMP_FILE) && name.startsWith(databaseName)) { // can't always delete the files, they may still be open FileUtils.tryDelete(name); } } }
return "File not found: " + fileName; long fileLength = FileUtils.size(fileName); MVStore store = new MVStore.Builder(). fileName(fileName). formatTimestamp(FileUtils.lastModified(fileName), fileCreated)); pw.printf("File length: %d\n", fileLength); pw.printf("The last chunk is not listed\n");
if (!FileUtils.isAbsolute(name)) { if (!name.contains("./") && !name.contains(".\\") && if (FileUtils.exists(name + suffix)) { n = FileUtils.toRealPath(name + suffix); } else { suffix = Constants.SUFFIX_MV_FILE; n = FileUtils.toRealPath(name + suffix); String fileName = FileUtils.getName(n); if (fileName.length() < suffix.length() + 1) { throw DbException.get(ErrorCode.INVALID_DATABASE_NAME_1, name);
/** * Open the file and read the header. */ public synchronized void open() { try { metaRootPageId.put(META_TABLE_ID, PAGE_ID_META_ROOT); if (FileUtils.exists(fileName)) { long length = FileUtils.size(fileName); if (length < MIN_PAGE_COUNT * PAGE_SIZE_MIN) { if (database.isReadOnly()) { throw DbException.get( ErrorCode.FILE_CORRUPTED_1, fileName + " length: " + length); } // the database was not fully created openNew(); } else { openExisting(); } } else { openNew(); } } catch (DbException e) { close(); throw e; } }
if (!FileUtils.exists(fileName) || FileUtils.lastModified(fileName) != lastWrite) { save();
private void process(Connection conn, String fileName, boolean continueOnError, Charset charset) throws SQLException, IOException { InputStream in = FileUtils.newInputStream(fileName); String path = FileUtils.getParent(fileName); try { in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE); Reader reader = new InputStreamReader(in, charset); process(conn, continueOnError, path, reader, charset); } finally { IOUtils.closeSilently(in); } }
/** * Save the lock file. * * @return the saved properties */ public Properties save() { try { try (OutputStream out = FileUtils.newOutputStream(fileName, false)) { properties.store(out, MAGIC); } lastWrite = FileUtils.lastModified(fileName); if (trace.isDebugEnabled()) { trace.debug("save " + properties); } return properties; } catch (IOException e) { throw getExceptionFatal("Could not save properties " + fileName, e); } }
return; if (FileUtils.size(fileName) > maxFileSize) { out = new PrintWriter( new OutputStreamWriter( FileUtils.newOutputStream(fileName + ".log", true))); testDatabase(fileName, out); } catch (IOException e) {