Ejemplo n.º 1
0
  protected void setControlFile() {
    TOTorrentFile tf = owner.getOwner().getTorrentFile();

    if (tf == null) {

      controlFileName = null;
      control_dir = null;

    } else {

      TOTorrent torrent = tf.getTorrent();

      TOTorrentFile[] files = torrent.getFiles();

      int file_index = -1;

      for (int i = 0; i < files.length; i++) {

        if (files[i] == tf) {

          file_index = i;

          break;
        }
      }

      if (file_index == -1) {

        Debug.out("File '" + owner.getName() + "' not found in torrent!");

        controlFileName = null;
        control_dir = null;

      } else {

        control_dir = owner.getOwner().getControlFileDir();
        controlFileName = StringInterner.intern("fmfile" + file_index + ".dat");
      }
    }
  }
Ejemplo n.º 2
0
  public static void generate_preview_for_torrent(TOTorrent created, File file)
      throws TOTorrentException {

    logger.finer("generating preview..2.");
    try {
      File largestFile = null;
      long largest = 0;
      for (TOTorrentFile f : created.getFiles()) {
        if (f == null) {
          continue;
        }

        if (InOrderType.getType(f.getRelativePath()) == null) {
          continue;
        }
        if (f.getLength() > largest) {
          largest = f.getLength();
          if (file.isDirectory() == false) {
            largestFile = new File(file.getParent(), f.getRelativePath());
          } else {
            largestFile = new File(file, f.getRelativePath());
          }
        }
      }

      if (largestFile != null) {
        logger.finer("largest is: " + largestFile.getAbsolutePath());
      }

      try {
        FFMpegAsyncOperationManager.getInstance()
            .getPreviewImage(created.getHash(), largestFile, 10, TimeUnit.SECONDS);
      } catch (TorrentException e) {
        // this should never happen...
        e.printStackTrace();
      } catch (DataNotAvailableException e) {
        logger.finest("unable to create preview for file: " + largest);
      }
    } catch (NullPointerException e) {
      logger.warning("Preview generation null pointer: " + e.toString());
      e.printStackTrace();
    }
  }
Ejemplo n.º 3
0
  public static boolean generate_audio_info_xml(
      File saveLocation, TOTorrent inTorrent, File metaFile) {

    Map<String, Properties> audio_file_properties = new HashMap<String, Properties>();

    if (inTorrent.isSimpleTorrent()) {
      Properties p = new Properties();
      InOrderType type = InOrderType.getType(saveLocation.getName());
      if (type != null) {

        if (type.getFileTypeFilter().equals(FileTypeFilter.Audio)) {
          try {
            AudioFile f = AudioFileIO.read(saveLocation);
            Tag tag = f.getTag();

            if (tag != null) {
              AudioHeader audioHeader = f.getAudioHeader();
              setPropsFromTagAndHeader(p, audioHeader, tag);
            }

            if (p.size() > 0) {
              audio_file_properties.put(saveLocation.getName(), p);
            }
          } catch (Exception e) {
            System.err.println("audio tag parse error: " + e.toString());
          }
        }
      }
    } else {
      for (TOTorrentFile torrent_file : inTorrent.getFiles()) {
        Properties p = new Properties();
        audio_file_properties.put(torrent_file.getRelativePath(), p);

        InOrderType type = InOrderType.getType(torrent_file.getRelativePath());
        if (type != null) {

          if (type.getFileTypeFilter().equals(FileTypeFilter.Audio)) {
            try {
              File file = new File(saveLocation, torrent_file.getRelativePath());
              AudioFile f = AudioFileIO.read(file);
              Tag tag = f.getTag();

              if (tag != null) {
                AudioHeader audioHeader = f.getAudioHeader();
                setPropsFromTagAndHeader(p, audioHeader, tag);

                if (p.size() > 0) {
                  audio_file_properties.put(saveLocation.getName(), p);
                }
              }
            } catch (Exception e) {
              System.err.println("audio tag parse error: " + e.toString());
            }
          } // if it's an audio type
        } // if this file has a recognizable type
      } // for over torrent files
    }

    if (audio_file_properties.size() > 0) {
      try {
        XMLEncoder encoder =
            new XMLEncoder(new BufferedOutputStream(new FileOutputStream(metaFile)));
        encoder.writeObject(audio_file_properties);
        encoder.close();
        logger.fine(
            "wrote audio properties xml for: " + (new String(inTorrent.getName(), "UTF-8")));
      } catch (Exception e) {
        try {
          logger.warning(
              "error writing audio properties for: "
                  + new String(inTorrent.getName(), "UTF-8")
                  + " / "
                  + e.toString());
        } catch (UnsupportedEncodingException e1) {
          e1.printStackTrace();
        }
        e.printStackTrace();
        return false;
      }

      return true;
    }
    return false;
  }
