@Override
  public String handleCommand(String aSessionId, String anInput) {
    if (anInput.startsWith(Command.FILE.name())) {
      // just get the pipe protocol to make sure it is there and to add the listener to it
      try {
        getPipeProtocol();

        String theFileName = anInput.substring(Command.FILE.name().length() + 1);

        if (myFileHandler == null) {
          return Response.REFUSED.name();
        }

        File theFile = myFileHandler.acceptFile(theFileName);

        if (theFile == null) {
          return Response.REFUSED.name();
        }

        FileStatus theStatus = new FileStatus(theFileName, theFile);

        UUID theUID = UUID.randomUUID();
        myMapping.put(theUID, theStatus);

        return Response.ACCEPTED.name() + " " + theUID.toString();
      } catch (ProtocolException e) {
        return ProtocolContainer.Response.UNKNOWN_PROTOCOL.name();
      }
    } else if (anInput.startsWith(Command.WAIT_FOR_FILE.name())) {
      String[] theFileAttributes = anInput.split(" ");
      String theFileId = theFileAttributes[1];
      long theFileSize = Long.parseLong(theFileAttributes[2]);
      UUID theFileUUID = UUID.fromString(theFileId);
      if (!myMapping.containsKey(theFileUUID)) {
        return Response.FILE_NOK.name();
      } else {
        try {
          FileStatus theFileStatus = myMapping.get(theFileUUID);
          FileStatus.Status theStatus = theFileStatus.waitForStatus();
          if (theStatus == FileStatus.Status.OK) {
            // check the file size
            long theRealLength = theFileStatus.getFile().length();
            if (theRealLength == theFileSize) {
              return Response.FILE_OK.name();
            } else {
              return Response.BAD_FILE_SIZE.name();
            }
          } else {
            return Response.FILE_NOK.name();
          }
        } catch (InterruptedException e) {
          return Response.FILE_NOK.name();
        }
      }
    }

    return Response.UNKNOWN_COMMAND.name();
  }
 @TestOnly
 public void assertAllDirtyScopesAreNull(@NotNull Document document) {
   synchronized (myDocumentToStatusMap) {
     FileStatus status = myDocumentToStatusMap.get(document);
     assert status != null
             && !status.defensivelyMarked
             && status.wolfPassFinished
             && status.allDirtyScopesAreNull()
         : status;
   }
 }
  public boolean allDirtyScopesAreNull(@NotNull Document document) {
    synchronized (myDocumentToStatusMap) {
      PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
      if (!ProblemHighlightFilter.shouldHighlightFile(file)) return true;

      FileStatus status = myDocumentToStatusMap.get(document);
      return status != null
          && !status.defensivelyMarked
          && status.wolfPassFinished
          && status.allDirtyScopesAreNull();
    }
  }
 public void setErrorFoundFlag(
     @NotNull Project project, @NotNull Document document, boolean errorFound) {
   // GHP has found error. Flag is used by ExternalToolPass to decide whether to run or not
   synchronized (myDocumentToStatusMap) {
     FileStatus status = myDocumentToStatusMap.get(document);
     if (status == null) {
       if (!errorFound) return;
       status = new FileStatus(project);
       myDocumentToStatusMap.put(document, status);
     }
     status.errorFound = errorFound;
   }
 }
 public void markFileScopeDirty(
     @NotNull Document document, @NotNull TextRange scope, int fileLength) {
   assertAllowModifications();
   if (LOG.isDebugEnabled()) {
     LOG.debug("********************************* Mark dirty: " + scope);
   }
   synchronized (myDocumentToStatusMap) {
     FileStatus status = myDocumentToStatusMap.get(document);
     if (status == null) return; // all dirty already
     if (status.defensivelyMarked) {
       status.defensivelyMarked = false;
     }
     status.combineScopesWith(scope, fileLength, document);
   }
 }
 public void markFileUpToDate(@NotNull Document document, int passId) {
   synchronized (myDocumentToStatusMap) {
     FileStatus status = myDocumentToStatusMap.get(document);
     if (status == null) {
       status = new FileStatus(myProject);
       myDocumentToStatusMap.put(document, status);
     }
     status.defensivelyMarked = false;
     if (passId == Pass.WOLF) {
       status.wolfPassFinished = true;
     } else if (status.dirtyScopes.containsKey(passId)) {
       status.setDirtyScope(passId, null);
     }
   }
 }
 public void markFileScopeDirtyDefensively(@NotNull PsiFile file) {
   assertAllowModifications();
   if (LOG.isDebugEnabled()) {
     LOG.debug("********************************* Mark dirty file defensively: " + file.getName());
   }
   // mark whole file dirty in case no subsequent PSI events will come, but file requires
   // rehighlighting nevertheless
   // e.g. in the case of quick typing/backspacing char
   synchronized (myDocumentToStatusMap) {
     Document document = PsiDocumentManager.getInstance(myProject).getCachedDocument(file);
     if (document == null) return;
     FileStatus status = myDocumentToStatusMap.get(document);
     if (status == null) return; // all dirty already
     status.defensivelyMarked = true;
   }
 }
