Esempio n. 1
0
  private void testDeleteFile(BlockWriter writer, String fileName, File baseDir)
      throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest();
    deleteRequest.setBaseDir(baseDir);
    deleteRequest.setFileName(fileName);

    logger.info(String.format("Delete Request Copy: %s", deleteRequest));
    DeleteResponse deleteResponse = writer.deleteFile(deleteRequest);
    logger.info(String.format("Delete Response Copy: %s", deleteResponse));

    if (!deleteResponse.isSuccess()) {
      throw new ErrorDeletingFileException(
          String.format(
              "Error deleting '%s' in '%s'",
              deleteRequest.getFileName(), deleteRequest.getBaseDir()));
    }
  }
Esempio n. 2
0
  private void testCopyFile(
      BlockReader reader,
      String readFileName,
      BlockWriter writer,
      String writeFileName,
      File baseDir)
      throws IOException {
    ReadRequest readRequest = new ReadRequest();
    readRequest.setBaseDir(baseDir);
    readRequest.setFileName(readFileName);
    readRequest.setBlockSize(FileUtils.MIN_BLOCK_SIZE);

    WriteRequest writeRequest = new WriteRequest();
    writeRequest.setBaseDir(baseDir);
    writeRequest.setFileName(writeFileName);

    long offset = 0L;
    while (true) {
      readRequest.setOffset(offset);
      logger.info(String.format("Read Request Copy: %s", readRequest));
      ReadResponse readResponse = reader.readBlock(readRequest);
      logger.info(String.format("Read Response Copy: %s", readResponse));
      if (!readResponse.isSuccess()) {
        throw new ErrorReadingBlockException(
            String.format(
                "Error reading %d bytes '%s' in '%s' at offset of %d",
                readRequest.getBlockSize(),
                readRequest.getFileName(),
                readRequest.getBaseDir(),
                readRequest.getOffset()));
      }

      if (readResponse.getLength() > 0 || readResponse.isEof()) {
        writeRequest.setData(readResponse.getData());
        writeRequest.setOffset(offset);
        writeRequest.setLength(readResponse.getLength());
        writeRequest.setEof(readResponse.isEof());
        logger.info(String.format("Write Request Copy: %s", writeRequest));
        WriteResponse writeResponse = writer.writeBlock(writeRequest);
        logger.info(String.format("Write Response Copy: %s", writeResponse));
        if (!writeResponse.isSuccess()) {
          throw new ErrorWritingBlockException(
              String.format(
                  "Error writing %d bytes '%s' in '%s' at offset of %d",
                  writeRequest.getLength(),
                  writeRequest.getFileName(),
                  writeRequest.getBaseDir(),
                  writeRequest.getOffset()));
        }

        if (writeRequest.getLength() != writeResponse.getLength()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid length while writing what was expected. Exepecting %d but was %d",
                  writeRequest.getLength(), writeResponse.getLength()));
        } else if (readResponse.getLength() != writeResponse.getLength()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid length while writing what was read. Exepecting %d but was %d",
                  readResponse.getLength(), writeResponse.getLength()));
        }

        if (readResponse.getCrc32() != writeResponse.getCrc32()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid CRC while writing what was read. Exepecting %d but was %d",
                  readResponse.getCrc32(), writeResponse.getCrc32()));
        }

        offset += readResponse.getLength();
      }

      if (readResponse.isEof()) {
        break;
      }
    }
  }