Analyze storage directories.
Recover from previous transitions if required.
Perform fs state transition if necessary depending on the namespace info.
Read storage info.

@throws IOException
@return true if the image needs to be saved or false otherwise
551887
public boolean recoverTransitionRead(StartupOption startOpt)
      throws IOException {
    
    FLOG.info("Startup: recovering namenode storage");
    
    assert startOpt != StartupOption.FORMAT : 
      "NameNode formatting should be performed before reading the image";
    
    Collection<File> imageDirs = storage.getImageDirectories();

    // none of the data dirs exist
    if(imageDirs.size() == 0 && startOpt != StartupOption.IMPORT)  
      throw new IOException(
          "All specified directories are not accessible or do not exist.");
    editLog.checkJournals();
    
    storage.setUpgradeManager(namesystem.upgradeManager);
    
    Map<ImageManager, RemoteStorageState> remoteImageStates = Maps.newHashMap();
    Map<JournalManager, RemoteStorageState> remoteJournalStates = Maps.newHashMap();

    List<ImageManager> nonFileImageManagers = getNonFileImageManagers();
    List<JournalManager> nonFileJournalManagers = getNonFileJournalManagers();

    updateRemoteStates(remoteImageStates, remoteJournalStates,
        nonFileImageManagers, nonFileJournalManagers);
    
    // number of non-file storage locations
    int nonFileStorageLocations = nonFileImageManagers.size()
        + nonFileJournalManagers.size();
    
    FLOG.info("Startup: checking storage directory state.");
    // 1. For each data directory calculate its state and 
    // check whether all is consistent before transitioning.
    Map<StorageDirectory, StorageState> dataDirStates = 
             new HashMap<StorageDirectory, StorageState>();
    boolean isFormatted = recoverStorageDirs(startOpt, dataDirStates);
    
    // Recover the non-file storage locations.
    editLog.transitionNonFileJournals(null, false,
        Transition.RECOVER, startOpt);
    imageSet.transitionNonFileImages(null, false, Transition.RECOVER,
        startOpt);

    if (!isFormatted && startOpt != StartupOption.ROLLBACK 
                     && startOpt != StartupOption.IMPORT) {
      for(Entry<StorageDirectory, StorageState> e : dataDirStates.entrySet()) {
        LOG.info("State : " + e.getKey().getCurrentDir() + " state: " +e.getValue());
      }
      throw new IOException("NameNode is not formatted." + dataDirStates);      
    }


    int layoutVersion = storage.getLayoutVersion();
    if (layoutVersion < Storage.LAST_PRE_UPGRADE_LAYOUT_VERSION) {
      NNStorage.checkVersionUpgradable(storage.getLayoutVersion());
    }
    if (startOpt != StartupOption.UPGRADE
        && layoutVersion < Storage.LAST_PRE_UPGRADE_LAYOUT_VERSION
        && layoutVersion != FSConstants.LAYOUT_VERSION) {
      throw new IOException(
          "\nFile system image contains an old layout version " 
          + storage.getLayoutVersion() + ".\nAn upgrade to version "
          + FSConstants.LAYOUT_VERSION + " is required.\n"
          + "Please restart NameNode with -upgrade option.");
    }
       
    editLog.updateNamespaceInfo(storage);

    // check whether distributed upgrade is required and/or should be continued
    storage.verifyDistributedUpgradeProgress(startOpt);

    FLOG.info("Startup: formatting unformatted directories.");
    
    // 2. Format unformatted dirs.
    for (Iterator<StorageDirectory> it = storage.dirIterator(); it.hasNext();) {
      StorageDirectory sd = it.next();
      StorageState curState = dataDirStates.get(sd);
      switch(curState) {
      case NON_EXISTENT:
        throw new IOException(StorageState.NON_EXISTENT + 
                              " state cannot be here");
      case NOT_FORMATTED:        
        LOG.info("Storage directory " + sd.getRoot() + " is not formatted.");
        if (!sd.isEmpty()) {
          LOG.error("Storage directory " + sd.getRoot()
            + " is not empty, and will not be formatted! Exiting.");
          throw new IOException(
            "Storage directory " + sd.getRoot() + " is not empty!");
        }   
        LOG.info("Formatting ...");
        sd.clearDirectory(); // create empty currrent dir
        break;
      default:
        break;
      }
    }
    
    // check non-file images
    for (Entry<ImageManager, RemoteStorageState> e : remoteImageStates
        .entrySet()) {
      checkAllowedNonFileState(e.getValue().getStorageState(), e.getKey());
    }
    // check non-file journals
    for (Entry<JournalManager, RemoteStorageState> e : remoteJournalStates
        .entrySet()) {
      checkAllowedNonFileState(e.getValue().getStorageState(), e.getKey());
    }  

    FLOG.info("Startup: Transitions.");
    // 3. Do transitions
    switch(startOpt) {
    case UPGRADE:
      doUpgrade();
      return false; // upgrade saved image already
    case IMPORT:
      doImportCheckpoint();
      if (nonFileStorageLocations > 0) {
        throwIOException("Import not supported for non-file storage");
      }
      return false; // import checkpoint saved image already
    case ROLLBACK:
      doRollback(remoteImageStates, remoteJournalStates);
      // Update the states since the remote states have changed after rollback.
      updateRemoteStates(remoteImageStates, remoteJournalStates,
          nonFileImageManagers, nonFileJournalManagers);
      InjectionHandler.processEvent(InjectionEvent.FSIMAGE_ROLLBACK_DONE);
      break;
    case REGULAR:
      // just load the image
    }
    
    if (inUpgradeStatus()) {
      namesystem.setUpgradeStartTime(FSNamesystem.now());
    }
    
    // final consistency check for non-file images and journals
    // read version file first
    FSImageStorageInspector inspector = storage.readAndInspectDirs();
    
    FLOG.info("Startup: starting with storage info: " + storage.toColonSeparatedString());
    
    // format unformatted journals and images
    // check if the formatted ones are consistent with local storage
    for (Entry<ImageManager, RemoteStorageState> e : remoteImageStates
        .entrySet()) {
      if (e.getValue().getStorageState() != StorageState.NORMAL) {
        LOG.info("Formatting remote image: " + e.getKey());
        e.getKey().transitionImage(storage, Transition.FORMAT, null);
      } else {
        checkConsistency(e.getValue().getStorageInfo(), storage, true, e.getKey());
      }
    } 
    for (Entry<JournalManager, RemoteStorageState> e : remoteJournalStates
        .entrySet()) {
      if (e.getValue().getStorageState() != StorageState.NORMAL) {
        LOG.info("Formatting remote journal: " + e.getKey());
        e.getKey().transitionJournal(storage, Transition.FORMAT, null);
      } else {
        checkConsistency(e.getValue().getStorageInfo(), storage, true, e.getKey());
      }
    }

    // load the image
    return loadFSImage(inspector);
  }
