Package com.aelitis.azureus.core.diskmanager.file

Examples of com.aelitis.azureus.core.diskmanager.file.FMFileManagerException


         
      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();
     
      String  users = "";
       
      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++;
          }
         
          users += (users.length()==0?"":",") + this_owner.getName() + " [write]";

        }else{
         
          read_access++;
         
          users += (users.length()==0?"":",") + this_owner.getName() + " [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 +"'"));
      }
     
    }finally{
     
      file_map_mon.exit();
View Full Code Here


         
          System.out.println( "created " + created_dirs.get(i));
        }
        */
      }else{
            throw( new FMFileManagerException( "Failed to create parent directory '" + parent + "'"))
          }
        }
  }
View Full Code Here

     
      return( raf.length());
     
    }catch( Throwable e ){
     
      throw( new FMFileManagerException( "getLength fails", e ));
    }
  }
View Full Code Here

     
      raf.setLength( length );
     
    }catch( Throwable e ){
     
      throw( new FMFileManagerException( "setLength fails", e ));
    }
  }
View Full Code Here

 
    throws FMFileManagerException
  {
    if (raf == null){
     
      throw new FMFileManagerException( "read: raf is null" );
    }
   
    FileChannel fc = raf.getChannel();
       
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }

    AEThread2.setDebug( owner );
   
    try{
      if(USE_MMAP)
      {
        long remainingInFile = fc.size()-offset;
        long remainingInTargetBuffer = buffer.remaining(DirectByteBuffer.SS_FILE);
        MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, Math.min(remainingInFile,remainingInTargetBuffer));
        buffer.put(DirectByteBuffer.SS_FILE, buf);
      } else {
        fc.position(offset);
        while (fc.position() < fc.size() && buffer.hasRemaining(DirectByteBuffer.SS_FILE))
          buffer.read(DirectByteBuffer.SS_FILE,fc);       
      }

     
     
    }catch ( Exception e ){
     
      Debug.printStackTrace( e );
     
      throw( new FMFileManagerException( "read fails", e ));
    }
  }
