/** * Gets the value from <code>file</code> that should be used to replace <code>keyword</code>. * @param keyword character to replace. * @param file file from which to retrieve the replacement value. * @return the requested replacement value. */ private static String getKeywordReplacement(char keyword, AbstractFile file) { switch(keyword) { case KEYWORD_PATH: return file.getAbsolutePath(); case KEYWORD_NAME: return file.getName(); case KEYWORD_PARENT: AbstractFile parentFile = file.getParent(); return parentFile==null?"":parentFile.getAbsolutePath(); case KEYWORD_VM_PATH: return new File(System.getProperty("user.dir")).getAbsolutePath(); case KEYWORD_EXTENSION: String extension; if((extension = file.getExtension()) == null) return ""; return extension; case KEYWORD_NAME_WITHOUT_EXTENSION: return file.getNameWithoutExtension(); } throw new IllegalArgumentException(); }
/** * Returns a checksum of this file (also referred to as <i>hash</i> or <i>digest</i>) calculated by reading this * file's contents and feeding the bytes to the given <code>MessageDigest</code>, until EOF is reached. * * <p>The checksum is returned as an hexadecimal string, such as "6d75636f0a". The length of this string depends on * the kind of <code>MessageDigest</code>.</p> * * <p>Note: this method does not reset the <code>MessageDigest</code> after the checksum has been calculated.</p> * * @param messageDigest the MessageDigest to use for calculating the checksum * @return this file's checksum, as an hexadecimal string * @throws IOException if an I/O error occurred while calculating the checksum * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public final String calculateChecksum(MessageDigest messageDigest) throws IOException, UnsupportedFileOperationException { InputStream in = getInputStream(); try { return calculateChecksum(in, messageDigest); } finally { in.close(); } }
/** * Sets the path to the configuration file. * @param file path to the file that should be used for configuration storage. * @throws FileNotFoundException if the specified file is not a valid file. */ private synchronized void setConfigurationFile(AbstractFile file) throws FileNotFoundException { // Makes sure file can be used as a configuration. if(file.isBrowsable()) throw new FileNotFoundException("Not a valid file: " + file.getAbsolutePath()); configurationFile = file; }
/** * Returns the canonical path to this file, resolving any symbolic links or '..' and '.' occurrences. * A separator character will be appended to the returned path if <code>true</code> is passed. * * @param appendSeparator if true, a separator will be appended to the returned path * @return the canonical path to this file */ public final String getCanonicalPath(boolean appendSeparator) { String path = getCanonicalPath(); return appendSeparator?addTrailingSeparator(path): removeTrailingSeparator(path); }
/** * Returns the absolute path to this file. * A separator character will be appended to the returned path if <code>true</code> is passed. * * @param appendSeparator if true, a separator will be appended to the returned path * @return the absolute path to this file */ public final String getAbsolutePath(boolean appendSeparator) { String path = getAbsolutePath(); return appendSeparator?addTrailingSeparator(path): removeTrailingSeparator(path); }
@Override protected boolean hasFolderChanged(AbstractFile folder) { // This job modifies the folder where the archive is return folder.equalsCanonical(destFile.getParent()); // Note: parent may be null }
String originalName = file.getName(); String destFileName; if(isFileInBaseFolder && newName!=null) if(!append && file.getURL().schemeEquals(destFile.getURL()) && file.isFileOperationSupported(FileOperation.RENAME)) { try { file.renameTo(destFile); return true; if(file.isDirectory()) { if(!(destFile.exists() && destFile.isDirectory())) { do { // Loop for retry try { destFile.mkdir(); int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_create_folder", destFile.getAbsolutePath())); AbstractFile subFiles[] = file.ls(); boolean isFolderEmpty = true; for (AbstractFile subFile : subFiles) { if(destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.changeDate(file.getDate()); int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_read_folder", file.getName())); file.delete();
String destFileName = isFileInBaseFolder && newName!=null ? newName : file.getName(); if (file.isSymlink() && ( !file.hasAncestor(LocalFile.class) || !destFile.hasAncestor(LocalFile.class))) { return true; return false; if (file.isSymlink()) { return tryCopySymlinkFile(file, destFile); if (file.isDirectory()) { if (!destFile.exists() || !destFile.isDirectory()) { destFile.mkdir(); for (AbstractFile subFile : file.ls()) { if (getState() == FileJobState.INTERRUPTED) break; if (destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.changeDate(file.getDate()); int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_read_folder", file.getName()));
@Override protected boolean processFile(AbstractFile file, Object recurseParams) { if(file.isDirectory()) { do { // Loop for retry try { AbstractFile children[] = file.ls(); for(int i=0; i<children.length && getState() != FileJobState.INTERRUPTED; i++) { int ret = showErrorDialog(Translator.get("error"), Translator.get("cannot_read_folder", file.getName())); in = setCurrentInputStream(file.getInputStream()); String relativePath = file.getAbsolutePath(); relativePath = relativePath.substring(baseSourcePath.length(), relativePath.length()); checksum = AbstractFile.calculateChecksum(in, digest); if(useSfvFormat) { int ret = showErrorDialog(Translator.get("error"), Translator.get("error_while_transferring", file.getAbsolutePath()));
public void testIsDirectory() throws IOException { FileURL tempFileURLB = (FileURL)tempFile.getURL().clone(); tempFileURLB.setPath(tempFile.addTrailingSeparator(tempFileURLB.getPath())); AbstractFile tempFileB = FileFactory.getFile(tempFileURLB, true); assert !tempFile.exists(); assert !tempFile.isDirectory(); assert !tempFileB.exists(); assert !tempFileB.isDirectory(); tempFile.mkdir(); assert tempFile.exists(); assert tempFile.isDirectory(); assert tempFileB.exists(); assert tempFileB.isDirectory(); tempFile.delete(); assert !tempFile.exists(); assert !tempFile.isDirectory(); assert !tempFileB.exists(); assert !tempFileB.isDirectory(); tempFile.mkfile(); assert tempFile.exists(); assert !tempFile.isDirectory(); assert tempFile.exists(); assert !tempFileB.isDirectory();
return false; if(recurseOnDirectories && file.isDirectory()) { do { // Loop for retries try { AbstractFile children[] = file.ls(); int nbChildren = children.length; int ret = showErrorDialog("", Translator.get("cannot_read_folder", file.getName())); if(!file.isFileOperationSupported(FileOperation.CHANGE_PERMISSION)) return false; file.changePermissions(permissions); return true; if(!file.isFileOperationSupported(FileOperation.CHANGE_DATE)) return false; file.changeDate(date); return true;
String crcFileName = sourceFile.getName() + ".sfv"; try { String sourceChecksum; if (recalculateCRC ) { origFileStream = sourceFile.getInputStream(); sourceChecksum = AbstractFile.calculateChecksum(origFileStream, MessageDigest.getInstance("CRC32")); origFileStream.close(); } else { sourceChecksum = ((ChecksumInputStream)origFileStream).getChecksumString(); AbstractFile crcFile = baseDestFolder.getDirectChild(crcFileName); OutputStream crcStream = crcFile.getOutputStream(); String line = sourceFile.getName() + " " + sourceChecksum; crcStream.write(line.getBytes("utf-8")); crcStream.close();
AbstractFile destFile = createDestinationFile(baseDestFolder, file.getName()); if (destFile == null) return false; out = destFile.getOutputStream(); if(destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.changeDate(sourceFile.getDate()); if(destFile.isFileOperationSupported(FileOperation.CHANGE_PERMISSION)) { try { destFile.importPermissions(sourceFile, FilePermissions.DEFAULT_FILE_PERMISSIONS); Translator.get("error_while_transferring", destFile.getName()), new String[]{FileJobAction.CANCEL_TEXT}, new int[]{FileJobAction.CANCEL}
@Override protected void saveAs(AbstractFile destFile) throws IOException { OutputStream out = null; try { out = destFile.getOutputStream(); write(out); } finally { if(out != null) { try {out.close();} catch(IOException e) { // Ignored } } } // We get here only if the destination file was updated successfully // so we can set that no further save is needed at this stage setSaveNeeded(false); // Change the parent folder's date to now, so that changes are picked up by folder auto-refresh (see ticket #258) if(destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.getParent().changeDate(System.currentTimeMillis()); } catch (IOException e) { LOGGER.debug("failed to change the date of "+destFile, e); // Fail silently } } }
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { // get file name and create default component (JLabel) to display it AbstractFile file = (AbstractFile) value; String name = file.isRoot()?file.getAbsolutePath():file.getName(); super.getTreeCellRendererComponent(tree, name, sel, expanded, leaf, row, hasFocus); setIcon(model.getCurrentIcon(file)); return this; }
/** * Looks for XML files in the specified folder recursively and tests them for well-formedness. * A <code>org.xml.sax.SAXException</code> is thrown when an error is encountered in one of the XML files, * even a recoverable one. Any archive contained in the folder will be searched for XML files. * * @param folder the folder in which to look for XML files recursively. * @throws IOException if a file or a folder couldn't be accessed * @throws SAXException if an error was found in one of the XML documents * @throws ParserConfigurationException if 'a serious configuration error' occurred in the XML parser */ private void testXMLFiles(AbstractFile folder) throws SAXException, IOException, ParserConfigurationException { AbstractFile children[] = folder.ls(); for (AbstractFile bhild : children) { if (bhild.isBrowsable()) testXMLFiles(bhild); else if ("xml".equals(bhild.getExtension())) testXMLDocument(bhild); } }
@Override protected AbstractFile[] getFiles(AbstractFile directory) throws IOException { return directory.ls(); } }
/** * Adds the given file to the FileSet if it's not a folder, recurses otherwise. */ private void recurseOnFolder(AbstractFile file, FileSet flattenedFiles) throws IOException { if(file.isDirectory() && !file.isSymlink()) { AbstractFile children[] = file.ls(); for (AbstractFile child : children) recurseOnFolder(child, flattenedFiles); } else { flattenedFiles.add(file); } }
/** * Resolves an AbstractFile instance corresponding to the file named <code>filename</code> within the temporary * folder and asserts its {@link AbstractFile#getName() name}, {@link AbstractFile#getExtension() extension} and * {@link AbstractFile#getNameWithoutExtension() name without extension} match the specified values. * * @param tempFolder the temporary folder which will be the parent of the resolved AbstractFile instance * @param filename filename of the AbstractFile to resolved * @param expectedExtension the expected file's extension * @param expectedNameWOExt the expected file's name without extension * @throws IOException if an error occurred while resolving the file */ protected void assertNameAndExtension(AbstractFile tempFolder, String filename, String expectedExtension, String expectedNameWOExt) throws IOException { AbstractFile file = tempFolder.getChild(filename); assert filename.equals(file.getName()); assert StringUtils.equals(expectedExtension, file.getExtension(), true); assert StringUtils.equals(expectedNameWOExt, expectedNameWOExt, true); }
private void tryCopyFileDate(AbstractFile sourceFile, AbstractFile destFile) { if(destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.changeDate(sourceFile.getDate()); } catch (IOException e) { LOGGER.debug("failed to change the date of "+destFile, e); // Fail silently } } }