예제 #1
0
  protected static void generateEvidence(IndentWriter writer) {
    writer.println(file_map.size() + " FMFile Reservations");

    try {
      writer.indent();

      try {
        file_map_mon.enter();

        Iterator it = file_map.keySet().iterator();

        while (it.hasNext()) {

          String key = (String) it.next();

          List owners = (List) file_map.get(key);

          Iterator it2 = owners.iterator();

          String str = "";

          while (it2.hasNext()) {

            Object[] entry = (Object[]) it2.next();

            FMFileOwner owner = (FMFileOwner) entry[0];
            Boolean write = (Boolean) entry[1];
            String reason = (String) entry[2];

            str +=
                (str.length() == 0 ? "" : ", ")
                    + owner.getName()
                    + "["
                    + (write.booleanValue() ? "write" : "read")
                    + "/"
                    + reason
                    + "]";
          }

          writer.println(Debug.secretFileName(key) + " -> " + str);
        }
      } finally {

        file_map_mon.exit();
      }

      FMFileManagerImpl.generateEvidence(writer);

    } finally {

      writer.exdent();
    }
  }
예제 #2
0
  private void reserveFile() throws FMFileManagerException {

    if (clone) {

      return;
    }

    try {
      file_map_mon.enter();

      // System.out.println( "FMFile::reserveFile:" + canonical_path + "("+ owner.getName() + ")" +
      // " - " + Debug.getCompressedStackTrace() );

      List owners = (List) file_map.get(canonical_path);

      if (owners == null) {

        owners = new ArrayList();

        // System.out.println( "    creating new owners entr" );

        file_map.put(canonical_path, owners);
      }

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

          // already present, start off read-access

          Debug.out("reserve file - entry already present");

          entry[1] = new Boolean(false);

          return;
        }
      }

      owners.add(new Object[] {owner, new Boolean(false), "<reservation>"});

    } finally {

      file_map_mon.exit();
    }
  }
예제 #3
0
  protected void deleteDirs() {
    if (clone) {

      return;
    }

    if (created_dirs_leaf != null) {

      // delete any dirs we created if the target file doesn't exist

      if (!created_dirs_leaf.exists()) {

        Iterator it = created_dirs.iterator();

        while (it.hasNext()) {

          File dir = (File) it.next();

          if (dir.exists() && dir.isDirectory()) {

            File[] entries = dir.listFiles();

            if (entries == null || entries.length == 0) {

              // System.out.println( "deleted " + dir );

              dir.delete();

            } else {

              break;
            }
          } else {

            break;
          }
        }
      }

      created_dirs_leaf = null;
      created_dirs = null;
    }
  }
예제 #4
0
  private void releaseFile() {
    if (clone) {

      return;
    }

    try {
      file_map_mon.enter();

      // System.out.println( "FMFile::releaseFile:" + canonical_path + "("+ owner.getName() + ")" +
      // " - " + Debug.getCompressedStackTrace());

      List owners = (List) file_map.get(canonical_path);

      if (owners != null) {

        for (Iterator it = owners.iterator(); it.hasNext(); ) {

          Object[] entry = (Object[]) it.next();

          if (owner.getName().equals(((FMFileOwner) entry[0]).getName())) {

            it.remove();

            break;
          }
        }

        if (owners.size() == 0) {

          file_map.remove(canonical_path);
        }
      }
    } finally {

      file_map_mon.exit();
    }
  }
  protected long allocateConnectionId(String client_address) {
    try {
      random_mon.enter();

      long id = random.nextLong();

      Long new_key = new Long(id);

      connectionData new_data = new connectionData(client_address, id);

      // check for timeouts

      if (new_data.getTime() - last_timeout_check > 500) {

        last_timeout_check = new_data.getTime();

        Iterator<Long> it = connection_id_map.keySet().iterator();

        while (it.hasNext()) {

          Long key = it.next();

          connectionData data = connection_id_map.get(key);

          if (new_data.getTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

            // System.out.println( "TRTrackerServerProcessorUDP: connection id timeout" );

            it.remove();

            List<connectionData> cds = connection_ip_map.get(client_address);

            if (cds != null) {

              Iterator<connectionData> it2 = cds.iterator();

              while (it2.hasNext()) {

                if (it2.next().getID() == key) {

                  it2.remove();

                  break;
                }
              }

              if (cds.size() == 0) {

                connection_ip_map.remove(client_address);
              }
            }

          } else {
            // insertion order into map is time based - LinkedHashMap returns keys in same order

            break;
          }
        }
      }

      List<connectionData> cds = connection_ip_map.get(client_address);

      if (cds == null) {

        cds = new ArrayList<connectionData>();

        connection_ip_map.put(client_address, cds);
      }

      cds.add(new_data);

      if (cds.size() > 512) {

        connectionData dead = cds.remove(0);

        connection_id_map.remove(dead.getID());
      }

      connection_id_map.put(new_key, new_data);

      // System.out.println( "TRTrackerServerProcessorUDP: allocated:" + id + ", connection id map
      // size = " + connection_id_map.size());

      return (id);

    } finally {

      random_mon.exit();
    }
  }
예제 #6
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();
    }
  }