538473
private boolean checkMultiReadBuffer() throws IOException {
        //first check existing buffer
        if (null != this.buffer) {
            if (this.buffer.hasRemaining()) {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "checkMultiReadBuffer, remaining ->" + this);
                }
                return true;
            }
            if (firstReadCompleteforMulti) { // multiRead enabled and subsequent read
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "checkMultiReadBuffer, buffer is completely read, ready this buffer for the subsequent read ->" + this);
                }
                postDataBuffer.get(postDataIndex).flip(); // make position 0 , to read it again from start
                postDataIndex++;
            } else {
                this.buffer.release();
            }
            this.buffer = null;
        }
        // no buffer read from store or first read of buffer
        if (firstReadCompleteforMulti) {
            // first read from what we have stored, if need more than go to channel
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "checkMultiReadBuffer ,index ->" + postDataIndex + " ,storage.size ->" + postDataBuffer.size());
            }
            if (postDataBuffer.size() <= postDataIndex) {
                //get remaining from channel now as read needs more than the stored
                readRemainingFromChannel();
            }
            if (postDataBuffer.size() > postDataIndex) {
                this.buffer = postDataBuffer.get(postDataIndex);
            }

            if (null != this.buffer) {
                // record the new amount of data read from the store
                this.bytesReadFromStore += this.buffer.remaining();
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "checkMultiReadBuffer ->" + this);
                }
                return true;
            }
        } else { // multiRead enabled and first read
            if (getBufferFromChannel()) {
                // store the channel buffer
                postDataBuffer.add(postDataIndex, this.buffer.duplicate());
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "checkMultiReadBuffer, buffer ->" + postDataBuffer.get(postDataIndex)
                                 + " ,buffersize ->" + postDataBuffer.size() + " ,index ->" + postDataIndex);
                }
                postDataIndex++;

                // record the new amount of data read from the channel
                this.bytesRead += this.buffer.remaining();
                return true;
            }
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "checkMultiReadBuffer:  no more buffer ->" + this);
        }
        return false;
    }