View Full Code Here

 
    throws FMFileManagerException
  {
    if ( raf == null ){
     
      throw new FMFileManagerException( "read: raf is null" );
    }
   
    FileChannel fc = raf.getChannel();
       
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }
   
    AEThread2.setDebug( owner );
   
    int[]  original_positions = new int[buffers.length];
   
    long read_start = SystemTime.getHighPrecisionCounter();
   
    try{
      if(USE_MMAP)
      {
       
        long size = 0;
        for(int i=0;i<buffers.length;i++)
        {
          size += buffers[i].remaining(DirectByteBuffer.SS_FILE);
          original_positions[i] = buffers[i].position(DirectByteBuffer.SS_FILE);
        }
         
        size = Math.min(size, fc.size()-offset);
        MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, size);
        for(DirectByteBuffer b : buffers)
        {
          buf.limit(buf.position()+b.remaining(DirectByteBuffer.SS_FILE));
          b.put(DirectByteBuffer.SS_FILE, buf);
        }
         
       
      } else {
       
        fc.position(offset);
        ByteBuffer[]  bbs = new ByteBuffer[buffers.length];
       
        ByteBuffer  last_bb  = null;
        for (int i=0;i<bbs.length;i++){
          ByteBuffer bb = bbs[i] = buffers[i].getBuffer(DirectByteBuffer.SS_FILE);
          int  pos = original_positions[i] = bb.position();
          if ( pos != bb.limit()){
            last_bb  = bbs[i];
          }
        }
       
        if ( last_bb != null ){
          int    loop      = 0;
         
            // we sometimes read off the end of the file (when rechecking) so
            // bail out if we've completed the read or got to file end
            // a "better" fix would be to prevent the over-read in the first
            // place, but hey, we're just about to release and there may be other
            // instances of this...

          while ( fc.position() < fc.size() && last_bb.hasRemaining()){
            long  read = fc.read( bbs );
            if ( read > 0 ){
              loop  = 0;
            }else{
              loop++;
              if ( loop == READ_RETRY_LIMIT ){
                Debug.out( "FMFile::read: zero length read - abandoning" );
                throw( new FMFileManagerException( "read fails: retry limit exceeded"));
              }
              if ( DEBUG_VERBOSE )
                Debug.out( "FMFile::read: zero length read - retrying" );

              try{
                Thread.sleep( READ_RETRY_DELAY*loop );
              }catch( InterruptedException e ){
                throw( new FMFileManagerException( "read fails: interrupted" ));
              }
             
            }           
          }
        }

       
      }
    }catch ( Throwable e ){
     
      try{
        Debug.out( "Read failed: " + owner.getString() + ": raf open=" + raf.getChannel().isOpen() + ", len=" + raf.length() + ",off=" + offset );
       
      }catch( IOException f ){
      }
     
      Debug.printStackTrace( e );
     
      if ( original_positions != null ){
     
        try{
          for (int i=0;i<original_positions.length;i++){
           
            buffers[i].position( DirectByteBuffer.SS_FILE, original_positions[i] );
           
          }
        }catch( Throwable e2 ){
         
          Debug.out( e2 );
        }
      }
     
      throw( new FMFileManagerException( "read fails", e ));
     
    }finally{
     
      long elapsed_millis = ( SystemTime.getHighPrecisionCounter() - read_start )/1000000;

View Full Code Here

    long          position )
 
    throws FMFileManagerException
  {
    if ( raf == null){
      throw( new FMFileManagerException( "write fails: raf is null" ));
    }
   
    FileChannel fc = raf.getChannel();
   
    if ( !fc.isOpen()){
     
      Debug.out("FileChannel is closed: " + owner.getName());
     
      throw( new FMFileManagerException( "read - file is closed"));
    }

    AEThread2.setDebug( owner );
   
    int[]  original_positions = new int[buffers.length];

    try{
     
      if(USE_MMAP) {
        long size = 0;
        for(int i=0;i<buffers.length;i++)
        {
          size += buffers[i].remaining(DirectByteBuffer.SS_FILE);
          original_positions[i] = buffers[i].position(DirectByteBuffer.SS_FILE);
        }
       
        if(position+size > fc.size())
        {
          fc.position(position+size-1);
          fc.write(ByteBuffer.allocate(1));
          fc.force(true);
        }
         
        MappedByteBuffer buf = fc.map(MapMode.READ_WRITE, position, size);
        for(DirectByteBuffer b : buffers)
          buf.put(b.getBuffer(DirectByteBuffer.SS_FILE));
        buf.force();
      } else {
        long  expected_write   = 0;
        long  actual_write  = 0;
        boolean  partial_write  = false;
       
        if ( DEBUG ){
          for (int i=0;i<buffers.length;i++){
            expected_write += buffers[i].limit(DirectByteBuffer.SS_FILE) - buffers[i].position(DirectByteBuffer.SS_FILE);
          }
        }
       
        fc.position( position );
        ByteBuffer[]  bbs = new ByteBuffer[buffers.length];


        ByteBuffer  last_bb  = null;
        for (int i=0;i<bbs.length;i++){
          ByteBuffer bb = bbs[i] = buffers[i].getBuffer(DirectByteBuffer.SS_FILE);
          int  pos = original_positions[i] = bb.position();
          if ( pos != bb.limit()){
            last_bb  = bbs[i];
          }
        }
       
        if ( last_bb != null ){
          int loop = 0;
         
          while( last_bb.position() != last_bb.limit()){
            long written = fc.write( bbs );
            actual_write += written;
           
            if ( written > 0 ){
              loop  = 0;
              if ( DEBUG ){
                if ( last_bb.position() != last_bb.limit()){
                  partial_write  = true;
                  if ( DEBUG_VERBOSE ){
                    Debug.out( "FMFile::write: **** partial write **** this = " + written + ", total = " + actual_write + ", target = " + expected_write );
                  }
                }
              }
             
            }else{
              loop++;
              if ( loop == WRITE_RETRY_LIMIT ){
                Debug.out( "FMFile::write: zero length write - abandoning" );
                throw( new FMFileManagerException( "write fails: retry limit exceeded"));
              }
               
              if ( DEBUG_VERBOSE )
                Debug.out( "FMFile::write: zero length write - retrying" );
             
              try{
                Thread.sleep( WRITE_RETRY_DELAY*loop );
              }catch( InterruptedException e ){
                throw( new FMFileManagerException( "write fails: interrupted" ));
              }
            }           
          }
        }
       
        if ( DEBUG ){
          if ( expected_write != actual_write ){
            Debug.out( "FMFile::write: **** partial write **** failed: expected = " + expected_write + ", actual = " + actual_write );
            throw( new FMFileManagerException( "write fails: expected write/actual write mismatch" ));
          }
          if ( partial_write && DEBUG_VERBOSE )
            Debug.out( "FMFile::write: **** partial write **** completed ok" );
        }
       
      }

     
    }catch ( Throwable e ){
       
      if ( original_positions != null ){
       
        try{
          for (int i=0;i<original_positions.length;i++){
           
            buffers[i].position( DirectByteBuffer.SS_FILE, original_positions[i] );
           
          }
        }catch( Throwable e2 ){
         
          Debug.out( e2 );
        }
      }
     
      throw( new FMFileManagerException( "write fails", e ));
    }   
  }
