/* returns null if consistent, error string when not */ public String checkConsistency() { final Collection<String> uploadSiteNames = getSiteNamesToUpload(); final String uploadSiteName = uploadSiteNames.isEmpty() ? null : uploadSiteNames.iterator().next(); final StringBuilder result = new StringBuilder(); final Set<FileObject> circularChecked = new HashSet<FileObject>(); for (final FileObject file : this) { if (uploadSiteName != null && !uploadSiteName.equals(file.updateSite) || file.getAction() == Action.REMOVE) { continue; } result.append(checkForCircularDependency(file, circularChecked, uploadSiteName)); // only non-obsolete components can have dependencies final Set<String> deps = file.dependencies.keySet(); if (deps.size() > 0 && file.isObsolete() && file.getAction() != Action.UPLOAD) { result.append( "Obsolete file " + file + " has dependencies: " + UpdaterUtil.join(", ", deps) + "!\n"); } for (final String dependency : deps) { final FileObject dep = get(dependency); if (dep == null || dep.current == null) result.append( "The file " + file + " has the obsolete/local-only " + "dependency " + dependency + "!\n"); } } return result.length() > 0 ? result.toString() : null; }
public void testPreferencesEvents() throws Exception { storage.toFolder(true); FileUtil.getConfigRoot().addRecursiveListener(new FileListener()); pref.addNodeChangeListener(new NodeListener()); String newPath = "a/b/c"; String[] paths = newPath.split("/"); FileObject fo = null; FileObject fo0 = null; for (int i = 0; i < paths.length; i++) { String path = paths[i]; fo = FileUtil.createFolder((fo == null) ? FileUtil.getConfigRoot() : fo, path); if (i == 0) { fo0 = fo; } nodeAddedEvent.await(); assertEquals("Missing node added event", 0, nodeAddedEvent.getCount()); nodeAddedEvent = new CountDownLatch(1); Preferences pref2 = pref.node(fo.getPath()); pref2.addNodeChangeListener(new NodeListener()); } FileObject fo1 = FileUtil.createData(fo, "a.properties"); nodeAddedEvent.await(); assertEquals("Missing node added event", 0, nodeAddedEvent.getCount()); nodeRemovedEvent = new CountDownLatch(paths.length + 1); fo0.delete(); nodeRemovedEvent.await(); assertEquals("Missing node removed event", 0, nodeRemovedEvent.getCount()); }
@Internal("file.copy") public static LogicalVector fileCopy( @Current Context context, StringVector fromFiles, String to, boolean overwrite, final boolean recursive) throws FileSystemException { LogicalArrayVector.Builder result = new LogicalArrayVector.Builder(); FileObject toFile = context.resolveFile(to); for (String from : fromFiles) { try { toFile.copyFrom( context.resolveFile(from), new FileSelector() { @Override public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception { return true; } @Override public boolean includeFile(FileSelectInfo fileInfo) throws Exception { return recursive; } }); result.add(true); } catch (FileSystemException e) { result.add(false); } } return result.build(); }
/** * ‘file.append’ attempts to append the files named by its second argument to those named by its * first. The R subscript recycling rule is used to align names given in vectors of different * lengths. */ @Internal("file.append") @DataParallel public static boolean fileAppend( @Current Context context, String destFileName, String sourceFileName) { try { FileObject sourceFile = context.resolveFile(sourceFileName); if (!sourceFile.exists()) { return false; } FileObject destFile = context.resolveFile(destFileName); OutputStream out = destFile.getContent().getOutputStream(true); try { InputStream in = sourceFile.getContent().getInputStream(); try { ByteStreams.copy(in, out); } finally { try { in.close(); } catch (Exception ignored) { } } } finally { try { out.close(); } catch (Exception ignored) { } } return true; } catch (Exception e) { return false; } }
/** * Gets all children of the parent, may return empty array. * * @param parent the parent * @return table of children */ public FileObject[] getChildren(FileObject parent) { if (parent == null) { return new FileObject[0]; } FileObject parentObject = parent; if (!parentObject.isDirectory()) { return new FileObject[0]; } String parentPath = normalizeDirPath(parentObject.getPath()); String[] fileNames; try { fileNames = fileSystem.list(parentPath); } catch (IOException e) { throw new RuntimeException(e); } if (fileNames == null) { return new FileObject[0]; } FileObject[] files = new FileObject[fileNames.length]; for (int i = 0; i < fileNames.length; i++) { String filePath = parentPath + '/' + fileNames[i]; files[i] = new FileObject(fileSystem, filePath); } return files; }
private void markForUpdate(final String updateSite, final boolean evenForcedUpdates) { for (final FileObject file : forUpdateSite(updateSite)) { if ((file.isUpdateable(evenForcedUpdates) || file.getStatus().isValid(Action.INSTALL)) && file.isUpdateablePlatform(this)) { file.setFirstValidAction(this, Action.UPDATE, Action.UNINSTALL, Action.INSTALL); } } }
/** Changes directly the filesystem attribute. */ protected void doSetPreferredLoader(FileObject fo, DataLoader loader) throws IOException { if (loader == null) { fo.setAttribute("NetBeansAttrAssignedLoader", null); } else { Class c = loader.getClass(); fo.setAttribute("NetBeansAttrAssignedLoader", c.getName()); } }
/** * Utility method for Fiji's Bug Submitter * * @return the list of files known to the Updater, with versions, as a String */ public static String getInstalledVersions(final File ijDirectory, final Progress progress) { final StringBuilder sb = new StringBuilder(); final FilesCollection files = new FilesCollection(ijDirectory); try { files.read(); } catch (Exception e) { sb.append("Error while reading db.xml.gz: ").append(e.getMessage()).append("\n\n"); } final Checksummer checksummer = new Checksummer(files, progress); try { checksummer.updateFromLocal(); } catch (UpdateCanceledException t) { return null; } final Map<String, FileObject.Version> checksums = checksummer.getCachedChecksums(); sb.append("Activated update sites:\n"); for (final UpdateSite site : files.getUpdateSites(false)) { sb.append(site.getName()) .append(": ") .append(site.getURL()) .append(" (last check:") .append(site.getTimestamp()) .append(")\n"); } boolean notUpToDateShown = false; for (final Map.Entry<String, FileObject.Version> entry : checksums.entrySet()) { String file = entry.getKey(); if (file.startsWith(":") && file.length() == 41) continue; final FileObject fileObject = files.get(file); if (fileObject != null && fileObject.getStatus() == Status.INSTALLED) continue; if (!notUpToDateShown) { sb.append("\nFiles not up-to-date:\n"); notUpToDateShown = true; } final FileObject.Version version = entry.getValue(); String checksum = version.checksum; if (version.checksum != null && version.checksum.length() > 8) { final StringBuilder rebuild = new StringBuilder(); for (final String element : checksum.split(":")) { if (rebuild.length() > 0) rebuild.append(":"); if (element == null || element.length() <= 8) rebuild.append(element); else rebuild.append(element.substring(0, 8)); } checksum = rebuild.toString(); } sb.append(" ").append(checksum).append(" "); if (fileObject != null) sb.append("(").append(fileObject.getStatus()).append(") "); sb.append(version.timestamp).append(" "); sb.append(file).append("\n"); } return sb.toString(); }
/** * Creates the last element of the path, unless recursive = TRUE. Trailing path separators are * removed. * * @param context the current call Context * @param path the path * @param showWarnings should the warnings on failure be shown? * @param recursive Should elements of the path other than the last be created? If true, like * Unix's mkdir -p * @param mode the file mode to be used on Unix-alikes: it will be coerced by as.octmode. * (currently ignored by renjin) * @return true if the operation succeeded for each of the files attempted. Using a missing value * for a path name will always be regarded as a failure. returns false if the directory * already exists * @throws FileSystemException */ @Internal("dir.create") public static SEXP dirCreate( @Current Context context, String path, boolean showWarnings, boolean recursive, int mode) throws FileSystemException { FileObject dir = context.resolveFile(path); dir.createFolder(); // TODO: return correct value and implement warnings documented above context.setInvisibleFlag(); return new LogicalArrayVector(true); }
/** * Deactivates the given update site. * * @param site the site to deactivate * @return the number of files marked for update/install/uninstall */ public int deactivateUpdateSite(final UpdateSite site) { if (!site.isActive()) return 0; final List<FileObject> list = new ArrayList<FileObject>(); final String updateSite = site.getName(); for (final FileObject file : forUpdateSite(updateSite)) { list.add(file); } for (final FileObject file : list) { file.removeFromUpdateSite(updateSite, this); } site.setActive(false); return list.size(); }
public void removeUpdateSite(final String name) { for (final FileObject file : clone(forUpdateSite(name))) { file.removeFromUpdateSite(name, this); } updateSites.remove(name); setUpdateSitesChanged(true); // update rank int counter = 1; for (final Map.Entry<String, UpdateSite> entry : updateSites.entrySet()) { entry.getValue().rank = counter++; } }
/** Set up given number of FileObjects */ protected FileObject[] setUpFileObjects(int foCount) throws Exception { tmp = createTempFolder(); destFolder = LocalFSTest.createFiles(foCount, 0, tmp); File xmlbase = generateXMLFile( destFolder, new ResourceComposer(LocalFSTest.RES_NAME, LocalFSTest.RES_EXT, foCount, 0)); xmlfs = new XMLFileSystem(); xmlfs.setXmlUrl(xmlbase.toURL(), false); FileObject pkg = xmlfs.findResource(PACKAGE); return pkg.getChildren(); }
/** * Take specified action to either move or delete the processed file, depending on the outcome * * @param entry the PollTableEntry for the file that has been processed * @param fileObject the FileObject representing the file to be moved or deleted */ private void moveOrDeleteAfterProcessing(final PollTableEntry entry, FileObject fileObject) throws AxisFault { String moveToDirectoryURI = null; try { switch (entry.getLastPollState()) { case PollTableEntry.SUCCSESSFUL: if (entry.getActionAfterProcess() == PollTableEntry.MOVE) { moveToDirectoryURI = entry.getMoveAfterProcess(); } break; case PollTableEntry.FAILED: if (entry.getActionAfterFailure() == PollTableEntry.MOVE) { moveToDirectoryURI = entry.getMoveAfterFailure(); } break; default: return; } if (moveToDirectoryURI != null) { FileObject moveToDirectory = fsManager.resolveFile(moveToDirectoryURI); String prefix; if (entry.getMoveTimestampFormat() != null) { prefix = entry.getMoveTimestampFormat().format(new Date()); } else { prefix = ""; } FileObject dest = moveToDirectory.resolveFile(prefix + fileObject.getName().getBaseName()); if (log.isDebugEnabled()) { log.debug("Moving to file :" + dest.getName().getURI()); } try { fileObject.moveTo(dest); } catch (FileSystemException e) { handleException("Error moving file : " + fileObject + " to " + moveToDirectoryURI, e); } } else { try { if (log.isDebugEnabled()) { log.debug("Deleting file :" + fileObject); } fileObject.close(); if (!fileObject.delete()) { String msg = "Cannot delete file : " + fileObject; log.error(msg); throw new AxisFault(msg); } } catch (FileSystemException e) { log.error("Error deleting file : " + fileObject, e); } } } catch (FileSystemException e) { handleException( "Error resolving directory to move after processing : " + moveToDirectoryURI, e); } }
@Invisible @Internal public static String setwd(@Current Context context, String workingDirectoryName) throws FileSystemException { FileObject newWorkingDirectory = context.resolveFile(workingDirectoryName); if (!newWorkingDirectory.exists() || newWorkingDirectory.getType() != FileType.FOLDER) { throw new EvalException("cannot change working directory"); } String previous = context.getSession().getWorkingDirectory().getName().getURI(); context.getSession().setWorkingDirectory(newWorkingDirectory); return previous; }
private synchronized void reTryFailedMove(PollTableEntry entry, FileObject fileObject) throws AxisFault { try { String moveToDirectoryURI = entry.getMoveAfterMoveFailure(); FileObject moveToDirectory = fsManager.resolveFile(moveToDirectoryURI); if (!moveToDirectory.exists()) { moveToDirectory.createFolder(); } String prefix; if (entry.getMoveTimestampFormat() != null) { prefix = entry.getMoveTimestampFormat().format(new Date()); } else { prefix = ""; } FileObject dest = moveToDirectory.resolveFile(prefix + fileObject.getName().getBaseName()); if (log.isDebugEnabled()) { log.debug("The failed file is moving to :" + dest.getName().getURI()); } try { fileObject.moveTo( dest); // FIXME - when an exception occurs here it causes the in folder to vanish } catch (FileSystemException e) { handleException( "Error moving the failed file : " + fileObject + " to " + moveToDirectoryURI, e); } } catch (FileSystemException e) { handleException("Cloud not move the failed file object '" + fileObject + "'", e); } catch (IOException e) { handleException("Cloud not create the folder", e); } }
@Override protected FileSystem createFS(String... resources) throws IOException { for (String s : resources) { FileObject fo = FileUtil.getConfigFile(s.replaceAll("/.*", "")); if (fo != null) { fo.delete(); } } FileSystem sfs = FileUtil.getConfigRoot().getFileSystem(); for (String s : resources) { assertNotNull("creating: " + s, FileUtil.createData(sfs.getRoot(), s)); } return sfs; }
/** * The DicomEventListener implementation. Listen for an event from the SCP, log the file, and move * it to the dicom-import directory for anonymization and export. * * @param event the event identifying the file that was received. */ public void dicomEventOccurred(DicomEvent event) { if ((event.getStatus() == 0) && event.serviceAsString(event.getService()).equals("C_STORE_RQ")) { File inFile = new File(event.getFilename()); Log.message(dicomImportServiceName + ": Image received: " + inFile.getName()); // Make the output directory in case it doesn't exist. File outDir = new File(TrialConfig.basepath + TrialConfig.dicomImportDir); outDir.mkdirs(); // Put the new file in it, using the overwrite attribute of the trial to determine // whether duplicate SOPInstanceUIDs are to be renamed so as not to lose them. FileObject fileObject = new FileObject(inFile); fileObject.moveToDirectory(outDir, TrialConfig.allowOverwrite()); } else if (event.getStatus() != 0xff00) Log.message(dicomImportServiceName + ": unexpected status: " + event.toStringNoPath()); }
public void okPressed() { if (fileDialogMode == VFS_DIALOG_SAVEAS && "".equals(fileNameText.getText())) { // $NON-NLS-1$ // do nothing, user did not enter a file name for saving MessageBox messageDialog = new MessageBox(dialog, SWT.OK); messageDialog.setText(Messages.getString("VfsFileChooserDialog.error")); // $NON-NLS-1$ messageDialog.setMessage( Messages.getString("VfsFileChooserDialog.noFilenameEntered")); // $NON-NLS-1$ messageDialog.open(); return; } if (fileDialogMode == VFS_DIALOG_SAVEAS) { try { FileObject toBeSavedFile = vfsBrowser.getSelectedFileObject().resolveFile(fileNameText.getText()); if (toBeSavedFile.exists()) { MessageBox messageDialog = new MessageBox(dialog, SWT.YES | SWT.NO); messageDialog.setText( Messages.getString("VfsFileChooserDialog.fileExists")); // $NON-NLS-1$ messageDialog.setMessage( Messages.getString("VfsFileChooserDialog.fileExistsOverwrite")); // $NON-NLS-1$ int flag = messageDialog.open(); if (flag == SWT.NO) { return; } } } catch (FileSystemException e) { e.printStackTrace(); } } if (fileDialogMode == VFS_DIALOG_SAVEAS) { enteredFileName = fileNameText.getText(); } try { if (fileDialogMode == VFS_DIALOG_OPEN_FILE && vfsBrowser.getSelectedFileObject().getType().equals(FileType.FOLDER)) { // try to open this node, it is a directory vfsBrowser.selectTreeItemByFileObject(vfsBrowser.getSelectedFileObject(), true); return; } } catch (FileSystemException e) { } okPressed = true; hideCustomPanelChildren(); dialog.dispose(); }
// Look through the http-import directory and process all // the files there, oldest first. Note that these are actual // files, not queue elements. private void processHttpImportFiles() { File importDirFile = new File(TrialConfig.basepath + TrialConfig.httpImportDir); if (!importDirFile.exists()) return; File[] files = importDirFile.listFiles(); for (int k = 0; k < files.length; k++) { File next = files[k]; if (next.canRead() && next.canWrite()) { FileObject fileObject = FileObject.getObject(next); if (preprocess(fileObject)) { process(fileObject); if (!queueForDatabase(fileObject)) Log.message(Quarantine.file(next, processorServiceName)); else Log.message(processorServiceName + ": Processing complete: " + next.getName()); } // If the file still exists, then there must be a bug // somewhere; log it and quarantine the file so we don't // fall into an infinite loop. if (next.exists()) { logger.warn( "File still in queue after processing:\n" + next + "The file will be quarantined."); Log.message(Quarantine.file(next, processorServiceName)); } Thread.currentThread().yield(); } } }
public void testJarFileRootObject() throws IOException { FileSystemManager fs = new FileSystemManager(); URL urlFile = loader.getResource("org/apache/commons/lang/StringUtils.class"); String urlString = urlFile.toString(); int index = urlString.indexOf("!/"); assertTrue(index > 0); String root = urlString.substring(0, index + 2); FileObject rootObject = fs.resolveFile(root); assertTrue(root, rootObject.exists()); assertTrue(rootObject.getChildren().length > 0); assertTrue(rootObject.getChild("org/").exists()); assertTrue(rootObject.getChild("org/") == fs.resolveFile(root + "org/")); }
private synchronized void addFailedRecord( PollTableEntry pollTableEntry, FileObject failedObject, String timeString) { try { String record = failedObject.getName().getBaseName() + VFSConstants.FAILED_RECORD_DELIMITER + timeString; String recordFile = pollTableEntry.getFailedRecordFileDestination() + pollTableEntry.getFailedRecordFileName(); File failedRecordFile = new File(recordFile); if (!failedRecordFile.exists()) { FileUtils.writeStringToFile(failedRecordFile, record); if (log.isDebugEnabled()) { log.debug("Added fail record '" + record + "' into the record file '" + recordFile + "'"); } } else { List<String> content = FileUtils.readLines(failedRecordFile); if (!content.contains(record)) { content.add(record); } FileUtils.writeLines(failedRecordFile, content); } } catch (IOException e) { log.fatal("Failure while writing the failed records!", e); } }
public void setSelectedFile(FileObject selectedFile) { this.selectedFile = selectedFile; if (selectedFile != null) { this.openFileCombo.setText(selectedFile.getName().getURI()); resolveVfsBrowser(); } }
public String getURL(final FileObject file) { final String siteName = file.updateSite; assert (siteName != null && !siteName.equals("")); final UpdateSite site = getUpdateSite(siteName, false); if (site == null) return null; return site.getURL() + file.filename.replace(" ", "%20") + "-" + file.getTimestamp(); }
// Queue a file for the DatabaseExportService. private boolean queueForDatabase(FileObject fileObject) { if (TrialConfig.getDatabaseExportMode().equals("auto")) { File databaseExportDirectoryFile = TrialConfig.getDatabaseExportDirectoryFile(); try { ExportQueueElement eqe = ExportQueueElement.createEQE(fileObject); eqe.queue(databaseExportDirectoryFile); } catch (Exception e) { Log.message( processorServiceName + ": Unable to queue " + fileObject.getFile().getName() + " for database export"); logger.warn("Unable to queue " + fileObject.getFile().getName() + " for database export"); } } return true; }
public void renameUpdateSite(final String oldName, final String newName) { if (getUpdateSite(newName, true) != null) throw new RuntimeException("Update site " + newName + " exists already!"); if (getUpdateSite(oldName, true) == null) throw new RuntimeException("Update site " + oldName + " does not exist!"); // handle all files for (final FileObject file : this) if (oldName.equals(file.updateSite)) file.updateSite = newName; // preserve order final Map<String, UpdateSite> oldMap = updateSites; updateSites = new LinkedHashMap<String, UpdateSite>(); for (final String name : oldMap.keySet()) { final UpdateSite site = oldMap.get(name); if (name.equals(oldName)) site.setName(newName); addUpdateSite(site.getName(), site); } }
/** * Gets the type or storage mode of an object. * * @return unix-style file mode integer */ private static int mode(FileObject file) throws FileSystemException { int access = 0; if (file.isReadable()) { access += 4; } if (file.isWriteable()) { access += 2; } if (file.getType() == FileType.FOLDER) { access += 1; } // i know this is braindead but i can't be bothered // to do octal math at the moment String digit = Integer.toString(access); String octalString = digit + digit + digit; return Integer.parseInt(octalString, 8); }
private static int checkAccess(FileObject file, int mode) throws FileSystemException { boolean ok = true; if ((mode & CHECK_ACCESS_EXISTENCE) != 0 && !file.exists()) { ok = false; } if ((mode & CHECK_ACCESS_READ) != 0 && !file.isReadable()) { ok = false; } if ((mode & CHECK_ACCESS_WRITE) != 0 & !file.isWriteable()) { ok = false; } // case CHECK_ACCESS_EXECUTE: // return -1; // don't know if this is possible to check with VFS // } return ok ? 0 : -1; }
private FileObject getChildFolder( final FileSystemManager fileSystem, final FileObject baseFolder, final String baseName) throws FileSystemException { for (int counter = 0; counter < TEMPORARY_FOLDER_ATTEMPTS; ++counter) { final FileObject temporaryFolder = fileSystem.resolveFile(baseFolder, baseName + counter); if (!temporaryFolder.exists()) { temporaryFolder.createFolder(); return temporaryFolder; } } throw new FileSystemException( "Failed to create directory within " + TEMPORARY_FOLDER_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (TEMPORARY_FOLDER_ATTEMPTS - 1) + ')'); }
public void updateParentFileCombo(FileObject selectedItem) { try { List<FileObject> parentChain = new ArrayList<FileObject>(); // are we a directory? try { if (selectedItem.getType() == FileType.FOLDER && selectedItem.getType().hasChildren()) { // we have real children.... parentChain.add(selectedItem); } } catch (Exception e) { // we are not a folder } FileObject parentFileObject; parentFileObject = selectedItem.getParent(); while (parentFileObject != null) { parentChain.add(parentFileObject); parentFileObject = parentFileObject.getParent(); } File roots[] = File.listRoots(); if (currentPanel != null) { for (int i = 0; i < roots.length; i++) { parentChain.add(currentPanel.resolveFile(roots[i].getAbsolutePath())); } } String items[] = new String[parentChain.size()]; int idx = 0; for (int i = parentChain.size() - 1; i >= 0; i--) { items[idx++] = ((FileObject) parentChain.get(i)).getName().getURI(); } openFileCombo.setItems(items); openFileCombo.select(items.length - 1); } catch (Exception e) { e.printStackTrace(); // then let's not update the GUI } }
@Internal("file.create") @DataParallel public static boolean fileCreate( @Current Context context, @Recycle String fileName, @Recycle(false) boolean showWarnings) throws IOException { try { FileObject file = context.resolveFile(fileName); // VFS will create the parent folder if it doesn't exist, // which the R method is not supposed to do if (!file.getParent().exists()) { throw new IOException("No such file or directory"); } file.getContent().getOutputStream().close(); return true; } catch (Exception e) { if (showWarnings) { Warning.invokeWarning( context, "cannot create file '%s', reason '%s'", fileName, e.getMessage()); } return false; } }