342581
@Override
    public boolean isDirectory() {
        final ArchivePath archivePath = ArchivePaths.create(path.toString());
        Node node = archive.get(archivePath);
        return node.getAsset() == null;
    }
1024731
public function afterSave()
    {
        if (!$this->files) {
            return true;
        }
        $oldFileCount = $this->getFileCount();
        /**
         * @var UploadedFile $file
         */
        foreach ($this->files as $key => $file) {
            $this->fileNumber = $oldFileCount + $key + 1;
            $this->fileName = $this->fileNumber . '_' . $file->name;
            if ($this->fileNumber == 1) {
                $this->owner->updateAttributes([$this->fileField => $this->fileName]);
            }
            $path = $this->getFilePath(null, $this->fileName);
            $this->createFilePath($path);
            $file->saveAs($path);
            foreach ($this->imageSettings as $name => $options) {
                $this->processImage($name, $options);
            }    
        }
    }
246510
void inspectStorageDirs(FSImageStorageInspector inspector)
      throws IOException {

    // Process each of the storage directories to find the pair of
    // newest image file and edit file
    for (Iterator<StorageDirectory> it = dirIterator(); it.hasNext();) {
      StorageDirectory sd = it.next();
      inspector.inspectDirectory(sd);
    }
  }
549094
public synchronized boolean update()
        throws IOException
    {
        if (_resource!=null && !_resource.exists())
        {
            clear();
            return true;
        }
        
        long lm=_resource.lastModified();
        
        if (lm==_lastModified && (_buf!=null || _list!=null))
            return false;
        _lastModified=lm;
        
        if (_resource.isDirectory())
            _list=_resource.list();

        if (_list==null)
        {
            int l=(int)_resource.length();
            if (l<0)
                l=1024;
            ByteArrayOutputStream2 bout = new ByteArrayOutputStream2(l);
            InputStream in = _resource.getInputStream();
            try{IO.copy(in,bout);} finally {in.close();}
            _buf=bout.getBuf();
            if (_buf.length!=l)
                _buf=bout.toByteArray();
        }
        return true;
    }
1016703
public boolean exists() {
		try {
			read();
			return true;
		} catch (FileFormatException e) {
			return false;
		} catch (IOException e) {
			logger.error(e);
			return false;
		}
	}