Ejemplo n.º 4
0
  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();

        // System.out.println( "    existing entry: " + entry_name );

        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] = new Boolean(access_mode == FM_WRITE);
      my_entry[2] = reason;

      int read_access = 0;
      int write_access = 0;
      int write_access_lax = 0;

      TOTorrentFile my_torrent_file = owner.getTorrentFile();

      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
              && my_torrent_file.getLength() == this_tf.getLength()) {

            write_access_lax++;
          }

          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)) {

        // relax locking if strict is disabled and torrent file is same size

        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();
    }
  }
Ejemplo n.º 5
0
  protected FMFileImpl(FMFileOwner _owner, FMFileManagerImpl _manager, File _file, int _type)
      throws FMFileManagerException {
    owner = _owner;
    manager = _manager;

    TOTorrentFile tf = owner.getTorrentFile();

    linked_file = manager.getFileLink(tf.getTorrent(), tf.getIndex(), _file);

    boolean file_was_created = false;
    boolean file_reserved = false;
    boolean ok = false;

    try {

      try {

        canonical_path = linked_file.getCanonicalPath();
        if (canonical_path.equals(linked_file.getPath())) canonical_path = linked_file.getPath();

      } catch (IOException ioe) {

        String msg = ioe.getMessage();

        if (msg != null && msg.indexOf("There are no more files") != -1) {

          String abs_path = linked_file.getAbsolutePath();

          String error =
              "Caught 'There are no more files' exception during file.getCanonicalPath(). "
                  + "os=["
                  + Constants.OSName
                  + "], file.getPath()=["
                  + linked_file.getPath()
                  + "], file.getAbsolutePath()=["
                  + abs_path
                  + "]. ";

          Debug.out(error, ioe);
        }

        throw ioe;
      }

      createDirs(linked_file);

      reserveFile();

      file_reserved = true;

      file_access = new FMFileAccessController(this, _type);

      ok = true;

    } catch (Throwable e) {

      if (file_was_created) {

        linked_file.delete();
      }

      deleteDirs();

      if (e instanceof FMFileManagerException) {

        throw ((FMFileManagerException) e);
      }

      throw (new FMFileManagerException("initialisation failed", e));

    } finally {

      if (file_reserved && !ok) {

        releaseFile();
      }
    }
  }
Ejemplo n.º 6
0
  public void moveFile(File new_unlinked_file) throws FMFileManagerException {

    try {
      this_mon.enter();

      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.indexOf("There are no more files") != -1) {
            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();

      close(); // full close, this will release any slots in the limited file case

      createDirs(new_linked_file);

      if (!linked_file.exists() || FileUtil.renameFile(linked_file, new_linked_file)) {

        linked_file = new_linked_file;
        canonical_path = new_canonical_path;

        reserveFile();

        if (was_open) {

          ensureOpen("moveFile target"); // ensure open will regain slots in limited file case
        }

      } 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 {

      this_mon.exit();
    }
  }