示例#8
0
  /**
   * 文件夹转换成一维数组
   *
   * @return
   */
  public static String[] dirToDimArr(String input_dir) throws Exception {
    File indir = new File(input_dir);
    File[] infiles = indir.listFiles();
    ArrayList<String> dataList = new ArrayList<String>();
    String[] sing_arr = null;
    String file_name = "";
    for (int i = 0; i < infiles.length; i++) {

      if (!(FileStatus.isRegularFile(infiles[i]))) {
        continue;
      }
      System.out.println(infiles[i].toString());
      sing_arr = fileToDimArr(infiles[i]);
      if (sing_arr == null) {
        continue;
      }
      System.out.println("sing_arr.length:" + sing_arr.length);
      for (int j = 0; j < sing_arr.length; j++) {
        dataList.add(sing_arr[j]);
      }
    }

    String[] dda = new String[dataList.size()];
    for (int i = 0; i < dda.length; i++) {
      dda[i] = dataList.get(i);
    }

    return dda;
  }
 /**
  * @return null for processed file, whole file for untouched or entirely dirty file, range(usually
  *     code block) for dirty region (optimization)
  */
 @Nullable
 public TextRange getFileDirtyScope(@NotNull Document document, int passId) {
   synchronized (myDocumentToStatusMap) {
     PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
     if (!ProblemHighlightFilter.shouldHighlightFile(file)) return null;
     FileStatus status = myDocumentToStatusMap.get(document);
     if (status == null) {
       return file == null ? null : file.getTextRange();
     }
     if (status.defensivelyMarked) {
       status.markWholeFileDirty(myProject);
       status.defensivelyMarked = false;
     }
     LOG.assertTrue(status.dirtyScopes.containsKey(passId), "Unknown pass " + passId);
     RangeMarker marker = status.dirtyScopes.get(passId);
     return marker == null
         ? null
         : marker.isValid()
             ? TextRange.create(marker)
             : new TextRange(0, document.getTextLength());
   }
 }
示例#10
0
  /**
   * Returns the number of blocks this file/directory consists of assuming the file system's
   * standard block size.
   *
   * @param file the file
   * @return the number of block's thie file/directory consists of
   * @throws IOException
   */
  public int getNumberOfBlocks(final FileStatus file) throws IOException {

    int numberOfBlocks = 0;

    if (file == null) {
      return 0;
    }

    // For a file, this is easy
    if (!file.isDir()) {
      return getNumberOfBlocks(file.getLen(), file.getBlockSize());
    }

    // file is a directory
    final FileStatus[] files = this.listStatus(file.getPath());
    for (int i = 0; i < files.length; i++) {

      if (!files[i].isDir()) {
        numberOfBlocks += getNumberOfBlocks(files[i].getLen(), files[i].getBlockSize());
      }
    }

    return numberOfBlocks;
  }
示例#11
0
 /**
  * Copies a tool binary from one path to another, returning the target path. The directory of the
  * target path must already exist. The target copy's time is set to match, as well as its
  * read-only and executable flags. The operation is skipped if the target file has the same time
  * and size as the source.
  */
 public static Path copyTool(Path source, Path target) throws IOException {
   FileStatus sourceStat = null;
   FileStatus targetStat = target.statNullable();
   if (targetStat != null) {
     // stat the source file only if we'll need the stat.
     sourceStat = source.stat(Symlinks.FOLLOW);
   }
   if (targetStat == null
       || targetStat.getLastModifiedTime() != sourceStat.getLastModifiedTime()
       || targetStat.getSize() != sourceStat.getSize()) {
     copyFile(source, target);
     target.setWritable(source.isWritable());
     target.setExecutable(source.isExecutable());
     target.setLastModifiedTime(source.getLastModifiedTime());
   }
   return target;
 }
    @Override
    public void incomingPipe(Pipe aPipe) {
      if (aPipe.getPipeDescription().startsWith("FILE:")) {
        String[] theAttributes = aPipe.getPipeDescription().split(":");

        UUID theFileUUID = UUID.fromString(theAttributes[1]);

        long theLength = Long.parseLong(theAttributes[2]);

        if (!myMapping.containsKey(theFileUUID)) {
          LOGGER.error(
              "The file for file id'" + theAttributes[1] + "' is not stored in file mapping");
          try {
            aPipe.getSocket().close();
          } catch (Exception e) {
          }
        } else {

          FileStatus theFileStatus = myMapping.get(theFileUUID);
          File theFile = theFileStatus.getFile();
          FileOutputStream theOut = null;
          try {
            theOut = new FileOutputStream(theFileStatus.getFile());
            IOTools.copyStream(
                aPipe.getSocket().getInputStream(),
                theOut,
                new FileTransferHandler(theFile, theLength));
            // the file is received, close the socket
            theOut.flush();
            theOut.close();

            myFileHandler.fileSaved(theFile);
            aPipe.getSocket().close();
            theFileStatus.setStatus(FileStatus.Status.OK);
          } catch (Exception e) {
            theFileStatus.setStatus(FileStatus.Status.NOK);
            myMapping.remove(theFileUUID);
            if (theFileStatus.getFile() != null) {
              myFileHandler.fileTransferInterrupted(theFile);
            }

            if (theOut != null) {
              try {
                theOut.close();
              } catch (Exception e2) {
              }
            }
            LOGGER.error("An error occured while receiving file", e);
            if (theFile != null && theFile.exists()) {
              theFile.delete();
            }
          } finally {

            if (theOut != null) {
              try {
                theOut.close();
              } catch (Exception e3) {
              }
            }
            try {
              aPipe.getSocket().close();
            } catch (Exception e) {
            }
          }
        }
      }
    }