use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.
the class DiskManagerUtil method getFileInfoSkeleton.
public static DiskManagerFileInfoSet getFileInfoSkeleton(final DownloadManager download_manager, final DiskManagerListener listener) {
final TOTorrent torrent = download_manager.getTorrent();
if (torrent == null) {
return (new DiskManagerFileInfoSetImpl(new DiskManagerFileInfoImpl[0], null));
}
String tempRootDir = download_manager.getAbsoluteSaveLocation().getParent();
if (// in case we alraedy are at the root
tempRootDir == null)
tempRootDir = download_manager.getAbsoluteSaveLocation().getPath();
if (!torrent.isSimpleTorrent()) {
tempRootDir += File.separator + download_manager.getAbsoluteSaveLocation().getName();
}
tempRootDir += File.separator;
// prevent attempted state saves and associated nastyness during population of
// the file skeleton entries
final boolean[] loading = { true };
try {
final String root_dir = StringInterner.intern(tempRootDir);
try {
final LocaleUtilDecoder locale_decoder = LocaleTorrentUtil.getTorrentEncoding(torrent);
TOTorrentFile[] torrent_files = torrent.getFiles();
final FileSkeleton[] res = new FileSkeleton[torrent_files.length];
final String incomplete_suffix = download_manager.getDownloadState().getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
final DiskManagerFileInfoSet fileSetSkeleton = new DiskManagerFileInfoSet() {
@Override
public DiskManagerFileInfo[] getFiles() {
return res;
}
@Override
public int nbFiles() {
return res.length;
}
@Override
public void setPriority(int[] toChange) {
if (toChange.length != res.length)
throw new IllegalArgumentException("array length mismatches the number of files");
for (int i = 0; i < res.length; i++) res[i].priority = toChange[i];
if (!loading[0]) {
DiskManagerImpl.storeFilePriorities(download_manager, res);
}
for (int i = 0; i < res.length; i++) if (toChange[i] != 0)
listener.filePriorityChanged(res[i]);
}
@Override
public void setSkipped(boolean[] toChange, boolean setSkipped) {
if (toChange.length != res.length)
throw new IllegalArgumentException("array length mismatches the number of files");
if (!setSkipped) {
String[] types = DiskManagerImpl.getStorageTypes(download_manager);
boolean[] toLinear = new boolean[toChange.length];
boolean[] toReorder = new boolean[toChange.length];
int num_linear = 0;
int num_reorder = 0;
for (int i = 0; i < toChange.length; i++) {
if (toChange[i]) {
int old_type = DiskManagerUtil.convertDMStorageTypeFromString(types[i]);
if (old_type == DiskManagerFileInfo.ST_COMPACT) {
toLinear[i] = true;
num_linear++;
} else if (old_type == DiskManagerFileInfo.ST_REORDER_COMPACT) {
toReorder[i] = true;
num_reorder++;
}
}
}
if (num_linear > 0) {
if (!Arrays.equals(toLinear, setStorageTypes(toLinear, DiskManagerFileInfo.ST_LINEAR))) {
return;
}
}
if (num_reorder > 0) {
if (!Arrays.equals(toReorder, setStorageTypes(toReorder, DiskManagerFileInfo.ST_REORDER))) {
return;
}
}
}
File[] to_link = new File[res.length];
for (int i = 0; i < res.length; i++) {
if (toChange[i]) {
to_link[i] = res[i].setSkippedInternal(setSkipped);
}
}
if (!loading[0]) {
DiskManagerImpl.storeFilePriorities(download_manager, res);
}
List<Integer> from_indexes = new ArrayList<>();
List<File> from_links = new ArrayList<>();
List<File> to_links = new ArrayList<>();
for (int i = 0; i < res.length; i++) {
if (to_link[i] != null) {
from_indexes.add(i);
from_links.add(res[i].getFile(false));
to_links.add(to_link[i]);
}
}
if (from_links.size() > 0) {
download_manager.getDownloadState().setFileLinks(from_indexes, from_links, to_links);
}
if (!setSkipped) {
doFileExistenceChecks(this, toChange, download_manager, true);
}
for (int i = 0; i < res.length; i++) {
if (toChange[i]) {
listener.filePriorityChanged(res[i]);
}
}
}
@Override
public boolean[] setStorageTypes(boolean[] toChange, int newStorageType) {
if (toChange.length != res.length)
throw new IllegalArgumentException("array length mismatches the number of files");
String[] types = DiskManagerImpl.getStorageTypes(download_manager);
boolean[] modified = new boolean[res.length];
boolean[] toSkip = new boolean[res.length];
int toSkipCount = 0;
DownloadManagerState dmState = download_manager.getDownloadState();
try {
dmState.suppressStateSave(true);
for (int i = 0; i < res.length; i++) {
if (!toChange[i])
continue;
final int idx = i;
int old_type = DiskManagerUtil.convertDMStorageTypeFromString(types[i]);
if (newStorageType == old_type) {
modified[i] = true;
continue;
}
try {
File target_file = res[i].getFile(true);
if (target_file.exists()) {
CacheFile cache_file = CacheFileManagerFactory.getSingleton().createFile(new CacheFileOwner() {
@Override
public String getCacheFileOwnerName() {
return (download_manager.getInternalName());
}
@Override
public TOTorrentFile getCacheFileTorrentFile() {
return (res[idx].getTorrentFile());
}
@Override
public File getCacheFileControlFileDir() {
return (download_manager.getDownloadState().getStateFile());
}
@Override
public int getCacheMode() {
return (CacheFileOwner.CACHE_MODE_NORMAL);
}
}, target_file, DiskManagerUtil.convertDMStorageTypeToCache(newStorageType));
// need this to trigger recovery for re-order files :(
cache_file.getLength();
cache_file.close();
}
toSkip[i] = (newStorageType == FileSkeleton.ST_COMPACT || newStorageType == FileSkeleton.ST_REORDER_COMPACT) && !res[i].isSkipped();
if (toSkip[i]) {
toSkipCount++;
}
modified[i] = true;
} catch (Throwable e) {
Debug.printStackTrace(e);
Logger.log(new LogAlert(download_manager, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to change storage type for '" + res[i].getFile(true) + "': " + Debug.getNestedExceptionMessage(e)));
// download's not running - tag for recheck
RDResumeHandler.recheckFile(download_manager, res[i]);
}
types[i] = DiskManagerUtil.convertDMStorageTypeToString(newStorageType);
}
/*
* set storage type and skipped before we do piece clearing and file
* clearing checks as those checks work better when skipped/stype is set
* properly
*/
dmState.setListAttribute(DownloadManagerState.AT_FILE_STORE_TYPES, types);
if (toSkipCount > 0) {
setSkipped(toSkip, true);
}
for (int i = 0; i < res.length; i++) {
if (!toChange[i])
continue;
// download's not running, update resume data as necessary
int cleared = RDResumeHandler.storageTypeChanged(download_manager, res[i]);
if (cleared > 0) {
res[i].downloaded = res[i].downloaded - cleared * res[i].getTorrentFile().getTorrent().getPieceLength();
if (res[i].downloaded < 0)
res[i].downloaded = 0;
}
}
DiskManagerImpl.storeFileDownloaded(download_manager, res, true);
doFileExistenceChecks(this, toChange, download_manager, newStorageType == FileSkeleton.ST_LINEAR || newStorageType == FileSkeleton.ST_REORDER);
} finally {
dmState.suppressStateSave(false);
dmState.save();
}
return modified;
}
};
for (int i = 0; i < res.length; i++) {
final TOTorrentFile torrent_file = torrent_files[i];
final int file_index = i;
FileSkeleton info = new FileSkeleton() {
private volatile CacheFile read_cache_file;
// do not access this field directly, use lazyGetFile() instead
private WeakReference dataFile = new WeakReference(null);
@Override
public void setPriority(int b) {
priority = b;
DiskManagerImpl.storeFilePriorities(download_manager, res);
listener.filePriorityChanged(this);
}
@Override
public void setSkipped(boolean _skipped) {
if (!_skipped && getStorageType() == ST_COMPACT) {
if (!setStorageType(ST_LINEAR)) {
return;
}
}
if (!_skipped && getStorageType() == ST_REORDER_COMPACT) {
if (!setStorageType(ST_REORDER)) {
return;
}
}
File to_link = setSkippedInternal(_skipped);
DiskManagerImpl.storeFilePriorities(download_manager, res);
if (to_link != null) {
download_manager.getDownloadState().setFileLink(file_index, getFile(false), to_link);
}
if (!_skipped) {
boolean[] toCheck = new boolean[fileSetSkeleton.nbFiles()];
toCheck[file_index] = true;
doFileExistenceChecks(fileSetSkeleton, toCheck, download_manager, true);
}
listener.filePriorityChanged(this);
}
@Override
public int getAccessMode() {
return (READ);
}
@Override
public long getDownloaded() {
return (downloaded);
}
@Override
public long getLastModified() {
return (getFile(true).lastModified());
}
@Override
public void setDownloaded(long l) {
downloaded = l;
}
@Override
public String getExtension() {
String ext = lazyGetFile().getName();
if (incomplete_suffix != null && ext.endsWith(incomplete_suffix)) {
ext = ext.substring(0, ext.length() - incomplete_suffix.length());
}
int separator = ext.lastIndexOf(".");
if (separator == -1)
separator = 0;
return ext.substring(separator);
}
@Override
public int getFirstPieceNumber() {
return (torrent_file.getFirstPieceNumber());
}
@Override
public int getLastPieceNumber() {
return (torrent_file.getLastPieceNumber());
}
@Override
public long getLength() {
return (torrent_file.getLength());
}
@Override
public int getIndex() {
return (file_index);
}
@Override
public int getNbPieces() {
return (torrent_file.getNumberOfPieces());
}
@Override
public int getPriority() {
return (priority);
}
@Override
protected File setSkippedInternal(boolean _skipped) {
// returns the file to link to if linkage is required
skipped_internal = _skipped;
if (!download_manager.isDestroyed()) {
DownloadManagerState dm_state = download_manager.getDownloadState();
String dnd_sf = dm_state.getAttribute(DownloadManagerState.AT_DND_SUBFOLDER);
if (dnd_sf != null) {
File link = getLink();
File file = getFile(false);
if (_skipped) {
if (link == null || link.equals(file)) {
File parent = file.getParentFile();
if (parent != null) {
String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
String file_name = file.getName();
if (prefix != null && !file_name.startsWith(prefix)) {
file_name = prefix + file_name;
}
File new_parent = new File(parent, dnd_sf);
File new_file = new File(new_parent, file_name);
if (!new_file.exists()) {
if (!new_parent.exists()) {
new_parent.mkdirs();
}
if (new_parent.canWrite()) {
boolean ok;
if (file.exists()) {
ok = FileUtil.renameFile(file, new_file);
} else {
ok = true;
}
if (ok) {
return (new_file);
}
}
}
}
}
} else {
if (link != null && !file.exists()) {
File parent = file.getParentFile();
if (parent != null && parent.canWrite()) {
File new_parent = parent.getName().equals(dnd_sf) ? parent : new File(parent, dnd_sf);
// use link name to handle incomplete file suffix if set
File new_file = new File(new_parent, link.getName());
if (new_file.equals(link)) {
boolean ok;
String incomp_ext = dm_state.getAttribute(DownloadManagerState.AT_INCOMP_FILE_SUFFIX);
String file_name = file.getName();
String prefix = dm_state.getAttribute(DownloadManagerState.AT_DND_PREFIX);
boolean prefix_removed = false;
if (prefix != null && file_name.startsWith(prefix)) {
file_name = file_name.substring(prefix.length());
prefix_removed = true;
}
if (incomp_ext != null && incomp_ext.length() > 0 && getDownloaded() != getLength()) {
if (prefix == null) {
prefix = "";
}
file = new File(file.getParentFile(), prefix + file_name + incomp_ext);
} else if (prefix_removed) {
file = new File(file.getParentFile(), file_name);
}
if (new_file.exists()) {
ok = FileUtil.renameFile(new_file, file);
} else {
ok = true;
}
if (ok) {
File[] files = new_parent.listFiles();
if (files != null && files.length == 0) {
new_parent.delete();
}
return (file);
}
}
}
}
}
}
}
return (null);
}
@Override
public boolean isSkipped() {
return (skipped_internal);
}
@Override
public DiskManager getDiskManager() {
return (null);
}
@Override
public DownloadManager getDownloadManager() {
return (download_manager);
}
@Override
public File getFile(boolean follow_link) {
if (follow_link) {
File link = getLink();
if (link != null) {
return (link);
}
}
return lazyGetFile();
}
private File lazyGetFile() {
File toReturn = (File) dataFile.get();
if (toReturn != null)
return toReturn;
TOTorrent tor = download_manager.getTorrent();
String path_str = root_dir;
File simpleFile = null;
if (tor == null || tor.isSimpleTorrent()) {
// rumour has it tor can sometimes be null
simpleFile = download_manager.getAbsoluteSaveLocation();
} else {
byte[][] path_comps = torrent_file.getPathComponents();
for (int j = 0; j < path_comps.length; j++) {
String comp;
try {
comp = locale_decoder.decodeString(path_comps[j]);
} catch (UnsupportedEncodingException e) {
Debug.printStackTrace(e);
comp = "undecodableFileName" + file_index;
}
comp = FileUtil.convertOSSpecificChars(comp, j != path_comps.length - 1);
path_str += (j == 0 ? "" : File.separator) + comp;
}
}
dataFile = new WeakReference(toReturn = simpleFile != null ? simpleFile : new File(path_str));
// System.out.println("new file:"+toReturn);
return toReturn;
}
@Override
public TOTorrentFile getTorrentFile() {
return (torrent_file);
}
@Override
public boolean setLink(File link_destination) {
/**
* If we a simple torrent, then we'll redirect the call to the download and move the
* data files that way - that'll keep everything in sync.
*/
if (download_manager.getTorrent().isSimpleTorrent()) {
try {
download_manager.moveDataFiles(link_destination.getParentFile(), link_destination.getName());
return true;
} catch (DownloadManagerException e) {
// What should we do with the error?
return false;
}
}
return setLinkAtomic(link_destination);
}
@Override
public boolean setLinkAtomic(File link_destination) {
return (setFileLink(download_manager, res, this, lazyGetFile(), link_destination, null));
}
@Override
public boolean setLinkAtomic(File link_destination, FileUtil.ProgressListener pl) {
return (setFileLink(download_manager, res, this, lazyGetFile(), link_destination, pl));
}
@Override
public File getLink() {
return (download_manager.getDownloadState().getFileLink(file_index, lazyGetFile()));
}
@Override
public boolean setStorageType(int type) {
boolean[] change = new boolean[res.length];
change[file_index] = true;
return fileSetSkeleton.setStorageTypes(change, type)[file_index];
}
@Override
public int getStorageType() {
return (DiskManagerUtil.convertDMStorageTypeFromString(DiskManagerImpl.getStorageType(download_manager, file_index)));
}
@Override
public void flushCache() {
}
@Override
public DirectByteBuffer read(long offset, int length) throws IOException {
CacheFile temp;
try {
cache_read_mon.enter();
if (read_cache_file == null) {
try {
int type = convertDMStorageTypeFromString(DiskManagerImpl.getStorageType(download_manager, file_index));
read_cache_file = CacheFileManagerFactory.getSingleton().createFile(new CacheFileOwner() {
@Override
public String getCacheFileOwnerName() {
return (download_manager.getInternalName());
}
@Override
public TOTorrentFile getCacheFileTorrentFile() {
return (torrent_file);
}
@Override
public File getCacheFileControlFileDir() {
return (download_manager.getDownloadState().getStateFile());
}
@Override
public int getCacheMode() {
return (CacheFileOwner.CACHE_MODE_NORMAL);
}
}, getFile(true), convertDMStorageTypeToCache(type));
} catch (Throwable e) {
Debug.printStackTrace(e);
throw (new IOException(e.getMessage()));
}
}
temp = read_cache_file;
} finally {
cache_read_mon.exit();
}
DirectByteBuffer buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_DM_READ, length);
try {
temp.read(buffer, offset, CacheFile.CP_READ_CACHE);
} catch (Throwable e) {
buffer.returnToPool();
Debug.printStackTrace(e);
throw (new IOException(e.getMessage()));
}
return (buffer);
}
@Override
public int getReadBytesPerSecond() {
CacheFile temp = read_cache_file;
if (temp == null) {
return (0);
}
return (0);
}
@Override
public int getWriteBytesPerSecond() {
return (0);
}
@Override
public long getETA() {
return (-1);
}
@Override
public void close() {
CacheFile temp;
try {
cache_read_mon.enter();
temp = read_cache_file;
read_cache_file = null;
} finally {
cache_read_mon.exit();
}
if (temp != null) {
try {
temp.close();
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
}
@Override
public void addListener(DiskManagerFileInfoListener listener) {
if (getDownloaded() == getLength()) {
try {
listener.dataWritten(0, getLength());
listener.dataChecked(0, getLength());
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
}
@Override
public void removeListener(DiskManagerFileInfoListener listener) {
}
};
res[i] = info;
}
loadFilePriorities(download_manager, fileSetSkeleton);
loadFileDownloaded(download_manager, res);
return (fileSetSkeleton);
} finally {
loading[0] = false;
}
} catch (Throwable e) {
Debug.printStackTrace(e);
return (new DiskManagerFileInfoSetImpl(new DiskManagerFileInfoImpl[0], null));
}
}
use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.
the class DiskAccessControllerInstance method queueRequest.
protected void queueRequest(DiskAccessRequestImpl request) {
requestDispatcher dispatcher;
if (dispatchers.length == 1) {
dispatcher = dispatchers[0];
} else {
synchronized (torrent_dispatcher_map) {
long now = System.currentTimeMillis();
boolean check = false;
if (now - last_check > 60000 || now < last_check) {
check = true;
last_check = now;
}
if (check) {
Iterator it = torrent_dispatcher_map.values().iterator();
while (it.hasNext()) {
requestDispatcher d = (requestDispatcher) it.next();
long last_active = d.getLastRequestTime();
if (now - last_active > 60000) {
it.remove();
} else if (now < last_active) {
d.setLastRequestTime(now);
}
}
}
TOTorrent torrent = request.getFile().getTorrentFile().getTorrent();
dispatcher = (requestDispatcher) torrent_dispatcher_map.get(torrent);
if (dispatcher == null) {
int min_index = 0;
int min_size = Integer.MAX_VALUE;
for (int i = 0; i < dispatchers.length; i++) {
int size = dispatchers[i].size();
if (size == 0) {
min_index = i;
break;
}
if (size < min_size) {
min_size = size;
min_index = i;
}
}
dispatcher = dispatchers[min_index];
torrent_dispatcher_map.put(torrent, dispatcher);
}
dispatcher.setLastRequestTime(now);
}
}
dispatcher.queue(request);
}
use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.
the class RDResumeHandler method setTorrentResumeDataComplete.
public static void setTorrentResumeDataComplete(DownloadManagerState download_manager_state) {
TOTorrent torrent = download_manager_state.getTorrent();
int piece_count = torrent.getNumberOfPieces();
byte[] resume_pieces = new byte[piece_count];
Arrays.fill(resume_pieces, PIECE_DONE);
Map resume_data = new HashMap();
resume_data.put("resume data", resume_pieces);
Map partialPieces = new HashMap();
resume_data.put("blocks", partialPieces);
resume_data.put("valid", new Long(1));
saveResumeData(download_manager_state, resume_data);
}
use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.
the class RDResumeHandler method setTorrentResumeDataNearlyComplete.
public static void setTorrentResumeDataNearlyComplete(DownloadManagerState download_manager_state) {
// backwards compatability, resume data key is the dir
TOTorrent torrent = download_manager_state.getTorrent();
long piece_count = torrent.getNumberOfPieces();
byte[] resume_pieces = new byte[(int) piece_count];
Arrays.fill(resume_pieces, PIECE_DONE);
for (int i = 0; i < 3; i++) {
int piece_num = (int) (Math.random() * piece_count);
resume_pieces[piece_num] = PIECE_RECHECK_REQUIRED;
}
Map resumeMap = new HashMap();
resumeMap.put("resume data", resume_pieces);
Map partialPieces = new HashMap();
resumeMap.put("blocks", partialPieces);
// recheck the not-done pieces
resumeMap.put("valid", new Long(0));
saveResumeData(download_manager_state, resumeMap);
}
use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.
the class SubscriptionUtils method getAllCachedDownloadDetails.
public static SubscriptionDownloadDetails[] getAllCachedDownloadDetails(Core core) {
List<DownloadManager> dms = core.getGlobalManager().getDownloadManagers();
List<SubscriptionDownloadDetails> result = new ArrayList<>();
SubscriptionManager sub_man = SubscriptionManagerFactory.getSingleton();
for (int i = 0; i < dms.size(); i++) {
DownloadManager dm = dms.get(i);
TOTorrent torrent = dm.getTorrent();
if (torrent != null) {
try {
Subscription[] subs = sub_man.getKnownSubscriptions(torrent.getHash());
if (subs != null && subs.length > 0) {
if (sub_man.hideSearchTemplates()) {
List<Subscription> filtered = new ArrayList<>();
for (Subscription s : subs) {
if (!s.isSearchTemplate()) {
filtered.add(s);
}
}
if (filtered.size() > 0) {
result.add(new SubscriptionDownloadDetails(dm, filtered.toArray(new Subscription[filtered.size()])));
}
} else {
result.add(new SubscriptionDownloadDetails(dm, subs));
}
}
} catch (Throwable e) {
}
}
}
return (result.toArray(new SubscriptionDownloadDetails[result.size()]));
}
Aggregations