Search in sources :

Example 21 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method reserveAccess.

private void reserveAccess(String reason) throws FMFileManagerException {
    if (clone) {
        return;
    }
    try {
        file_map_mon.enter();
        // System.out.println( "FMFile::reserveAccess:" + canonical_path + "("+ owner.getName() + ")" + " [" + (access_mode==FM_WRITE?"write":"read") + "]" + " - " + Debug.getCompressedStackTrace());
        List owners = (List) file_map.get(canonical_path);
        Object[] my_entry = null;
        if (owners == null) {
            Debug.out("reserveAccess fail");
            throw (new FMFileManagerException("File '" + canonical_path + "' has not been reserved (no entries), '" + owner.getName() + "'"));
        }
        for (Iterator it = owners.iterator(); it.hasNext(); ) {
            Object[] entry = (Object[]) it.next();
            String entry_name = ((FMFileOwner) entry[0]).getName();
            if (owner.getName().equals(entry_name)) {
                my_entry = entry;
            }
        }
        if (my_entry == null) {
            Debug.out("reserveAccess fail");
            throw (new FMFileManagerException("File '" + canonical_path + "' has not been reserved (not found), '" + owner.getName() + "'"));
        }
        my_entry[1] = Boolean.valueOf(access_mode == FM_WRITE);
        my_entry[2] = reason;
        int read_access = 0;
        int write_access = 0;
        int write_access_lax = 0;
        int write_access_hybrid = 0;
        TOTorrentFile my_torrent_file = owner.getTorrentFile();
        TOTorrent my_torrent = my_torrent_file == null ? null : my_torrent_file.getTorrent();
        byte[] my_v2_hash = null;
        if (my_torrent != null && my_torrent.getTorrentType() == TOTorrent.TT_V1_V2) {
            try {
                my_v2_hash = my_torrent.getFullHash(TOTorrent.TT_V2);
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
        StringBuilder users_sb = owners.size() == 1 ? null : new StringBuilder(128);
        for (Iterator it = owners.iterator(); it.hasNext(); ) {
            Object[] entry = (Object[]) it.next();
            FMFileOwner this_owner = (FMFileOwner) entry[0];
            if (((Boolean) entry[1]).booleanValue()) {
                write_access++;
                TOTorrentFile this_tf = this_owner.getTorrentFile();
                if (my_torrent_file != null && this_tf != null) {
                    if (my_torrent_file.getLength() == this_tf.getLength()) {
                        write_access_lax++;
                    }
                    if (my_v2_hash != null) {
                        TOTorrent this_torrent = this_tf.getTorrent();
                        if (this_torrent != null && this_torrent.getTorrentType() == TOTorrent.TT_V1_V2) {
                            try {
                                if (Arrays.equals(my_v2_hash, this_torrent.getFullHash(TOTorrent.TT_V2))) {
                                    write_access_hybrid++;
                                }
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    }
                }
                if (users_sb != null) {
                    if (users_sb.length() > 0) {
                        users_sb.append(",");
                    }
                    users_sb.append(this_owner.getName());
                    users_sb.append(" [write]");
                }
            } else {
                read_access++;
                if (users_sb != null) {
                    if (users_sb.length() > 0) {
                        users_sb.append(",");
                    }
                    users_sb.append(this_owner.getName());
                    users_sb.append(" [read]");
                }
            }
        }
        if (write_access > 1 || (write_access == 1 && read_access > 0)) {
            if (write_access_hybrid == write_access) {
                return;
            }
            if (!COConfigurationManager.getBooleanParameter("File.strict.locking")) {
                if (write_access_lax == write_access) {
                    return;
                }
            }
            Debug.out("reserveAccess fail");
            throw (new FMFileManagerException("File '" + canonical_path + "' is in use by '" + (users_sb == null ? "eh?" : users_sb.toString()) + "'"));
        }
    } finally {
        file_map_mon.exit();
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) FMFileOwner(com.biglybt.core.diskmanager.file.FMFileOwner) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 22 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method createDirs.

protected void createDirs(File target) throws FMFileManagerException {
    if (clone) {
        return;
    }
    deleteDirs();
    File parent = target.getParentFile();
    if (!parent.exists()) {
        List new_dirs = new ArrayList();
        File current = parent;
        while (current != null && !current.exists()) {
            new_dirs.add(current);
            current = current.getParentFile();
        }
        created_dirs_leaf = target;
        created_dirs = new ArrayList();
        if (FileUtil.mkdirs(parent)) {
            created_dirs = new_dirs;
        /*
				for (int i=created_dirs.size()-1;i>=0;i--){

					System.out.println( "created " + created_dirs.get(i));
				}
				*/
        } else {
            try {
                Thread.sleep(RandomUtils.nextInt(1000));
            } catch (Throwable e) {
            }
            FileUtil.mkdirs(parent);
            if (parent.isDirectory()) {
                created_dirs = new_dirs;
            } else {
                throw (new FMFileManagerException("Failed to create parent directory '" + parent + "'"));
            }
        }
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) FMFile(com.biglybt.core.diskmanager.file.FMFile) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Example 23 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method openSupport.

protected void openSupport(String reason) throws FMFileManagerException {
    if (fa != null) {
        throw (new FMFileManagerException("file already open"));
    }
    reserveAccess(reason);
    try {
        file_access.aboutToOpen();
        fa = FileUtil.newFileAccessor(linked_file, access_mode == FM_READ ? READ_ACCESS_MODE : WRITE_ACCESS_MODE);
        last_modified = 0;
    } catch (FileNotFoundException e) {
        int st = file_access.getStorageType();
        boolean ok = false;
        try {
            linked_file.getParentFile().mkdirs();
            linked_file.createNewFile();
            fa = FileUtil.newFileAccessor(linked_file, access_mode == FM_READ ? READ_ACCESS_MODE : WRITE_ACCESS_MODE);
            last_modified = 0;
            ok = true;
        } catch (Throwable f) {
            Debug.printStackTrace(f);
        }
        if (!ok) {
            Debug.printStackTrace(e);
            throw (new FMFileManagerException("open fails for '" + linked_file.getAbsolutePath() + "'", e));
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        throw (new FMFileManagerException("open fails for '" + linked_file.getAbsolutePath() + "'", e));
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) FileNotFoundException(java.io.FileNotFoundException)

Example 24 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method renameFile.

@Override
public void renameFile(String new_name) throws FMFileManagerException {
    try {
        this_mon.enter();
        length_cache = getLength();
        String new_canonical_path;
        File new_linked_file = FileUtil.newFile(linked_file.getParentFile(), new_name);
        try {
            try {
                new_canonical_path = new_linked_file.getCanonicalPath();
            } catch (IOException ioe) {
                String msg = ioe.getMessage();
                if (msg != null && msg.contains("There are no more files")) {
                    String abs_path = new_linked_file.getAbsolutePath();
                    String error = "Caught 'There are no more files' exception during new_file.getCanonicalPath(). " + "os=[" + Constants.OSName + "], new_file.getPath()=[" + new_linked_file.getPath() + "], new_file.getAbsolutePath()=[" + abs_path + "]. ";
                    // "new_canonical_path temporarily set to [" +abs_path+ "]";
                    Debug.out(error, ioe);
                }
                throw ioe;
            }
        } catch (Throwable e) {
            throw (new FMFileManagerException("getCanonicalPath fails", e));
        }
        if (new_linked_file.exists()) {
            throw (new FMFileManagerException("renameFile fails - file '" + new_canonical_path + "' already exists"));
        }
        boolean was_open = isOpen();
        // full close, this will release any slots in the limited file case
        close();
        if (!linked_file.exists() || linked_file.renameTo(new_linked_file)) {
            linked_file = new_linked_file;
            canonical_path = new_canonical_path;
            reserveFile();
            if (was_open) {
                // ensure open will regain slots in limited file case
                ensureOpen("renameFile target");
            }
        } else {
            try {
                reserveFile();
            } catch (FMFileManagerException e) {
                Debug.printStackTrace(e);
            }
            if (was_open) {
                try {
                    ensureOpen("renameFile recovery");
                } catch (FMFileManagerException e) {
                    Debug.printStackTrace(e);
                }
            }
            throw (new FMFileManagerException("renameFile fails"));
        }
    } finally {
        length_cache = -1;
        this_mon.exit();
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) IOException(java.io.IOException) FMFile(com.biglybt.core.diskmanager.file.FMFile) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Example 25 with FMFileManagerException

use of com.biglybt.core.diskmanager.file.FMFileManagerException in project BiglyBT by BiglySoftware.

the class FMFileImpl method moveFile.

@Override
public void moveFile(File new_unlinked_file, FileUtil.ProgressListener pl) throws FMFileManagerException {
    try {
        this_mon.enter();
        // when a file is being moved we want to prevent other operations from being blocked simply if they want
        // to read the file length
        length_cache = getLength();
        TOTorrentFile tf = owner.getTorrentFile();
        String new_canonical_path;
        File new_linked_file = manager.getFileLink(tf.getTorrent(), tf.getIndex(), new_unlinked_file);
        try {
            try {
                new_canonical_path = new_linked_file.getCanonicalPath();
            } catch (IOException ioe) {
                String msg = ioe.getMessage();
                if (msg != null && msg.contains("There are no more files")) {
                    String abs_path = new_linked_file.getAbsolutePath();
                    String error = "Caught 'There are no more files' exception during new_file.getCanonicalPath(). " + "os=[" + Constants.OSName + "], new_file.getPath()=[" + new_linked_file.getPath() + "], new_file.getAbsolutePath()=[" + abs_path + "]. ";
                    // "new_canonical_path temporarily set to [" +abs_path+ "]";
                    Debug.out(error, ioe);
                }
                throw ioe;
            }
        } catch (Throwable e) {
            throw (new FMFileManagerException("getCanonicalPath fails", e));
        }
        if (new_linked_file.exists()) {
            throw (new FMFileManagerException("moveFile fails - file '" + new_canonical_path + "' already exists"));
        }
        boolean was_open = isOpen();
        // full close, this will release any slots in the limited file case
        close();
        createDirs(new_linked_file);
        if (!linked_file.exists() || FileUtil.renameFile(linked_file, new_linked_file, pl)) {
            linked_file = new_linked_file;
            canonical_path = new_canonical_path;
            reserveFile();
            if (was_open) {
                // ensure open will regain slots in limited file case
                ensureOpen("moveFile target");
            }
        } else {
            try {
                reserveFile();
            } catch (FMFileManagerException e) {
                Debug.printStackTrace(e);
            }
            if (was_open) {
                try {
                    ensureOpen("moveFile recovery");
                } catch (FMFileManagerException e) {
                    Debug.printStackTrace(e);
                }
            }
            throw (new FMFileManagerException("moveFile fails"));
        }
    } finally {
        length_cache = -1;
        this_mon.exit();
    }
}
Also used : FMFileManagerException(com.biglybt.core.diskmanager.file.FMFileManagerException) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) IOException(java.io.IOException) FMFile(com.biglybt.core.diskmanager.file.FMFile) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Aggregations

FMFileManagerException (com.biglybt.core.diskmanager.file.FMFileManagerException)35 FMFile (com.biglybt.core.diskmanager.file.FMFile)14 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)8 File (java.io.File)7 IOException (java.io.IOException)7 ByteBuffer (java.nio.ByteBuffer)6 MappedByteBuffer (java.nio.MappedByteBuffer)6 FileChannel (java.nio.channels.FileChannel)6 CacheFileManagerException (com.biglybt.core.diskmanager.cache.CacheFileManagerException)3 LogEvent (com.biglybt.core.logging.LogEvent)3 RandomAccessFile (java.io.RandomAccessFile)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FMFileOwner (com.biglybt.core.diskmanager.file.FMFileOwner)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 FileNotFoundException (java.io.FileNotFoundException)1