View Full Code Here

       
        file_access = new FMFileAccessLinear( owner );
       
      }else{
       
        throw( new FMFileManagerException( "Compact storage not supported: no control file available" ));
      }

    }else{
   
      if ( new File( control_dir, controlFileName ).exists()){
View Full Code Here

        type = target_type;
       
        return;
      }
     
      throw( new FMFileManagerException( "Conversion to/from piece-reorder not supported" ));
    }
   
    File  file = owner.getLinkedFile();
     
    RandomAccessFile raf = null;
   
    boolean  ok  = false;
   
    try{
      FMFileAccess  target_access;
     
      if ( target_type == FMFile.FT_LINEAR ){
       
        target_access = new FMFileAccessLinear( owner );
                 
      }else{
       
        target_access = new FMFileAccessCompact(
                    owner.getOwner().getTorrentFile(),control_dir,
                    controlFileName, 
                    new FMFileAccessLinear( owner ));
      }

      if ( file.exists()){

        raf = new RandomAccessFile( file, FMFileImpl.WRITE_ACCESS_MODE);
       
          // due to the simplistic implementation of compact we only actually need to deal with
          // the last piece of the file (first piece is in the right place already)
       
        FMFileAccessCompact  compact_access;
       
        if ( target_type == FMFile.FT_LINEAR ){
                   
          compact_access = (FMFileAccessCompact)file_access;
           
        }else{
         
           compact_access = (FMFileAccessCompact)target_access;
        }
       
        long  length = file_access.getLength( raf );
       
        long  last_piece_start   = compact_access.getLastPieceStart();
        long  last_piece_length   = compact_access.getLastPieceLength();
       
          // see if we have any potential data for the last piece
       
        if ( last_piece_length > 0 && length > last_piece_start ){
         
          long  data_length = length - last_piece_start;
         
          if ( data_length > last_piece_length ){
           
            Debug.out("data length inconsistent: len=" + data_length + ",limit=" + last_piece_length );
           
            data_length = last_piece_length;
          }
         
          DirectByteBuffer  buffer =
            DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_FILE, (int)data_length );
         
          try{
         
            file_access.read( raf, new DirectByteBuffer[]{ buffer }, last_piece_start );
           
              // see if we need to truncate
           
            if ( target_type == FMFile.FT_COMPACT ){
             
              long  first_piece_length = compact_access.getFirstPieceLength();
             
              long  physical_length = raf.length();
             
              if ( physical_length > first_piece_length ){
           
                raf.setLength( first_piece_length );
              }
            }
           
            buffer.flip( DirectByteBuffer.AL_FILE );
           
            target_access.write( raf, new DirectByteBuffer[]{ buffer }, last_piece_start );
           
          }finally{
           
            buffer.returnToPool();
          }
        }else{
         
            // no last piece, truncate after the first piece
         
          if ( target_type == FMFile.FT_COMPACT ){
           
            long  first_piece_length = compact_access.getFirstPieceLength();
           
            long  physical_length = raf.length();
           
            if ( physical_length > first_piece_length ){
         
              raf.setLength( first_piece_length );
            }
          }
        }
         
        target_access.setLength( raf, length );
       
        target_access.flush();
      }
     
      type    = target_type;
      file_access  = target_access;
 
      ok  = true;
     
    }catch( Throwable e ){
     
      Debug.printStackTrace( e );
     
      throw( new FMFileManagerException( "convert fails", e ));
     
    }finally{
     
      try{
        if ( raf != null ){
         
          try{
            raf.close();
           
          }catch( Throwable e ){
         
              // override original exception if there isn't one
           
            if ( ok ){
             
              ok  = false;
             
              throw( new FMFileManagerException( "convert fails", e ));
            }
          }
        }
      }finally{
       
View Full Code Here

     
      dirt_state = new File( control_dir, control_file ).exists()?DIRT_CLEAN:DIRT_NEVER_WRITTEN;
 
    }catch( Throwable e ){
       
      throw( new FMFileManagerException( "Piece-reorder file init fail", e ));
    }
  }
View Full Code Here

TOP

Related Classes of com.aelitis.azureus.core.diskmanager.file.FMFileManagerException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.