1206892
public boolean isEmpty() throws IOException {
    Closer closer = Closer.create();
    try {
      InputStream in = closer.register(openStream());
      return in.read() == -1;
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
777699
@Deprecated
  public boolean recoverLease(Path f) throws IOException {
    return dfs.recoverLease(getPathName(f), false);
  }
237992
public boolean isDirectory() {
        if (this.file != null) {
            return this.file.isDirectory();
        }

        if (this.header != null) {
            if (this.header.linkFlag == TarHeader.LF_DIR) {
                return true;
            }

            if (this.header.name.toString().endsWith("/")) {
                return true;
            }
        }

        return false;
    }
140386
private void getNextFileToRead() throws IOException {
    if (this.currentFile != null && this.currentFileItr != null) {
      closeCurrentFile();
      incrementBytesReadCounter();
    }

    while (!this.hasNext && !this.filesToPull.isEmpty()) {
      this.currentFile = this.filesToPull.remove(0);
      this.currentFileItr = downloadFile(this.currentFile);
      this.hasNext = this.currentFileItr == null ? false : this.currentFileItr.hasNext();
      LOG.info("Will start downloading file: " + this.currentFile);
    }
  }
518832
protected boolean fileExists()
    {
        if (tc.isEntryEnabled())
            Tr.entry(tc, "fileExists", this);

        boolean fileAlreadyExists = true;

        File file = new File(_logDirectory, _fileName);
        fileAlreadyExists = (file.exists() && (file.length() > 0));

        if (tc.isEntryEnabled())
            Tr.exit(tc, "fileExists", new Boolean(fileAlreadyExists));
        return fileAlreadyExists;
    }
544776
public boolean read()
    {
        if (state == State.READING_FRAME_SIZE)
        {
            // try to read the frame size completely
            if (!internalRead(frameSizeBuffer))
                return false;

            // if the frame size has been read completely, then prepare to read the
            // actual frame.
            if (frameSizeBuffer.remaining() == 0)
            {
                // pull out the frame size as an integer.
                int frameSize = frameSizeBuffer.getInt(0);

                if (frameSize <= 0)
                {
                    logger.error("Read an invalid frame size of " + frameSize + ". Are you using TFramedTransport on the client side?");
                    return false;
                }

                if (frameSize > thriftFactories.maxFrameSizeInBytes)
                {
                    logger.error("Invalid frame size got (" + frameSize + "), maximum expected " + thriftFactories.maxFrameSizeInBytes);
                    return false;
                }


                // reallocate to match frame size (if needed)
                reallocateDataBuffer(frameSize);

                frameSizeBuffer.clear(); // prepare it to the next round of reading (if any)

                state = State.READING_FRAME;
            }
            else
            {
                // this skips the check of READING_FRAME state below, since we can't
                // possibly go on to that state if there's data left to be read at
                // this one.
                state = State.READY_TO_READ_FRAME_SIZE;
                return true;
            }
        }

        // it is possible to fall through from the READING_FRAME_SIZE section
        // to READING_FRAME if there's already some frame data available once
        // READING_FRAME_SIZE is complete.

        if (state == State.READING_FRAME)
        {
            if (!internalRead(dataBuffer))
                return false;

            state = (dataBuffer.remaining() == 0)
                     ? State.READ_FRAME_COMPLETE
                     : State.READY_TO_READ_FRAME;

            // Do not read until we finish processing request.
            if (state == State.READ_FRAME_COMPLETE)
            {
                switchMode(State.READ_FRAME_COMPLETE);
            }

            return true;
        }

        // if we fall through to this point, then the state must be invalid.
        logger.error("Read was called but state is invalid (" + state + ")");
        return false;
    }
1226091
public boolean markedForStop() throws IOException {
        if (stopFile.exists()) {
            try {
                stopFile.delete();
            } catch (Exception e) {
            }
            return true;
        }
        return false;
    }
825318
public boolean hasNextValue() throws IOException {
        if (_parser == null) {
            return false;
        }
        JsonToken t = _parser.getCurrentToken();
        if (t == null) { // un-initialized or cleared; find next
            t = _parser.nextToken();
            // If EOF, no more
            if (t == null) {
                _parser.close();
                return false;
            }
            // And similarly if we hit END_ARRAY; except that we won't close parser
            if (t == JsonToken.END_ARRAY) {
                return false;
            }
        }
        return true;
    }
1301400
public boolean abandonFile(String src, String holder
      ) throws IOException {
    writeLock();
    try {
      if (NameNode.stateChangeLog.isDebugEnabled()) {
        NameNode.stateChangeLog.debug("FILE* NameSystem.abandonFile: " + src);
      }
      if (isInSafeMode()) {
        throw new SafeModeException("Cannot abandon file " + src, safeMode);
      }
      checkLease(src, holder);
      internalReleaseLeaseOne(leaseManager.getLease(holder), src);
      if (NameNode.stateChangeLog.isDebugEnabled()) {
        NameNode.stateChangeLog.debug("FILE* NameSystem.abandonFile: "
                                        + " has been scheduled for lease recovery");
      }
      return true;
    } finally {
      writeUnlock();
    }
  }
970507
protected boolean isRarArchive(URL url)
   {
      if (url == null)
         return false;

      return isRarFile(url) || isRarDirectory(url);
   }