📄 fileutil.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: FileUtil.java
package org.gudy.azureus2.core3.util;
import com.aelitis.azureus.core.*;
import java.io.*;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.platform.*;
import org.gudy.azureus2.plugins.platform.PlatformManagerException;
// Referenced classes of package org.gudy.azureus2.core3.util:
// AEMonitor, BDecoder, BEncoder, Constants,
// Debug, SystemProperties
public class FileUtil
{
private static final LogIDs LOGID;
public static final String DIR_SEP = System.getProperty("file.separator");
private static final int RESERVED_FILE_HANDLE_COUNT = 4;
private static List reserved_file_handles = new ArrayList();
private static AEMonitor class_mon = new AEMonitor("FileUtil:class");
private static Method reflectOnUsableSpace;
public FileUtil()
{
}
public static boolean isAncestorOf(File parent, File child)
{
parent = canonise(parent);
child = canonise(child);
if (parent.equals(child))
return true;
String parent_s = parent.getPath();
String child_s = child.getPath();
if (parent_s.charAt(parent_s.length() - 1) != File.separatorChar)
parent_s = (new StringBuilder()).append(parent_s).append(File.separatorChar).toString();
return child_s.startsWith(parent_s);
}
public static File canonise(File file)
{
return file.getCanonicalFile();
IOException ioe;
ioe;
return file;
}
public static String getCanonicalFileName(String filename)
{
String canonicalFileName = filename;
try
{
canonicalFileName = (new File(filename)).getCanonicalPath();
}
catch (IOException ignore) { }
return canonicalFileName;
}
public static File getUserFile(String filename)
{
return new File(SystemProperties.getUserPath(), filename);
}
public static File getApplicationFile(String filename)
{
String path = SystemProperties.getApplicationPath();
if (Constants.isOSX)
path = (new StringBuilder()).append(path).append("/").append(SystemProperties.getApplicationName()).append(".app/Contents/").toString();
return new File(path, filename);
}
public static boolean recursiveDelete(File f)
{
String defSaveDir;
String moveToDir;
defSaveDir = COConfigurationManager.getStringParameter("Default save path");
moveToDir = COConfigurationManager.getStringParameter("Completed Files Directory", "");
try
{
moveToDir = (new File(moveToDir)).getCanonicalPath();
}
catch (Throwable e) { }
try
{
defSaveDir = (new File(defSaveDir)).getCanonicalPath();
}
catch (Throwable e) { }
if (!f.getCanonicalPath().equals(moveToDir))
break MISSING_BLOCK_LABEL_67;
System.out.println("FileUtil::recursiveDelete:: not allowed to delete the MoveTo dir !");
return false;
if (!f.getCanonicalPath().equals(defSaveDir))
break MISSING_BLOCK_LABEL_88;
System.out.println("FileUtil::recursiveDelete:: not allowed to delete the default data dir !");
return false;
File files[];
int i;
if (!f.isDirectory())
break MISSING_BLOCK_LABEL_140;
files = f.listFiles();
i = 0;
_L1:
if (i >= files.length)
break MISSING_BLOCK_LABEL_128;
if (!recursiveDelete(files[i]))
return false;
i++;
goto _L1
if (!f.delete())
return false;
break MISSING_BLOCK_LABEL_153;
if (!f.delete())
return false;
break MISSING_BLOCK_LABEL_153;
Exception ignore;
ignore;
return true;
}
public static long getFileOrDirectorySize(File file)
{
if (file.isFile())
return file.length();
long res = 0L;
File files[] = file.listFiles();
if (files != null)
{
for (int i = 0; i < files.length; i++)
res += getFileOrDirectorySize(files[i]);
}
return res;
}
protected static void recursiveEmptyDirDelete(File f, Set ignore_set, boolean log_warnings)
{
String defSaveDir;
String moveToDir;
File files[];
defSaveDir = COConfigurationManager.getStringParameter("Default save path");
moveToDir = COConfigurationManager.getStringParameter("Completed Files Directory", "");
if (defSaveDir.trim().length() > 0)
defSaveDir = (new File(defSaveDir)).getCanonicalPath();
if (moveToDir.trim().length() > 0)
moveToDir = (new File(moveToDir)).getCanonicalPath();
if (!f.isDirectory())
break MISSING_BLOCK_LABEL_363;
files = f.listFiles();
if (files == null)
{
if (log_warnings)
Debug.out((new StringBuilder()).append("Empty folder delete: failed to list contents of directory ").append(f).toString());
return;
}
for (int i = 0; i < files.length; i++)
{
File x = files[i];
if (x.isDirectory())
{
recursiveEmptyDirDelete(files[i], ignore_set, log_warnings);
continue;
}
if (ignore_set.contains(x.getName().toLowerCase()) && !x.delete() && log_warnings)
Debug.out((new StringBuilder()).append("Empty folder delete: failed to delete file ").append(x).toString());
}
if (f.getCanonicalPath().equals(moveToDir))
{
if (log_warnings)
Debug.out("Empty folder delete: not allowed to delete the MoveTo dir !");
return;
}
if (f.getCanonicalPath().equals(defSaveDir))
{
if (log_warnings)
Debug.out("Empty folder delete: not allowed to delete the default data dir !");
return;
}
try
{
File files_inside[] = f.listFiles();
if (files_inside.length == 0)
{
if (!f.delete() && log_warnings)
Debug.out((new StringBuilder()).append("Empty folder delete: failed to delete directory ").append(f).toString());
} else
if (log_warnings)
Debug.out((new StringBuilder()).append("Empty folder delete: ").append(files_inside.length).append(" file(s)/folder(s) still in \"").append(f).append("\" - first listed item is \"").append(files_inside[0].getName()).append("\". Not removing.").toString());
}
catch (Exception e)
{
Debug.out(e.toString());
}
}
public static String convertOSSpecificChars(String file_name_in, boolean is_folder)
{
char chars[] = file_name_in.toCharArray();
for (int i = 0; i < chars.length; i++)
if (chars[i] == '"')
chars[i] = '\'';
if (!Constants.isOSX)
{
if (Constants.isWindows)
{
String not_allowed = "\\/:?*<>|";
for (int i = 0; i < chars.length; i++)
if (not_allowed.indexOf(chars[i]) != -1)
chars[i] = '_';
if (is_folder)
{
for (int i = chars.length - 1; i >= 0 && (chars[i] == '.' || chars[i] == ' '); i--)
chars[i] = '_';
}
}
for (int i = 0; i < chars.length; i++)
{
char c = chars[i];
if (c == '/' || c == '\r' || c == '\n')
chars[i] = ' ';
}
}
String file_name_out = new String(chars);
try
{
if (Constants.isWindows)
{
for (; file_name_out.endsWith(" "); file_name_out = file_name_out.substring(0, file_name_out.length() - 1));
} else
{
String str = (new File(file_name_out)).getCanonicalFile().toString();
int p = str.lastIndexOf(File.separator);
file_name_out = str.substring(p + 1);
}
}
catch (Throwable e) { }
return file_name_out;
}
public static void writeResilientConfigFile(String file_name, Map data)
{
File parent_dir = new File(SystemProperties.getUserPath());
boolean use_backups = COConfigurationManager.getBooleanParameter("Use Config File Backups");
writeResilientFile(parent_dir, file_name, data, use_backups);
}
public static void writeResilientFile(File file, Map data)
{
writeResilientFile(file.getParentFile(), file.getName(), data, false);
}
public static void writeResilientFile(File parent_dir, String file_name, Map data, boolean use_backup)
{
writeResilientFile(parent_dir, file_name, data, use_backup, true);
}
public static void writeResilientFile(File parent_dir, String file_name, Map data, boolean use_backup, boolean copy_to_backup)
{
if (use_backup)
{
File originator = new File(parent_dir, file_name);
if (originator.exists())
backupFile(originator, copy_to_backup);
}
writeResilientFile(parent_dir, file_name, data);
}
private static void writeResilientFile(File parent_dir, String file_name, Map data)
{
class_mon.enter();
File temp;
BufferedOutputStream baos;
getReservedFileHandles();
temp = new File(parent_dir, (new StringBuilder()).append(file_name).append(".saving").toString());
baos = null;
byte encoded_data[] = BEncoder.encode(data);
FileOutputStream tempOS = new FileOutputStream(temp, false);
baos = new BufferedOutputStream(tempOS, 8192);
baos.write(encoded_data);
baos.flush();
tempOS.getFD().sync();
baos.close();
baos = null;
if (temp.length() > 1L)
{
File file = new File(parent_dir, file_name);
if (file.exists())
file.delete();
temp.renameTo(file);
}
Exception e;
try
{
if (baos != null)
baos.close();
}
// Misplaced declaration of an exception variable
catch (Exception e)
{
Logger.log(new LogAlert(false, (new StringBuilder()).append("Save of '").append(file_name).append("' fails").toString(), e));
}
break MISSING_BLOCK_LABEL_346;
e;
Logger.log(new LogAlert(false, (new StringBuilder()).append("Save of '").append(file_name).append("' fails").toString(), e));
try
{
if (baos != null)
baos.close();
}
// Misplaced declaration of an exception variable
catch (Exception e)
{
Logger.log(new LogAlert(false, (new StringBuilder()).append("Save of '").append(file_name).append("' fails").toString(), e));
}
break MISSING_BLOCK_LABEL_346;
Exception exception;
exception;
try
{
if (baos != null)
baos.close();
}
catch (Exception e)
{
Logger.log(new LogAlert(false, (new StringBuilder()).append("Save of '").append(file_name).append("' fails").toString(), e));
}
throw exception;
releaseReservedFileHandles();
break MISSING_BLOCK_LABEL_360;
Exception exception1;
exception1;
releaseReservedFileHandles();
throw exception1;
class_mon.exit();
break MISSING_BLOCK_LABEL_380;
Exception exception2;
exception2;
class_mon.exit();
throw exception2;
}
public static boolean resilientConfigFileExists(String name)
{
File parent_dir = new File(SystemProperties.getUserPath());
boolean use_backups = COConfigurationManager.getBooleanParameter("Use Config File Backups");
return (new File(parent_dir, name)).exists() || use_backups && (new File(parent_dir, (new StringBuilder()).append(name).append(".bak").toString())).exists();
}
public static Map readResilientConfigFile(String file_name)
{
File parent_dir = new File(SystemProperties.getUserPath());
boolean use_backups = COConfigurationManager.getBooleanParameter("Use Config File Backups");
return readResilientFile(parent_dir, file_name, use_backups);
}
public static Map readResilientConfigFile(String file_name, boolean use_backups)
{
File parent_dir = new File(SystemProperties.getUserPath());
if (!use_backups && (new File(parent_dir, (new StringBuilder()).append(file_name).append(".bak").toString())).exists())
use_backups = true;
return readResilientFile(parent_dir, file_name, use_backups);
}
public static Map readResilientFile(File file)
{
return readResilientFile(file.getParentFile(), file.getName(), false);
}
public static Map readResilientFile(File parent_dir, String file_name, boolean use_backup)
{
File backup_file = new File(parent_dir, (new StringBuilder()).append(file_name).append(".bak").toString());
if (use_backup)
use_backup = backup_file.exists();
Map res = readResilientFileSupport(parent_dir, file_name, !use_backup);
if (res == null && use_backup)
{
res = readResilientFileSupport(parent_dir, (new StringBuilder()).append(file_name).append(".bak").toString(), false);
if (res != null)
{
Logger.log(new LogAlert(false, 1, (new StringBuilder()).append("Backup file '").append(backup_file).append("' has been used for recovery purposes").toString()));
writeResilientFile(parent_dir, file_name, res, false);
} else
{
res = readResilientFileSupport(parent_dir, file_name, true);
}
}
if (res == null)
res = new HashMap();
return res;
}
private static Map readResilientFileSupport(File parent_dir, String file_name, boolean attempt_recovery)
{
class_mon.enter();
Map map;
getReservedFileHandles();
Map res = null;
try
{
res = readResilientFile(file_name, parent_dir, file_name, 0, false);
}
catch (Throwable e) { }
if (res == null && attempt_recovery)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -