public boolean hasAccess() { assert (binDir != null); assert (homeDir != null); return binDir.canWrite() && homeDir.canWrite(); }
public void deinit(Appendable out, boolean force) throws Exception { Settings settings = new Settings(platform.getConfigFile()); if (!force) { Justif justify = new Justif(80, 40); StringBuilder sb = new StringBuilder(); Formatter f = new Formatter(sb); try { String list = listFiles(platform.getGlobal()); if (list != null) { f.format("In global default environment:%n"); f.format(list); } list = listFiles(platform.getLocal()); if (list != null) { f.format("In local default environment:%n"); f.format(list); } if (settings.containsKey(JPM_CACHE_GLOBAL)) { list = listFiles(IO.getFile(settings.get(JPM_CACHE_GLOBAL))); if (list != null) { f.format("In global configured environment:%n"); f.format(list); } } if (settings.containsKey(JPM_CACHE_LOCAL)) { list = listFiles(IO.getFile(settings.get(JPM_CACHE_LOCAL))); if (list != null) { f.format("In local configured environment:%n"); f.format(list); } } list = listSupportFiles(); if (list != null) { f.format("jpm support files:%n"); f.format(list); } f.format("%n%n"); f.format( "All files listed above will be deleted if deinit is run with the force flag set" + " (\"jpm deinit -f\" or \"jpm deinit --force\"%n%n"); f.flush(); justify.wrap(sb); out.append(sb.toString()); } finally { f.close(); } } else { // i.e. if(force) int count = 0; File[] caches = {platform.getGlobal(), platform.getLocal(), null, null}; if (settings.containsKey(JPM_CACHE_LOCAL)) { caches[2] = IO.getFile(settings.get(JPM_CACHE_LOCAL)); } if (settings.containsKey(JPM_CACHE_GLOBAL)) { caches[3] = IO.getFile(settings.get(JPM_CACHE_GLOBAL)); } ArrayList<File> toDelete = new ArrayList<File>(); for (File cache : caches) { if (cache == null || !cache.exists()) { continue; } listFiles(cache, toDelete); if (toDelete.size() > count) { count = toDelete.size(); if (!cache.canWrite()) { reporter.error(PERMISSION_ERROR + " (" + cache + ")"); return; } toDelete.add(cache); } } listSupportFiles(toDelete); for (File f : toDelete) { if (f.exists() && !f.canWrite()) { reporter.error(PERMISSION_ERROR + " (" + f + ")"); } } if (reporter.getErrors().size() > 0) { return; } for (File f : toDelete) { if (f.exists()) { IO.deleteWithException(f); } } } }
private static String validateLogFile(String logFileName, String scriptName) { String strippedDownScriptName = null; if (scriptName != null) { File scriptFile = new File(scriptName); if (!scriptFile.isDirectory()) { String scriptFileAbsPath; try { scriptFileAbsPath = scriptFile.getCanonicalPath(); } catch (IOException ioe) { throw new AssertionError( "Could not compute canonical path to the script file " + ioe.getMessage()); } strippedDownScriptName = getFileFromCanonicalPath(scriptFileAbsPath); } } String defaultLogFileName = (strippedDownScriptName == null ? "pig_" : strippedDownScriptName) + new Date().getTime() + ".log"; File logFile; if (logFileName != null) { logFile = new File(logFileName); // Check if the file name is a directory // append the default file name to the file if (logFile.isDirectory()) { if (logFile.canWrite()) { try { logFileName = logFile.getCanonicalPath() + File.separator + defaultLogFileName; } catch (IOException ioe) { throw new AssertionError( "Could not compute canonical path to the log file " + ioe.getMessage()); } return logFileName; } else { throw new AssertionError( "Need write permission in the directory: " + logFileName + " to create log file."); } } else { // we have a relative path or an absolute path to the log file // check if we can write to the directory where this file is/will be stored if (logFile.exists()) { if (logFile.canWrite()) { try { logFileName = new File(logFileName).getCanonicalPath(); } catch (IOException ioe) { throw new AssertionError( "Could not compute canonical path to the log file " + ioe.getMessage()); } return logFileName; } else { // do not have write permissions for the log file // bail out with an error message throw new AssertionError( "Cannot write to file: " + logFileName + ". Need write permission."); } } else { logFile = logFile.getParentFile(); if (logFile != null) { // if the directory is writable we are good to go if (logFile.canWrite()) { try { logFileName = new File(logFileName).getCanonicalPath(); } catch (IOException ioe) { throw new AssertionError( "Could not compute canonical path to the log file " + ioe.getMessage()); } return logFileName; } else { throw new AssertionError( "Need write permission in the directory: " + logFile + " to create log file."); } } // end if logFile != null else is the default in fall through } // end else part of logFile.exists() } // end else part of logFile.isDirectory() } // end if logFileName != null // file name is null or its in the current working directory // revert to the current working directory String currDir = System.getProperty("user.dir"); logFile = new File(currDir); logFileName = currDir + File.separator + (logFileName == null ? defaultLogFileName : logFileName); if (logFile.canWrite()) { return logFileName; } throw new RuntimeException("Cannot write to log file: " + logFileName); }