Package com.aelitis.azureus.core.diskmanager.cache

Examples of com.aelitis.azureus.core.diskmanager.cache.CacheFile


         
          DMPieceMapEntry piece_entry = pieceList.get(i);
           
          DiskManagerFileInfoImpl  file_info = piece_entry.getFile();
         
          CacheFile  cache_file = file_info.getCacheFile();
         
          if ( cache_file.compareLength( piece_entry.getOffset()) < 0 ){
             
            listener.checkCompleted( request, false );
           
            return;
          }
         
          if ( all_compact ){
           
            int st = cache_file.getStorageType();
           
            if (( st != CacheFile.CT_COMPACT && st != CacheFile.CT_PIECE_REORDER_COMPACT ) || file_info.getNbPieces() <= 2 ){
                   
              all_compact = false;
            }
          }
        }
       
        if ( all_compact ){
       
            // System.out.println( "Piece " + pieceNumber + " is all compact, failing hash check" );
         
          listener.checkCompleted( request, false );
         
          return;
        }
       
      }catch( Throwable e ){
     
          // we can fail here if the disk manager has been stopped as the cache file length access may be being
          // performed on a "closed" (i.e. un-owned) file
       
        listener.checkCancelled( request );

        return;
      }
     
      int this_piece_length = disk_manager.getPieceLength( pieceNumber );

      DiskManagerReadRequest read_request = disk_manager.createReadRequest( pieceNumber, 0, this_piece_length );
     
         try{
           this_mon.enter();
        
        if ( stopped ){
         
          listener.checkCancelled( request );
         
          return;
        }
       
        async_reads++;
          
         }finally{
          
           this_mon.exit();
         }
        
         read_request.setFlush( read_flush );
        
         read_request.setUseCache( !request.isAdHoc());
        
      disk_manager.enqueueReadRequest(
        read_request,
        new DiskManagerReadRequestListener()
        {
          public void
          readCompleted(
            DiskManagerReadRequest   read_request,
            DirectByteBuffer     buffer )
          {
            complete();
           
               try{
                 this_mon.enter();
              
              if ( stopped ){
               
                buffer.returnToPool();
               
                listener.checkCancelled( request );
               
                return;
              }
             
              async_checks++;
                
               }finally{
                
                 this_mon.exit();
               }
           
            try{
                final  DirectByteBuffer  f_buffer  = buffer;
               
                 ConcurrentHasher.getSingleton().addRequest(
                    buffer.getBuffer(DirectByteBuffer.SS_DW),
                  new ConcurrentHasherRequestListener()
                  {
                      public void
                    complete(
                      ConcurrentHasherRequest  hash_request )
                      {
                        int  async_result  = 3; // cancelled
                                               
                        try{
                         
                        byte[] actual_hash = hash_request.getResult();
                                               
                        if ( actual_hash != null ){
                           
                          request.setHash( actual_hash );
                         
                            async_result = 1; // success
                           
                            for (int i = 0; i < actual_hash.length; i++){
                             
                              if ( actual_hash[i] != required_hash[i]){
                               
                                async_result = 2; // failed;
                               
                                break;
                              }
                            }
                        }
                        }finally{
                         
                          try{
                            if ( async_result == 1 ){
                           
                              try{
                                for (int i = 0; i < pieceList.size(); i++) {
                                 
                                  DMPieceMapEntry piece_entry = pieceList.get(i);
                                   
                                  DiskManagerFileInfoImpl  file_info = piece_entry.getFile();
                                 
                                    // edge case here for skipped zero length files that have been deleted
                                 
                                  if ( file_info.getLength() > 0 || !file_info.isSkipped()){

                                    CacheFile  cache_file = file_info.getCacheFile();                   
                                 
                                    cache_file.setPieceComplete( pieceNumber, f_buffer );
                                  }
                                }
                              }catch( Throwable e ){
                               
                                f_buffer.returnToPool();
View Full Code Here


                    close_it    = true;
                }

                try{
                    CacheFile   cache_file  = file_info.getCacheFile();
                    File        data_file   = file_info.getFile(true);

                    if ( !cache_file.exists()){

                            // look for something sensible to report

                          File current = data_file;
View Full Code Here

                    setState( FAULTY );

                    return( -1 );
                }

                CacheFile   cache_file      = fileInfo.getCacheFile();
                File        data_file       = fileInfo.getFile(true);

                String  file_key = data_file.getAbsolutePath();

                if ( Constants.isWindows ){

                    file_key = file_key.toLowerCase();
                }

                if ( file_set.contains( file_key )){

                    this.errorMessage = "File occurs more than once in download: " + data_file.toString();

                    setState( FAULTY );

                    return( -1 );
                }

                file_set.add( file_key );

                String      ext  = data_file.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;
                }

                fileInfo.setExtension(ext.substring(separator));

                    //Added for Feature Request
                    //[ 807483 ] Prioritize .nfo files in new torrents
                    //Implemented a more general way of dealing with it.

                String extensions = COConfigurationManager.getStringParameter("priorityExtensions","");

                if(!extensions.equals("")) {
                    boolean bIgnoreCase = COConfigurationManager.getBooleanParameter("priorityExtensionsIgnoreCase");
                    StringTokenizer st = new StringTokenizer(extensions,";");
                    while(st.hasMoreTokens()) {
                        String extension = st.nextToken();
                        extension = extension.trim();
                        if(!extension.startsWith("."))
                            extension = "." + extension;
                        boolean bHighPriority = (bIgnoreCase) ?
                                              fileInfo.getExtension().equalsIgnoreCase(extension) :
                                              fileInfo.getExtension().equals(extension);
                        if (bHighPriority)
                            fileInfo.setPriority(1);
                    }
                }

                fileInfo.setDownloaded(0);
               
                int st = cache_file.getStorageType();
               
                boolean compact = st == CacheFile.CT_COMPACT || st == CacheFile.CT_PIECE_REORDER_COMPACT;
               
                boolean mustExistOrAllocate = ( !compact ) || RDResumeHandler.fileMustExist(download_manager, fileInfo);
               
                  // delete compact files that do not contain pieces we need
               
                if (!mustExistOrAllocate && cache_file.exists()){
                 
          data_file.delete();
                }
               
                if ( cache_file.exists() ){

                    try {

                        //make sure the existing file length isn't too large

                        long    existing_length = fileInfo.getCacheFile().getLength();

                        ifexisting_length > target_length ){

                            if ( COConfigurationManager.getBooleanParameter("File.truncate.if.too.large")){

                                fileInfo.setAccessMode( DiskManagerFileInfo.WRITE );

                                cache_file.setLength( target_length );

                                fileInfo.setAccessMode( DiskManagerFileInfo.READ );
                               
                                Debug.out( "Existing data file length too large [" +existing_length+ ">" +target_length+ "]: " +data_file.getAbsolutePath() + ", truncating" );
View Full Code Here

                // files for dnd files which then force a recheck when the download
                // starts for the first time)
 
                if ( target_file.exists()){
 
                  CacheFile cache_file =
                    CacheFileManagerFactory.getSingleton().createFile(
                      new CacheFileOwner()
                      {
                        public String
                        getCacheFileOwnerName()
                        {
                          return( download_manager.getInternalName());
                        }
 
                        public TOTorrentFile
                        getCacheFileTorrentFile()
                        {
                          return( res[idx].getTorrentFile() );
                        }
 
                        public File
                        getCacheFileControlFileDir()
                        {
                          return( download_manager.getDownloadState().getStateFile( ));
                        }
                        public int
                        getCacheMode()
                        {
                          return( CacheFileOwner.CACHE_MODE_NORMAL );
                        }
                      },
                      target_file,
                      DiskManagerUtil.convertDMStorageTypeToCache( newStorageType ));
 
                  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] );
 
              // try and maintain reasonable figures for downloaded. Note that because
              // we don't screw with the first and last pieces of the file during
              // storage type changes we don't have the problem of dealing with
              // the last piece being smaller than torrent piece size
 
              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 CacheFile   read_cache_file;
                // do not access this field directly, use lazyGetFile() instead
                private WeakReference dataFile = new WeakReference(null);
 
                public void
                setPriority(int b)
                {
                  priority    = b;
 
                  DiskManagerImpl.storeFilePriorities( download_manager, res );
 
                  listener.filePriorityChanged( this );
                }
 
                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;
                    }
                  }

                  skipped = _skipped;
 
                  DiskManagerImpl.storeFilePriorities( download_manager, res );
                 
                  if(!_skipped)
                  {
                    boolean[] toCheck = new boolean[fileSetSkeleton.nbFiles()];
                    toCheck[file_index] = true;
                    doFileExistenceChecks(fileSetSkeleton, toCheck, download_manager, true);                     
                  }
                 
 
                  listener.filePriorityChanged( this );
                }
 
                public int
                getAccessMode()
                {
                  return( READ );
                }
 
                public long
                getDownloaded()
                {
                  return( downloaded );
                }
 
                public void
                setDownloaded(
                  long    l )
                {
                  downloaded  = l;
                }
 
                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);
                }
 
                public int
                getFirstPieceNumber()
                {
                  return( torrent_file.getFirstPieceNumber());
                }
 
                public int
                getLastPieceNumber()
                {
                  return( torrent_file.getLastPieceNumber());
                }
 
                public long
                getLength()
                {
                  return( torrent_file.getLength());
                }
 
                public int
                getIndex()
                {
                  return( file_index );
                }
 
                public int
                getNbPieces()
                {
                  return( torrent_file.getNumberOfPieces());
                }
 
                public int
                getPriority()
                {
                  return( priority );
                }
 
                public boolean
                isSkipped()
                {
                  return( skipped );
                }
 
                public DiskManager
                getDiskManager()
                {
                  return( null );
                }
 
                public DownloadManager
                getDownloadManager()
                {
                  return( download_manager );
                }
 
                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;
 
                  // for a simple torrent the target file can be changed
 
                  if ( tor.isSimpleTorrent()){
 
                    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;
                }
 
                public TOTorrentFile
                getTorrentFile()
                {
                  return( torrent_file );
                }
               
                  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);
                  }

                  public boolean
                  setLinkAtomic(
                    File    link_destination )
                  {
                    return( setFileLink( download_manager, res, this, lazyGetFile(), link_destination ));
                  }

                  public File
                  getLink()
                  {
                    return( download_manager.getDownloadState().getFileLink( lazyGetFile() ));
                  }

                  public boolean setStorageType(int type) {
                    boolean[] change = new boolean[res.length];
                    change[file_index] = true;
                    return fileSetSkeleton.setStorageTypes(change, type)[file_index];
                  }

                  public int
                  getStorageType()
                  {
                    return( DiskManagerUtil.convertDMStorageTypeFromString( DiskManagerImpl.getStorageType(download_manager, file_index)));
                  }

                  public void
                  flushCache()
                  {
                  }

                  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()
                              {
                                public String
                                getCacheFileOwnerName()
                                {
                                  return( download_manager.getInternalName());
                                }

                                public TOTorrentFile
                                getCacheFileTorrentFile()
                                {
                                  return( torrent_file );
                                }

                                public File
                                getCacheFileControlFileDir()
                                {
                                  return( download_manager.getDownloadState().getStateFile( ));
                                }
                                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 );
                  }

                  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);
                      }
View Full Code Here

  {
      // assumption - they are all for the same file, sequential offsets and aggregatable, not cancelled
   
    int      op         = base_request.getOperation();
     
    CacheFile  file       = base_request.getFile();
    long    offset      = base_request.getOffset();
    short    cache_policy  = base_request.getCachePolicy();
   
    DirectByteBuffer[]  buffers = new DirectByteBuffer[requests.length];
   
    long  current_offset   = offset;
    long  total_size    = 0;
   
    for (int i=0;i<buffers.length;i++){
   
      DiskAccessRequestImpl  request = requests[i];
     
      if ( current_offset != request.getOffset()){
       
        Debug.out( "assert failed: requests not contiguous" );
      }
     
      int  size = request.getSize();
     
      current_offset += size;
     
      total_size += size;
     
      buffers[i] = request.getBuffer();
    }
   
    try
      if ( op == OP_READ ){
       
        file.read( buffers, offset, cache_policy );
       
      }else if ( op == OP_WRITE ){
       
        file.write( buffers, offset );
     
      }else{
       
        file.writeAndHandoverBuffers( buffers, offset );
      }
     
      base_request.getListener().requestExecuted( total_size );

      for (int i=0;i<requests.length;i++){
View Full Code Here

     
                          request = (DiskAccessRequestImpl)requests.remove(0);
                         
                          if ( enable_aggregation ){
                           
                            CacheFile  file = request.getFile();
                           
                            Map  file_map = (Map)request_map.get( file );
                             
                              // it is possible for the file_map to be null here due to
                              // the fact that the entries can be zero sized even though
View Full Code Here

                      pm_info.getTorrentFile(),
                      DiskManagerFileInfo.ST_LINEAR );
       
        local_files[i] = file_info;

        CacheFile  cache_file  = file_info.getCacheFile();
        File    data_file  = file_info.getFile(true);
 
        if ( !cache_file.exists()){
           
          throw( new Exception( "File '" + data_file + "' doesn't exist" ));
        }
       
        if ( cache_file.getLength() != target_length ){
       
          throw( new Exception( "File '" + data_file + "' doesn't exist" ));
       
        }
       
View Full Code Here

TOP

Related Classes of com.aelitis.azureus.core.diskmanager.cache.CacheFile

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.