⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskmanagerimpl.java

📁 java 文件下载器。可自定义
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		}
		if (pieceNumber >= nbPieces)
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, (new StringBuilder()).append("Write invalid: ").append(originator).append(" pieceNumber=").append(pieceNumber).append(" >= this.nbPieces=").append(nbPieces).toString()));
			return false;
		}
		int length = pieceLength;
		if (pieceNumber == nbPieces - 1)
			length = lastPieceLength;
		if (offset < 0)
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, (new StringBuilder()).append("Write invalid: ").append(originator).append(" offset=").append(offset).append(" < 0").toString()));
			return false;
		}
		if (offset > length)
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, (new StringBuilder()).append("Write invalid: ").append(originator).append(" offset=").append(offset).append(" > length=").append(length).toString()));
			return false;
		}
		int size = data.remaining((byte)8);
		if (size <= 0)
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, (new StringBuilder()).append("Write invalid: ").append(originator).append(" size=").append(size).append(" <= 0").toString()));
			return false;
		}
		if (offset + size > length)
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, (new StringBuilder()).append("Write invalid: ").append(originator).append(" offset=").append(offset).append(" + size=").append(size).append(" > length=").append(length).toString()));
			return false;
		} else
		{
			return true;
		}
	}

	public boolean checkBlockConsistencyForRead(String originator, int pieceNumber, int offset, int length)
	{
		return DiskManagerUtil.checkBlockConsistencyForRead(this, originator, pieceNumber, offset, length);
	}

	public boolean checkBlockConsistencyForHint(String originator, int pieceNumber, int offset, int length)
	{
		return DiskManagerUtil.checkBlockConsistencyForHint(this, originator, pieceNumber, offset, length);
	}

	public void saveResumeData(boolean interim_save)
		throws Exception
	{
		resume_handler.saveResumeData(interim_save);
	}

	public void downloadEnded()
	{
		moveDownloadFilesWhenEndedOrRemoved(false, true);
	}

	public void downloadRemoved()
	{
		moveDownloadFilesWhenEndedOrRemoved(true, true);
	}

	private boolean moveDownloadFilesWhenEndedOrRemoved(boolean removing, boolean torrent_file_exists)
	{
		boolean flag;
		start_stop_mon.enter();
		boolean ending = !removing;
		if (!ending)
			break MISSING_BLOCK_LABEL_87;
		if (!alreadyMoved)
			break MISSING_BLOCK_LABEL_82;
		flag = false;
		start_stop_mon.exit();
		if (!removing)
			try
			{
				saveResumeData(false);
			}
			catch (Throwable e)
			{
				setFailed((new StringBuilder()).append("Resume data save fails: ").append(Debug.getNestedExceptionMessage(e)).toString());
			}
		return flag;
		alreadyMoved = true;
		boolean flag1;
		SaveLocationChange move_details;
		if (removing)
			move_details = DownloadManagerMoveHandler.onRemoval(download_manager);
		else
			move_details = DownloadManagerMoveHandler.onCompletion(download_manager);
		if (move_details != null)
			moveFiles(move_details, true);
		flag1 = true;
		start_stop_mon.exit();
		if (!removing)
			try
			{
				saveResumeData(false);
			}
			catch (Throwable e)
			{
				setFailed((new StringBuilder()).append("Resume data save fails: ").append(Debug.getNestedExceptionMessage(e)).toString());
			}
		return flag1;
		Exception exception;
		exception;
		start_stop_mon.exit();
		if (!removing)
			try
			{
				saveResumeData(false);
			}
			catch (Throwable e)
			{
				setFailed((new StringBuilder()).append("Resume data save fails: ").append(Debug.getNestedExceptionMessage(e)).toString());
			}
		throw exception;
	}

	public void moveDataFiles(File new_parent_dir, String new_name)
	{
		SaveLocationChange loc_change = new SaveLocationChange();
		loc_change.download_location = new_parent_dir;
		loc_change.download_name = new_name;
		moveFiles(loc_change, false);
	}

	protected void moveFiles(SaveLocationChange loc_change, boolean change_to_read_only)
	{
		boolean move_files;
		move_files = false;
		if (loc_change.hasDownloadChange())
			move_files = !isFileDestinationIsItself(loc_change);
		start_stop_mon.enter();
		boolean files_moved = true;
		if (move_files)
			files_moved = moveDataFiles0(loc_change, change_to_read_only);
		if (loc_change.hasTorrentChange() && files_moved)
			moveTorrentFile(loc_change);
		start_stop_mon.exit();
		break MISSING_BLOCK_LABEL_101;
		Exception e;
		e;
		Debug.printStackTrace(e);
		start_stop_mon.exit();
		break MISSING_BLOCK_LABEL_101;
		Exception exception;
		exception;
		start_stop_mon.exit();
		throw exception;
	}

	private void logMoveFileError(String destination_path, String message)
	{
		Logger.log(new LogEvent(this, LOGID, 3, message));
		Logger.logTextResource(new LogAlert(this, true, 3, "DiskManager.alert.movefilefails"), new String[] {
			destination_path, message
		});
	}

	private boolean isFileDestinationIsItself(SaveLocationChange loc_change)
	{
		File old_location;
		File new_location;
		old_location = download_manager.getAbsoluteSaveLocation();
		new_location = loc_change.normaliseDownloadLocation(old_location);
		old_location = old_location.getCanonicalFile();
		new_location = new_location.getCanonicalFile();
		if (old_location.equals(new_location))
			return true;
		if (download_manager.getTorrent().isSimpleTorrent() || !FileUtil.isAncestorOf(new_location, old_location))
			break MISSING_BLOCK_LABEL_88;
		String msg = "Target is sub-directory of files";
		logMoveFileError(new_location.toString(), msg);
		return true;
		Throwable e;
		e;
		Debug.out(e);
		return false;
	}

	private boolean moveDataFiles0(SaveLocationChange loc_change, boolean change_to_read_only)
		throws Exception
	{
		File move_to_dir_name = loc_change.download_location;
		if (move_to_dir_name == null)
			move_to_dir_name = download_manager.getAbsoluteSaveLocation().getParentFile();
		String move_to_dir = move_to_dir_name.toString();
		String new_name = loc_change.download_name;
		if (files == null)
			return false;
		if (isFileDestinationIsItself(loc_change))
			return false;
		boolean simple_torrent = download_manager.getTorrent().isSimpleTorrent();
		File save_location = download_manager.getAbsoluteSaveLocation();
		String move_from_dir = save_location.getParentFile().getCanonicalFile().getPath();
		File new_files[] = new File[files.length];
		File old_files[] = new File[files.length];
		boolean link_only[] = new boolean[files.length];
		for (int i = 0; i < files.length; i++)
		{
			File old_file = files[i].getFile(false);
			File linked_file = FMFileManagerFactory.getSingleton().getFileLink(torrent, old_file);
			if (!linked_file.equals(old_file))
				if (simple_torrent)
				{
					if (linked_file.getParentFile().getCanonicalPath().equals(save_location.getParentFile().getCanonicalPath()))
						old_file = linked_file;
					else
						link_only[i] = true;
				} else
				if (linked_file.getCanonicalPath().startsWith(save_location.getCanonicalPath()))
					old_file = linked_file;
				else
					link_only[i] = true;
			old_files[i] = old_file;
			String old_parent_path = old_file.getCanonicalFile().getParent();
			String sub_path;
			if (old_parent_path.startsWith(move_from_dir))
			{
				sub_path = old_parent_path.substring(move_from_dir.length());
			} else
			{
				logMoveFileError(move_to_dir, (new StringBuilder()).append("Could not determine relative path for file - ").append(old_parent_path).toString());
				throw new IOException((new StringBuilder()).append("relative path assertion failed: move_from_dir=\"").append(move_from_dir).append("\", old_parent_path=\"").append(old_parent_path).append("\"").toString());
			}
			if (sub_path.startsWith(File.separator))
				sub_path = sub_path.substring(1);
			File new_file;
			if (new_name == null)
				new_file = new File(new File(move_to_dir, sub_path), old_file.getName());
			else
			if (simple_torrent)
			{
				new_file = new File(new File(move_to_dir, sub_path), new_name);
			} else
			{
				int pos = sub_path.indexOf(File.separator);
				String new_path;
				if (pos == -1)
				{
					new_path = new_name;
				} else
				{
					String sub_sub_path = sub_path.substring(pos);
					String expected_old_name = sub_path.substring(0, pos);
					new_path = (new StringBuilder()).append(new_name).append(sub_sub_path).toString();
					boolean assert_expected_old_name = expected_old_name.equals(save_location.getName());
					if (!assert_expected_old_name)
						Debug.out((new StringBuilder()).append("Assertion check for renaming file in multi-name torrent ").append(assert_expected_old_name ? "passed" : "failed").append("\n").append("  Old parent path: ").append(old_parent_path).append("\n").append("  Subpath: ").append(sub_path).append("\n").append("  Sub-subpath: ").append(sub_sub_path).append("\n").append("  Expected old name: ").append(expected_old_name).append("\n").append("  Torrent pre-move name: ").append(save_location.getName()).append("\n").append("  New torrent name: ").append(new_name).append("\n").append("  Old file: ").append(old_file).append("\n").append("  Linked file: ").append(linked_file).append("\n").append("\n").append("  Move-to-dir: ").append(move_to_dir).append("\n").append("  New path: ").append(new_path).append("\n").append("  Old file [name]: ").append(old_file.getName()).append("\n").toString());
				}
				new_file = new File(new File(move_to_dir, new_path), old_file.getName());
			}
			new_files[i] = new_file;
			if (link_only[i])
				continue;
			if (new_file.exists())
			{
				String msg = (new StringBuilder()).append("").append(linked_file.getName()).append(" already exists in MoveTo destination dir").toString();
				Logger.log(new LogEvent(this, LOGID, 3, msg));
				Logger.logTextResource(new LogAlert(this, true, 3, "DiskManager.alert.movefileexists"), new String[] {
					old_file.getName()
				});
				Debug.out(msg);
				return false;
			}
			FileUtil.mkdirs(new_file.getParentFile());
		}

		for (int i = 0; i < files.length; i++)
		{
			File new_file = new_files[i];
			int j;
			try
			{
				files[i].moveFile(new_file, link_only[i]);
				if (change_to_read_only)
					files[i].setAccessMode(1);
				continue;
			}
			catch (CacheFileManagerException e)
			{
				String msg = (new StringBuilder()).append("Failed to move ").append(old_files[i].toString()).append(" to destination dir").toString();
				Logger.log(new LogEvent(this, LOGID, 3, msg));
				Logger.logTextResource(new LogAlert(this, true, 3, "DiskManager.alert.movefilefails"), new String[] {
					old_files[i].toString(), Debug.getNestedExceptionMessage(e)
				});
				j = 0;
			}
			for (; j < i; j++)
				try
				{
					files[j].moveFile(old_files[j], link_only[j]);
				}
				catch (CacheFileManagerException f)
				{
					Logger.logTextResource(new LogAlert(this, true, 3, "DiskManager.alert.movefilerecoveryfails"), new String[] {
						old_files[j].toString(), Debug.getNestedExceptionMessage(f)
					});
				}

			return false;
		}

		if (save_location.isDirectory())
			TorrentUtils.recursiveEmptyDirDelete(save_location, false);
		if (new_name == null)
			download_manager.setTorrentSaveDir(move_to_dir);
		else
			download_manager.setTorrentSaveDir(move_to_dir, new_name);
		return true;
	}

	private void moveTorrentFile(SaveLocationChange loc_change)
	{
		if (!loc_change.hasTorrentChange())
			return;
		File old_torrent_file = new File(download_manager.getTorrentFileName());
		File new_torrent_file = loc_change.normaliseTorrentLocation(old_torrent_file);
		if (!old_torrent_file.exists())
		{
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 1, (new StringBuilder()).append("Torrent file '").append(old_torrent_file.getPath()).append("' has been deleted, move operation ignored").toString()));
			return;
		}
		try
		{
			download_manager.setTorrentFile(loc_change.torrent_location, loc_change.torrent_name);
		}
		catch (DownloadManagerException e)
		{
			String msg = (new StringBuilder()).append("Failed to move ").append(old_torrent_file.toString()).append(" to ").append(new_torrent_file.toString()).toString();
			if (Logger.isEnabled())
				Logger.log(new LogEvent(this, LOGID, 3, msg));
			Logger.logTextResource(new LogAlert(this, true, 3, "DiskManager.alert.movefilefails"), new String[] {
				old_torrent_file.toString(), new_torrent_file.toString()
			});
			Debug.out(msg);
		}
	}

	public TOTorrent getTorrent()
	{
		return torrent;
	}

	public void addListener(DiskManagerListener l)
	{
		listeners.addListener(l);
		int params[] = {
			getState(), getState()
		};
		listeners.dispatch(l, 1, params);
	}

	public void removeListener(DiskManagerListener l)
	{
		listeners.removeListener(l);
	}

	public boolean hasListener(DiskManagerListener l)
	{
		return listeners.hasListener(l);
	}

	public static void deleteDataFiles(TOTorrent torrent, String torrent_save_dir, String torrent_save_file)
	{
		if (torrent == null || torrent_save_file == null)
			return;
		try
		{
			if (torrent.isSimpleTorrent())
			{
				File target = new File(torrent_save_dir, torrent_save_file);
				target = FMFileManagerFactory.getSingleton().getFileLink(torrent, target.getCanonicalFile());
				FileUtil.deleteWithRecycle(target);
			} else
			{
				PlatformManager mgr = PlatformManagerFactory.getPlatformManager();
				if (Constants.isOSX && torrent_save_file.length() > 0 && COConfigurationManager.getBooleanParameter("Move Deleted Data To Recycle Bin") && mgr.hasCapability(PlatformManagerCapabilities.RecoverableFileDelete))
					try
					{
						String dir = (new StringBuilder()).append(torrent_save_dir).append(File.separatorChar).append(torrent_save_file).append(File.separatorChar).toString();
						if (countFiles(new File(dir)) == countDataFiles(torrent, torrent_save_dir, torrent_save_file))
							mgr.performRecoverableFileDelete(dir);
						else
							deleteDataFileContents(torrent, torrent_save_dir, torrent_save_file);
					}
					catch (PlatformManagerException ex)
					{
						deleteDataFileContents(torrent, torrent_save_dir, torrent_save_file);
					}
				else
					deleteDataFileContents(torrent, torrent_save_dir, torrent_save_file);
			}
		}
		catch (Throwable e)
		{
			Debug.printStackTrace(e);
		}
	}

	private static int countFiles(File f)
	{
		if (f.isFile())
			return 1;
		int res = 0;
		File files[] = f.listFiles();
		if (files != null)
		{
			for (int i = 0; i < files.length; i++)
				res += countFiles(files[i]);

		}
		return res;
	}

	private static int countDataFiles(TOTorrent torrent, String torrent_save_dir, String torrent_save_file)
	{
		int res;
		res = 0;
		LocaleUtilDecoder locale_decoder = LocaleTorrentUtil.getTorrentEncoding(torrent);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -