/** * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the * {@link File#delete()} method. * * @param path the path to the file or directory that is to be deleted * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise */ public static boolean delete( String path ) { if (path == null || path.trim().length() == 0) return false; return delete(new File(path)); }
/** * Copy the source file system structure into the supplied target location. If the source is a file, the destination will be * created as a file; if the source is a directory, the destination will be created as a directory. * * @param sourceFileOrDirectory the file or directory whose contents are to be copied into the target location * @param destinationFileOrDirectory the location where the copy is to be placed; does not need to exist, but if it does its * type must match that of <code>src</code> * @return the number of files (not directories) that were copied * @throws IllegalArgumentException if the <code>src</code> or <code>dest</code> references are null * @throws IOException */ public static int copy( File sourceFileOrDirectory, File destinationFileOrDirectory ) throws IOException { return copy(sourceFileOrDirectory, destinationFileOrDirectory, null); }
/** * Adds folder to the archive. * * @param path path to the folder * @param srcFolder folder name * @param zip zip archive * @throws IOException */ public static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException { File folder = new File(srcFolder); if (folder.list().length == 0) { addFileToZip(path, srcFolder, zip, true); } else { for (String fileName : folder.list()) { if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false); } } } }
@Test @FixFor( "MODE-2302" ) public void shouldRun4_0_0_Beta3_UpgradeFunction() throws Exception { FileUtil.delete("target/legacy_fs_binarystore"); String config = "config/repo-config-persistent-legacy-fsbinary.json"; // copy the test-resources legacy structure onto the configured one FileUtil.copy(new File("src/test/resources/legacy_fs_binarystore"), new File("target/legacy_fs_binarystore")); // this is coming from the test resources final BinaryKey binaryKey = new BinaryKey("ef2138973a86a8929eebe7bf52419b7cde73ba0a"); // first run is empty, so no upgrades will be performed but we'll decrement the last upgrade ID to force an upgrade next // restart startRunStop(repository -> { changeLastUpgradeId(repository, Upgrades.ModeShape_4_0_0_Beta3.INSTANCE.getId() - 1); FileSystemBinaryStore binaryStore = (FileSystemBinaryStore)repository.runningState().binaryStore(); assertFalse("No used binaries expected", binaryStore.getAllBinaryKeys().iterator().hasNext()); assertFalse("The binary should not be found", binaryStore.hasBinary(binaryKey)); File mainStorageDirectory = binaryStore.getDirectory(); File[] files = mainStorageDirectory.listFiles(); assertEquals("Just the trash directory was expected", 1, files.length); File trash = files[0]; assertTrue(trash.isDirectory()); }, config); // run the repo a second time, which should run the upgrade startRunStop(repository -> { FileSystemBinaryStore binaryStore = (FileSystemBinaryStore)repository.runningState().binaryStore(); assertFalse("No used binaries expected", binaryStore.getAllBinaryKeys().iterator().hasNext()); assertTrue("The binary should be found", binaryStore.hasBinary(binaryKey)); }, config); }
/** * Compresses directory into zip archive. * * @param dirName the path to the directory * @param nameZipFile archive name. * @throws IOException */ public static void zipDir(String dirName, String nameZipFile) throws IOException { try (FileOutputStream fW = new FileOutputStream(nameZipFile); ZipOutputStream zip = new ZipOutputStream(fW)) { addFolderToZip("", dirName, zip); } }
private BinaryKey restoreBinaryFile( File binaryFile ) { if (!binaryFile.exists()) return null; if (!binaryFile.canRead()) { I18n msg = JcrI18n.problemsReadingBinaryFromBackup; BinaryKey key = binaryKeyFor(binaryFile, false); problems.addError(msg, key.toString(), repositoryName(), backupLocation()); } boolean isCompressed = FileUtil.getExtension(binaryFile.getAbsolutePath()).equals(GZIP_EXTENSION); try { InputStream stream = new FileInputStream(binaryFile); if(isCompressed){ stream = new GZIPInputStream(stream); } stream = new BufferedInputStream(stream); try { BinaryValue stored = binaryStore.storeValue(stream, isCompressed); assert stored.getKey().equals(binaryKeyFor(binaryFile, isCompressed)); return stored.getKey(); } finally { stream.close(); } } catch (Exception e) { // We already checked that it exists and is readable, so this shouldn't happen. But ... I18n msg = JcrI18n.problemsReadingBinaryFromBackup; BinaryKey key = binaryKeyFor(binaryFile, isCompressed); problems.addError(e, msg, key.toString(), repositoryName(), backupLocation()); return null; } }
/** * Compresses directory into zip archive. * * @param dirName the path to the directory * @param nameZipFile archive name. * @throws IOException */ public static void zipDir(String dirName, String nameZipFile) throws IOException { ZipOutputStream zip = null; FileOutputStream fW = null; fW = new FileOutputStream(nameZipFile); zip = new ZipOutputStream(fW); addFolderToZip("", dirName, zip); zip.close(); fW.close(); }
private BinaryKey restoreBinaryFile( File binaryFile ) { if (!binaryFile.exists()) return null; if (!binaryFile.canRead()) { I18n msg = JcrI18n.problemsReadingBinaryFromBackup; BinaryKey key = binaryKeyFor(binaryFile, false); problems.addError(msg, key.toString(), repositoryName(), backupLocation()); } boolean isCompressed = FileUtil.getExtension(binaryFile.getAbsolutePath()).equals(GZIP_EXTENSION); try { InputStream stream = new FileInputStream(binaryFile); if(isCompressed){ stream = new GZIPInputStream(stream); } stream = new BufferedInputStream(stream); try { BinaryValue stored = binaryStore.storeValue(stream, isCompressed); assert stored.getKey().equals(binaryKeyFor(binaryFile, isCompressed)); return stored.getKey(); } finally { stream.close(); } } catch (Exception e) { // We already checked that it exists and is readable, so this shouldn't happen. But ... I18n msg = JcrI18n.problemsReadingBinaryFromBackup; BinaryKey key = binaryKeyFor(binaryFile, isCompressed); problems.addError(e, msg, key.toString(), repositoryName(), backupLocation()); return null; } }
/** * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the * {@link File#delete()} method. * * @param path the path to the file or directory that is to be deleted * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise */ public static boolean delete( String path ) { if (path == null || path.trim().length() == 0) return false; return delete(new File(path)); }
/** * Copy the source file system structure into the supplied target location. If the source is a file, the destination will be * created as a file; if the source is a directory, the destination will be created as a directory. * * @param sourceFileOrDirectory the file or directory whose contents are to be copied into the target location * @param destinationFileOrDirectory the location where the copy is to be placed; does not need to exist, but if it does its * type must match that of <code>src</code> * @return the number of files (not directories) that were copied * @throws IllegalArgumentException if the <code>src</code> or <code>dest</code> references are null * @throws IOException */ public static int copy( File sourceFileOrDirectory, File destinationFileOrDirectory ) throws IOException { return copy(sourceFileOrDirectory, destinationFileOrDirectory, null); }
/** * Compresses directory into zip archive. * * @param dirName the path to the directory * @param nameZipFile archive name. * @throws IOException */ public static void zipDir(String dirName, String nameZipFile) throws IOException { ZipOutputStream zip = null; FileOutputStream fW = null; fW = new FileOutputStream(nameZipFile); zip = new ZipOutputStream(fW); addFolderToZip("", dirName, zip); zip.close(); fW.close(); }
/** * Adds folder to the archive. * * @param path path to the folder * @param srcFolder folder name * @param zip zip archive * @throws IOException */ public static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException { File folder = new File(srcFolder); if (folder.list().length == 0) { addFileToZip(path, srcFolder, zip, true); } else { for (String fileName : folder.list()) { if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false); } } } }
/** * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the * {@link File#delete()} method. * * @param path the path to the file or directory that is to be deleted * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise */ public static boolean delete( String path ) { if (path == null || path.trim().length() == 0) return false; return delete(new File(path)); }
/** * Copy the source file system structure into the supplied target location. If the source is a file, the destination will be * created as a file; if the source is a directory, the destination will be created as a directory. * * @param sourceFileOrDirectory the file or directory whose contents are to be copied into the target location * @param destinationFileOrDirectory the location where the copy is to be placed; does not need to exist, but if it does its * type must match that of <code>src</code> * @return the number of files (not directories) that were copied * @throws IllegalArgumentException if the <code>src</code> or <code>dest</code> references are null * @throws IOException */ public static int copy( File sourceFileOrDirectory, File destinationFileOrDirectory ) throws IOException { return copy(sourceFileOrDirectory, destinationFileOrDirectory, null); }
/** * Appends file to the archive. * * @param path path to the file * @param srcFile file name * @param zip archive * @param flag * @throws IOException */ public static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws IOException { File folder = new File(srcFile); if (flag) { zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/")); } else { if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } } }
/** * Adds folder to the archive. * * @param path path to the folder * @param srcFolder folder name * @param zip zip archive * @throws IOException */ public static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException { File folder = new File(srcFolder); if (folder.list().length == 0) { addFileToZip(path, srcFolder, zip, true); } else { for (String fileName : folder.list()) { if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false); } } } }
/** * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike * the {@link File#delete()} method. * * @param fileOrDirectory the reference to the Java File object that is to be deleted * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise */ public static boolean delete( File fileOrDirectory ) { if (fileOrDirectory == null) return false; if (!fileOrDirectory.exists()) return false; // The file/directory exists, so if a directory delete all of the contents ... if (fileOrDirectory.isDirectory()) { File[] files = fileOrDirectory.listFiles(); if (files != null) { for (File childFile : files) { delete(childFile); // recursive call (good enough for now until we need something better) } } // Now an empty directory ... } // Whether this is a file or empty directory, just delete it ... return fileOrDirectory.delete(); }
String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i]; numberOfFilesCopied += copy(new File(src1), new File(dest1), exclusionFilter);
/** * Appends file to the archive. * * @param path path to the file * @param srcFile file name * @param zip archive * @param flag * @throws IOException */ public static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws IOException { File folder = new File(srcFile); if (flag) { zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/")); } else { if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } } }
/** * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike * the {@link File#delete()} method. * * @param fileOrDirectory the reference to the Java File object that is to be deleted * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise */ public static boolean delete( File fileOrDirectory ) { if (fileOrDirectory == null) return false; if (!fileOrDirectory.exists()) return false; // The file/directory exists, so if a directory delete all of the contents ... if (fileOrDirectory.isDirectory()) { File[] files = fileOrDirectory.listFiles(); if (files != null) { for (File childFile : files) { delete(childFile); // recursive call (good enough for now until we need something better) } } // Now an empty directory ... } // Whether this is a file or empty directory, just delete it ... return fileOrDirectory.delete(); }