Пример #1
0
ssize_t HdfsFile::read(void *ptr, size_t count)
{
	size_t offset = tell();
	tSize ret = hdfsRead(m_fs, m_file, ptr, count);
	// see comment in pread
	if( ret < 0 )
	{
		reopen();
		if (hdfsSeek(m_fs, m_file, offset) == 0)
			ret = hdfsRead(m_fs, m_file, ptr, count);
	}
	else if ((size_t) ret < count)  // ret >= 0
	{
		// retry
		int retryCount = 0;
		while (ret >= 0 && ret != (tSize) count && ++retryCount < 40)
		{
			ssize_t n = hdfsRead(m_fs, m_file, (char*)ptr+ret, count-ret);
			if (n >= 0)
				ret += n;
			else
				ret = n;
		}
	}

	if( IDBLogger::isEnabled() )
		IDBLogger::logRW("read", m_fname, this, offset, count, ret);

	return ret;
}
Пример #2
0
qioerr hdfs_readv (void* file, const struct iovec *vector, int count, ssize_t* num_read_out, void* fs)
{
  ssize_t got;
  ssize_t got_total;
  qioerr err_out = 0;
  int i;

  STARTING_SLOW_SYSCALL;

  got_total = 0;
  for( i = 0; i < count; i++) {
    got = hdfsRead(to_hdfs_fs(fs)->hfs, to_hdfs_file(file)->file, (void*)vector[i].iov_base, vector[i].iov_len);
    if( got != -1 ) {
      got_total += got;
    } else {
      err_out = qio_mkerror_errno();
      break;
    }
    if( got != sys_iov_total_bytes(&vector[i], i)) {
      break;
    }
  }

  if( err_out == 0 && got_total == 0 && sys_iov_total_bytes(vector, count) != 0 )
    err_out = qio_int_to_err(EEOF);

  *num_read_out = got_total;

  DONE_SLOW_SYSCALL;

  return err_out;
}
Пример #3
0
static PyObject *pyhdfsFS_read(pyhdfsFS *self, PyObject *args) {

    int n;

    PyArrayObject *result;

    if (!PyArg_ParseTuple(args, "i", &n)) {
        return NULL;
    }
    
    char *buffer = (char*) malloc(n);
	int len = hdfsRead(self->fs, self->file, (void*)buffer, n);
	if (len <= 0)
	{		
		Py_INCREF(Py_None);
		return Py_None;
	}

    int dim[1];
    dim[0] = len;
    result = (PyArrayObject *) PyArray_SimpleNew(1, dim, PyArray_CHAR);
	memcpy(result->data,buffer,len);

	free(buffer);

    return PyArray_Return(result);
}
Пример #4
0
 int read(char * start, size_t size)
 {
     int bytes_read = hdfsRead(fs.get(), fin, start, size);
     if (bytes_read < 0)
         throw Exception("Fail to read HDFS file: " + hdfs_uri.toString() + " " + std::string(hdfsGetLastError()),
             ErrorCodes::NETWORK_ERROR);
     return bytes_read;
 }
int main(int argc, char **argv) {

    const char* rfile;
    tSize fileTotalSize, bufferSize, curSize, totalReadSize;
    hdfsFS fs;
    hdfsFile readFile;
    char *buffer = NULL;
    
    if (argc != 4) {
        fprintf(stderr, "Usage: test_libwebhdfs_read"
                " <filename> <filesize> <buffersize>\n");
        exit(1);
    }
    
    fs = hdfsConnect("localhost", 50070);
    if (!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(1);
    }
    
    rfile = argv[1];
    fileTotalSize = strtoul(argv[2], NULL, 10);
    bufferSize = strtoul(argv[3], NULL, 10);
    
    readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
    if (!readFile) {
        fprintf(stderr, "Failed to open %s for writing!\n", rfile);
        exit(1);
    }
    
    // data to be written to the file
    buffer = malloc(sizeof(char) * bufferSize);
    if(buffer == NULL) {
        fprintf(stderr, "Failed to allocate buffer.\n");
        exit(1);
    }
    
    // read from the file
    curSize = bufferSize;
    totalReadSize = 0;
    for (; (curSize = hdfsRead(fs, readFile, buffer, bufferSize)) == bufferSize; ) {
        totalReadSize += curSize;
    }
    totalReadSize += curSize;
    
    fprintf(stderr, "size of the file: %d; reading size: %d\n",
            fileTotalSize, totalReadSize);
    
    free(buffer);
    hdfsCloseFile(fs, readFile);
    hdfsDisconnect(fs);
    
    return 0;
}
Пример #6
0
int main(int argc, char **argv) {

    if (argc != 3) {
        fprintf(stderr, "Usage: hdfs_read <filename> <buffersize>\n");
        exit(-1);
    }
    
    hdfsFS fs = hdfsConnect("default", 0);
    if (!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(-2);
    } 
 
    const char* rfile = argv[1];
    tSize bufferSize = strtoul(argv[2], NULL, 10);

   
    hdfsFile readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
    if (!readFile) {
        fprintf(stderr, "Failed to open %s for writing!\n", rfile);
        exit(-3);
    }

	FILE *outf = fopen(rfile, "wb");
	if (outf == NULL) {
		printf("FILEIO error %d\n", errno);
		exit(-4);
	}

    // data to be written to the file
    char* buffer = malloc(sizeof(char) * bufferSize);
    if(buffer == NULL) {
        return -5;
    }
    
	//printf("buffersize is %d\n", bufferSize);
    // read from the file
    tSize curSize = bufferSize;
    for (; curSize == bufferSize;) {
		//printf("cursize before is %d\n", curSize);
        curSize = hdfsRead(fs, readFile, (void*)buffer, curSize);
		//printf("cursize is %d, errno is %d\n", curSize, errno);
		fwrite((void *)buffer, sizeof(char), curSize, outf);
		//printf("%.*s", bufferSize, buffer);
    }

	fclose(outf);
    free(buffer);
    hdfsCloseFile(fs, readFile);
    hdfsDisconnect(fs);

    return 0;
}
Пример #7
0
uint32_t THDFSFileTransport::read(uint8_t* buf, uint32_t len) {
  tSize rv = hdfsRead(hdfsFile_->getFS()->getHandle(), (hdfsFile)hdfsFile_->getHandle(), buf, len);
  if (rv < 0) {
    int errno_copy = errno;
    throw TTransportException(TTransportException::UNKNOWN,
                              "THDFSFileTransport::read()",
                              errno_copy);
  } else if (rv == 0) {
    throw TTransportException(TTransportException::END_OF_FILE,
                              "THDFSFileTransport::read()");
  }
  return rv;
}
Пример #8
0
int HDFSFileSplitter::read_block(const std::string& fn) {
    file_ = hdfsOpenFile(fs_, fn.c_str(), O_RDONLY, 0, 0, 0);
    assert(file_ != NULL);
    hdfsSeek(fs_, file_, offset_);
    size_t start = 0;
    size_t nbytes = 0;
    while (start < hdfs_block_size) {
        // only 128KB per hdfsRead
        nbytes = hdfsRead(fs_, file_, data_ + start, hdfs_block_size);
        start += nbytes;
        if (nbytes == 0)
            break;
    }
    return start;
}
Пример #9
0
int main(int argc, char **argv) {

    hdfsFS fs;
    char* rfile;
    int bufferSize;
    hdfsFile readFile;
    char* buffer;
    int curSize;
    
    if (argc != 4) {
        fprintf(stderr, "Usage: hdfs_read <filename> <filesize> <buffersize>\n");
        exit(-1);
    }
    
    fs = hdfsConnect("default", 0);
    if (!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(-1);
    } 
 
    rfile = argv[1];
    bufferSize = strtoul(argv[3], NULL, 10);
   
    readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
    if (!readFile) {
        fprintf(stderr, "Failed to open %s for writing!\n", rfile);
        exit(-2);
    }

    /* data to be written to the file */
    buffer = malloc(sizeof(char) * bufferSize);
    if(buffer == NULL) {
        return -2;
    }
    
    /* read from the file */
    curSize = bufferSize;
    for (; curSize == bufferSize;) {
        curSize = hdfsRead(fs, readFile, (void*)buffer, curSize);
    }
    
    free(buffer);
    hdfsCloseFile(fs, readFile);
    hdfsDisconnect(fs);

    return 0;
}
Пример #10
0
void ClaimsHDFS::claimsRead(){
	hdfsFS fs;
	hdfsFile fd;
	string filename="/home/casa/data/kmeans_data.txt";
	fs=hdfsConnect("10.11.1.174",9000);
	fd=hdfsOpenFile(fs,filename.c_str(),O_RDONLY,0,0,0);
	if(!fd){
		cout<<"failed to open hdfs file!!!"<<endl;
	}

	char array[72];
    tSize bytes=hdfsRead(fs,fd,array,72);
	cout<<"string is: "<<array<<endl;

	hdfsCloseFile(fs,fd);
	hdfsDisconnect(fs);
}
Пример #11
0
int libhdfsconnector::streamInFile(const char * rfile, int bufferSize)
{
    if (!fs)
    {
        fprintf(stderr, "Could not connect to hdfs on");
        return RETURN_FAILURE;
    }

    unsigned long fileTotalSize = 0;

    hdfsFileInfo *fileInfo = NULL;
    if ((fileInfo = hdfsGetPathInfo(fs, rfile)) != NULL)
    {
        fileTotalSize = fileInfo->mSize;
        hdfsFreeFileInfo(fileInfo, 1);
    }
    else
    {
        fprintf(stderr, "Error: hdfsGetPathInfo for %s - FAILED!\n", rfile);
        return RETURN_FAILURE;
    }

    hdfsFile readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0);
    if (!readFile)
    {
        fprintf(stderr, "Failed to open %s for writing!\n", rfile);
        return RETURN_FAILURE;
    }

    unsigned char buff[bufferSize + 1];
    buff[bufferSize] = '\0';

    for (unsigned long bytes_read = 0; bytes_read < fileTotalSize;)
    {
        unsigned long read_length = hdfsRead(fs, readFile, buff, bufferSize);
        bytes_read += read_length;
        for (unsigned long i = 0; i < read_length; i++)
            fprintf(stdout, "%c", buff[i]);
    }

    hdfsCloseFile(fs, readFile);

    return 0;
}
Пример #12
0
bool FileManagerHdfs::readBlockOrBlob(const block_id block,
                                      void *buffer,
                                      const size_t length) {
  DEBUG_ASSERT(buffer);
  DEBUG_ASSERT(length % kSlotSizeBytes == 0);

  string filename(blockFilename(block));

  hdfsFile file_handle = hdfsOpenFile(hdfs_,
                                      filename.c_str(),
                                      O_RDONLY,
                                      kSlotSizeBytes,
                                      FLAGS_hdfs_num_replications,
                                      kSlotSizeBytes);
  if (file_handle == nullptr) {
    LOG_WARNING("Failed to open file " << filename << " with error: " << strerror(errno));
    return false;
  }

  size_t bytes_total = 0;
  while (bytes_total < length) {
    tSize bytes = hdfsRead(hdfs_, file_handle, static_cast<char*>(buffer) + bytes_total, length - bytes_total);
    if (bytes > 0) {
      bytes_total += bytes;
    } else if (bytes == -1) {
      if (errno != EINTR) {
        LOG_WARNING("Failed to read file " << filename << " with error: " << strerror(errno));
        break;
      }
    } else {
      LOG_WARNING("Failed to read file " << filename << " since EOF was reached unexpectedly");
      break;
    }
  }

  if (hdfsCloseFile(hdfs_, file_handle) != 0) {
    LOG_WARNING("Failed to close file " << filename << " with error: " << strerror(errno));
  }

  return (bytes_total == length);
}
Пример #13
0
tSize hdfsReadFully(hdfsFS fs, hdfsFile f, void* buffer, tSize length)
{
    uint8_t *buf = buffer;
    tSize ret, nread = 0;

    while (length > 0) {
        ret = hdfsRead(fs, f, buf, length);
        if (ret < 0) {
            if (errno != EINTR) {
                return -1;
            }
        }
        if (ret == 0) {
            break;
        }
        nread += ret;
        length -= ret;
        buf += ret;
    }
    return nread;
}
Пример #14
0
int main(int argc, char* argv[]) {
  if (argc < 4) {
    printf("usage: hdfs_get <name node address> <name node port> <input file>\n");
    return 1;
  }
  // Sleep for 100ms.
  usleep(100 * 1000);
  struct hdfsBuilder* hdfs_builder = hdfsNewBuilder();
  if (!hdfs_builder) {
    printf("Could not create HDFS builder");
    return 1;
  }
  hdfsBuilderSetNameNode(hdfs_builder, argv[1]);
  int port = atoi(argv[2]);
  hdfsBuilderSetNameNodePort(hdfs_builder, port);
  hdfsBuilderConfSetStr(hdfs_builder, "dfs.client.read.shortcircuit", "false");
  hdfsFS fs = hdfsBuilderConnect(hdfs_builder);
  hdfsFreeBuilder(hdfs_builder);
  if (!fs) {
    printf("Could not connect to HDFS");
    return 1;
  }

  hdfsFile file_in = hdfsOpenFile(fs, argv[3], O_RDONLY, 0, 0, 0);
  char buffer[1048576];
  int done = 0;
  do {
    done = hdfsRead(fs, file_in, &buffer, 1048576);
  } while (done > 0);
  if (done < 0) {
    printf("Failed to read file: %s", hdfsGetLastError());
    return 1;
  }

  hdfsCloseFile(fs, file_in);
  hdfsDisconnect(fs);
  return 0;
}
Пример #15
0
int libhdfsconnector::streamFlatFileOffset(const char * filename, unsigned long seekPos, unsigned long readlen,unsigned long bufferSize, int maxretries)
{
    hdfsFile readFile = hdfsOpenFile(fs, filename, O_RDONLY, 0, 0, 0);
    if (!readFile)
    {
        fprintf(stderr, "Failed to open %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    if (hdfsSeek(fs, readFile, seekPos))
    {
        fprintf(stderr, "Failed to seek %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    unsigned char buffer[bufferSize + 1];

    unsigned long currentPos = seekPos;

    fprintf(stderr, "\n--Start piping: %ld--\n", currentPos);

    unsigned long bytesLeft = readlen;
    while (hdfsAvailable(fs, readFile) && bytesLeft > 0)
    {
        tSize num_read_bytes = hdfsRead(fs, readFile, buffer, bytesLeft < bufferSize ? bytesLeft : bufferSize);
        if (num_read_bytes <= 0)
            break;
        bytesLeft -= num_read_bytes;
        for (int i = 0; i < num_read_bytes; i++, currentPos++)
            fprintf(stdout, "%c", buffer[i]);
    }

    fprintf(stderr, "--\nStop Streaming: %ld--\n", currentPos);

    hdfsCloseFile(fs, readFile);

    return EXIT_SUCCESS;
}
Пример #16
0
boost::string_ref HDFSFileSplitter::fetch_block(bool is_next) {
    int nbytes = 0;
    if (is_next) {
        // directly read the next block using the current file
        nbytes = hdfsRead(fs_, file_, data_, hdfs_block_size);
        if (nbytes == 0)
            return "";
        if (nbytes == -1) {
            throw base::HuskyException("read next block error!");
        }
    } else {
        // Ask the master for a new block
        BinStream question;
        question << url_ << husky::Context::get_param("hostname");
        BinStream answer = husky::Context::get_coordinator()->ask_master(question, husky::TYPE_HDFS_BLK_REQ);
        std::string fn;
        answer >> fn;
        answer >> offset_;

        if (fn == "") {
            // no more files
            return "";
        }

        if (file_ != NULL) {
            int rc = hdfsCloseFile(fs_, file_);
            assert(rc == 0);
            // Notice that "file" will be deleted inside hdfsCloseFile
            file_ = NULL;
        }

        // read block
        nbytes = read_block(fn);
    }
    return boost::string_ref(data_, nbytes);
}
Пример #17
0
 std::streamsize read(char* strm_ptr, std::streamsize n) {
   return hdfsRead(filesystem, file, strm_ptr, n);
 } // end of read
Пример #18
0
/**
 * hdfsCopy - Copy file from one filesystem to another.
 *
 * @param srcFS The handle to source filesystem.
 * @param src The path of source file.
 * @param dstFS The handle to destination filesystem.
 * @param dst The path of destination file.
 * @return Returns 0 on success, -1 on error.
 */
int
hdfsCopy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst)
{
	char *block = NULL, *src_abs, *dst_abs;
	hdfsFileInfo *srcinfo = NULL;
	int res = -1;
	hdfsFile a = NULL, b = NULL;
	tOffset twritten = 0;

	src_abs = _makeabs(srcFS, src);
	dst_abs = _makeabs(dstFS, dst);

	if (hdfsExists(srcFS, src_abs) == -1) {
		ERR(ENOENT, "'%s' doesn't exist on srcFS", src_abs);
		goto out;
	}

	srcinfo = hdfsGetPathInfo(srcFS, src_abs);
	if (!srcinfo) {
		ERR(errno, "hdfsGetPathInfo failed");
		goto out;
	}

	if (srcinfo->mKind == kObjectKindDirectory) {
		ERR(ENOTSUP, "hdfsCopy can't do directories right now");
		goto out;
	}

	a = hdfsOpenFile(srcFS, src_abs, O_RDONLY, 0, 0, 0);
	if (!a) {
		ERR(errno, "hdfsOpenFile failed");
		goto out;
	}

	b = hdfsOpenFile(dstFS, dst_abs, O_WRONLY, 0, DEFAULT_REPLICATION,
	    DEFAULT_BLOCK_SIZE);
	if (!b) {
		ERR(errno, "hdfsOpenFile failed");
		goto out;
	}

	block = malloc(DEFAULT_BLOCK_SIZE);
	assert(block);

	while (twritten < srcinfo->mSize) {
		tSize toread, read, written;

		toread = _imin(DEFAULT_BLOCK_SIZE, srcinfo->mSize - twritten);

		read = hdfsRead(srcFS, a, block, toread);
		if (read == -1) {
			ERR(errno, "hdfsRead failed");
			goto out;
		}

		written = hdfsWrite(dstFS, b, block, read);
		if (written == -1) {
			ERR(errno, "hdfsWrite failed");
			goto out;
		}
		assert(written == read);

		twritten += written;
	}

	res = 0;

out:
	if (a)
		hdfsCloseFile(srcFS, a);
	if (b)
		hdfsCloseFile(dstFS, b);
	if (src_abs != src)
		free(src_abs);
	if (dst_abs != dst)
		free(dst_abs);
	if (block)
		free(block);
	if (srcinfo)
		hdfsFreeFileInfo(srcinfo, 1);
	return res;
}
Пример #19
0
int libhdfsconnector::readXMLOffset(const char * filename, unsigned long seekPos, unsigned long readlen,
        const char * rowTag, const char * headerText, const char * footerText, unsigned long bufferSize)
{
    string xmlizedxpath;
    string elementname;
    string rootelement;
    xpath2xml(&xmlizedxpath, rowTag, true);
    getLastXPathElement(&elementname, rowTag);

    hdfsFile readFile = hdfsOpenFile(fs, filename, O_RDONLY, 0, 0, 0);
    if (!readFile)
    {
        fprintf(stderr, "Failed to open %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    if (hdfsSeek(fs, readFile, seekPos))
    {
        fprintf(stderr, "Failed to seek %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    unsigned char buffer[bufferSize + 1];

    bool firstRowfound = false;

    string openRowTag("<");
    openRowTag.append(elementname).append(1, '>');

    string closeRowTag("</");
    closeRowTag.append(elementname).append(1, '>');

    string closeRootTag("</");
    getLastXMLElement(&closeRootTag, footerText);
    closeRootTag.append(1, '>');

    unsigned long currentPos = seekPos + openRowTag.size();

    string currentTag("");
    bool withinRecord = false;
    bool stopAtNextClosingTag = false;
    bool parsingTag = false;

    fprintf(stderr, "--Start looking <%s>: %ld--\n", elementname.c_str(), currentPos);

    fprintf(stdout, "%s", xmlizedxpath.c_str());

    unsigned long bytesLeft = readlen;
    while (hdfsAvailable(fs, readFile) && bytesLeft > 0)
    {
        tSize numOfBytesRead = hdfsRead(fs, readFile, (void*) buffer, bufferSize);
        if (numOfBytesRead <= 0)
        {
            fprintf(stderr, "\n--Hard Stop at: %ld--\n", currentPos);
            break;
        }

        for (int buffIndex = 0; buffIndex < numOfBytesRead;)
        {
            char currChar = buffer[buffIndex];

            if (currChar == '<' || parsingTag)
            {
                if (!parsingTag)
                    currentTag.clear();

                int tagpos = buffIndex;
                while (tagpos < numOfBytesRead)
                {
                    currentTag.append(1, buffer[tagpos++]);
                    if (buffer[tagpos - 1] == '>')
                        break;
                }

                if (tagpos == numOfBytesRead && buffer[tagpos - 1] != '>')
                {
                    fprintf(stderr, "\nTag accross buffer reads...\n");

                    currentPos += tagpos - buffIndex;
                    bytesLeft -= tagpos - buffIndex;

                    buffIndex = tagpos;
                    parsingTag = true;

                    if (bytesLeft <= 0)
                    {
                        bytesLeft = readlen; //not sure how much longer til next EOL read up readlen;
                        stopAtNextClosingTag = true;
                    }
                    break;
                }
                else
                    parsingTag = false;

                if (!firstRowfound)
                {
                    firstRowfound = strcmp(currentTag.c_str(), openRowTag.c_str()) == 0;
                    if (firstRowfound)
                        fprintf(stderr, "--start piping tag %s at %lu--\n", currentTag.c_str(), currentPos);
                }

                if (strcmp(currentTag.c_str(), closeRootTag.c_str()) == 0)
                {
                    bytesLeft = 0;
                    break;
                }

                if (strcmp(currentTag.c_str(), openRowTag.c_str()) == 0)
                    withinRecord = true;
                else if (strcmp(currentTag.c_str(), closeRowTag.c_str()) == 0)
                    withinRecord = false;
                else if (firstRowfound && !withinRecord)
                {
                    bytesLeft = 0;
                    fprintf(stderr, "Unexpected Tag found: %s at position %lu\n", currentTag.c_str(), currentPos);
                    break;
                }

                currentPos += tagpos - buffIndex;
                bytesLeft -= tagpos - buffIndex;

                buffIndex = tagpos;

                if (bytesLeft <= 0 && !withinRecord)
                    stopAtNextClosingTag = true;

                if (stopAtNextClosingTag && strcmp(currentTag.c_str(), closeRowTag.c_str()) == 0)
                {
                    fprintf(stdout, "%s", currentTag.c_str());
                    fprintf(stderr, "--stop piping at %s %lu--\n", currentTag.c_str(), currentPos);
                    bytesLeft = 0;
                    break;
                }

                if (firstRowfound)
                    fprintf(stdout, "%s", currentTag.c_str());
                else
                    fprintf(stderr, "skipping tag %s\n", currentTag.c_str());

                if (buffIndex < numOfBytesRead)
                    currChar = buffer[buffIndex];
                else
                    break;
            }

            if (firstRowfound)
                fprintf(stdout, "%c", currChar);

            buffIndex++;
            currentPos++;
            bytesLeft--;

            if (bytesLeft <= 0)
            {
                if (withinRecord)
                {
                    fprintf(stderr, "\n--Looking for last closing row tag: %ld--\n", currentPos);
                    bytesLeft = readlen; //not sure how much longer til next EOL read up readlen;
                    stopAtNextClosingTag = true;
                }
                else
                    break;
            }
        }
    }

    xmlizedxpath.clear();

    xpath2xml(&xmlizedxpath, rowTag, false);
    fprintf(stdout, "%s", xmlizedxpath.c_str());

    return EXIT_SUCCESS;
}
Пример #20
0
int libhdfsconnector::streamCSVFileOffset(const char * filename, unsigned long seekPos, unsigned long readlen,
        const char * eolseq, unsigned long bufferSize, bool outputTerminator, unsigned long recLen,
        unsigned long maxLen, const char * quote, int maxretries)
{
    fprintf(stderr, "CSV terminator: \'%s\' and quote: \'%c\'\n", eolseq, quote[0]);
    unsigned long recsFound = 0;

    hdfsFile readFile = hdfsOpenFile(fs, filename, O_RDONLY, 0, 0, 0);
    if (!readFile)
    {
        fprintf(stderr, "Failed to open %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    unsigned eolseqlen = strlen(eolseq);
    if (seekPos > eolseqlen)
        seekPos -= eolseqlen; //read back sizeof(EOL) in case the seekpos happens to be a the first char after an EOL

    if (hdfsSeek(fs, readFile, seekPos))
    {
        fprintf(stderr, "Failed to seek %s for reading!\n", filename);
        return EXIT_FAILURE;
    }

    bool withinQuote = false;
    unsigned char buffer[bufferSize + 1];

    bool stopAtNextEOL = false;
    bool firstEOLfound = seekPos == 0 ? true : false;

    unsigned long currentPos = seekPos;

    fprintf(stderr, "--Start looking: %ld--\n", currentPos);

    unsigned long bytesLeft = readlen;

    while (hdfsAvailable(fs, readFile) && bytesLeft > 0)
    {
        tSize num_read_bytes = hdfsRead(fs, readFile, (void*) buffer, bufferSize);

        if (num_read_bytes <= 0)
        {
            fprintf(stderr, "\n--Hard Stop at: %ld--\n", currentPos);
            break;
        }
        for (int bufferIndex = 0; bufferIndex < num_read_bytes; bufferIndex++, currentPos++)
        {
            char currChar = buffer[bufferIndex];

            if (currChar == EOF)
                break;

            if (currChar == quote[0])
            {
                fprintf(stderr, "found quote char at pos: %ld\n", currentPos);
                withinQuote = !withinQuote;
            }

            if (currChar == eolseq[0] && !withinQuote)
            {
                bool eolfound = true;
                tSize extraNumOfBytesRead = 0;
                string tmpstr("");

                if (eolseqlen > 1)
                {
                    int eoli = bufferIndex;
                    while (eoli < num_read_bytes && eoli - bufferIndex < eolseqlen)
                    {
                        tmpstr.append(1, buffer[eoli++]);
                    }

                    if (eoli == num_read_bytes && tmpstr.size() < eolseqlen)
                    {
                        //looks like we have to do a remote read, but before we do, let's make sure the substring matches
                        if (strncmp(eolseq, tmpstr.c_str(), tmpstr.size()) == 0)
                        {
                            unsigned char tmpbuffer[eolseqlen - tmpstr.size() + 1];
                            //TODO have to make a read... of eolseqlen - tmpstr.size is it worth it?
                            //extraNumOfBytesRead = hdfsRead(*fs, readFile, (void*) tmpbuffer,
                            extraNumOfBytesRead = hdfsRead(fs, readFile, (void*) tmpbuffer, eolseqlen - tmpstr.size());

                            for (int y = 0; y < extraNumOfBytesRead; y++)
                                tmpstr.append(1, tmpbuffer[y]);
                        }
                    }

                    if (strcmp(tmpstr.c_str(), eolseq) != 0)
                        eolfound = false;
                }

                if (eolfound)
                {
                    if (!firstEOLfound)
                    {
                        bufferIndex = bufferIndex + eolseqlen - 1;
                        currentPos = currentPos + eolseqlen - 1;
                        bytesLeft = bytesLeft - eolseqlen;

                        fprintf(stderr, "\n--Start reading: %ld--\n", currentPos);

                        firstEOLfound = true;
                        continue;
                    }

                    if (outputTerminator)
                    {
                        //if (currentPos > seekPos) //Don't output first EOL
                        fprintf(stdout, "%s", eolseq);

                        bufferIndex += eolseqlen;
                        currentPos += eolseqlen;
                        bytesLeft -= eolseqlen;
                    }

                    recsFound++;
                    if (stopAtNextEOL)
                    {
                        fprintf(stderr, "\n--Stop piping: %ld--\n", currentPos);
                        bytesLeft = 0;
                        break;
                    }

                    if (bufferIndex < num_read_bytes)
                        currChar = buffer[bufferIndex];
                    else
                        break;
                }
                else if (extraNumOfBytesRead > 0)
                {
                    if (hdfsSeek(fs, readFile, hdfsTell(fs, readFile) - extraNumOfBytesRead))
                    {
                        fprintf(stderr, "Error while attempting to correct seek position\n");
                        return EXIT_FAILURE;
                    }
                }
            }

            //don't pipe until we're beyond the first EOL (if offset = 0 start piping ASAP)
            if (firstEOLfound)
            {
                fprintf(stdout, "%c", currChar);
                bytesLeft--;
            }
            else
            {
                fprintf(stderr, "%c", currChar);
                bytesLeft--;
                if (maxLen > 0 && currentPos - seekPos > maxLen * 10)
                {
                    fprintf(stderr, "\nFirst EOL was not found within the first %lu bytes", currentPos - seekPos);
                    return EXIT_FAILURE;
                }
            }

            if (stopAtNextEOL)
                fprintf(stderr, "%c", currChar);

            // ok, so if bytesLeft <= 0 at this point, we need to keep piping
            // IF the last char read was not an EOL char
            if (bytesLeft <= 0 && currChar != eolseq[0])
            {
                if (!firstEOLfound)
                {
                    fprintf(stderr,
                            "\n--Reached end of readlen before finding first record start at: %ld (breaking out)--\n",
                            currentPos);
                    break;
                }

                fprintf(stderr, "\n--Looking for Last EOL: %ld--\n", currentPos);
                bytesLeft = readlen; //not sure how much longer until next EOL read up readlen;
                stopAtNextEOL = true;
            }
        }
    }

    fprintf(stderr, "\nCurrentPos: %ld, RecsFound: %ld\n", currentPos, recsFound);
    hdfsCloseFile(fs, readFile);

    return EXIT_SUCCESS;
}
Пример #21
0
int main(int argc, char **argv)
{
    char buffer[32];
    tSize num_written_bytes;
    const char* slashTmp = "/tmp";
    int nnPort;
    char *rwTemplate, *rwTemplate2, *newDirTemplate,
    *appendTemplate, *userTemplate, *rwPath = NULL;
    const char* fileContents = "Hello, World!";
    const char* nnHost = NULL;
    
    if (argc != 2) {
        fprintf(stderr, "usage: test_libwebhdfs_ops <username>\n");
        exit(1);
    }
    
    struct NativeMiniDfsConf conf = {
        .doFormat = 1, .webhdfsEnabled = 1, .namenodeHttpPort = 50070,
    };
    cluster = nmdCreate(&conf);
    if (!cluster) {
        fprintf(stderr, "Failed to create the NativeMiniDfsCluster.\n");
        exit(1);
    }
    if (nmdWaitClusterUp(cluster)) {
        fprintf(stderr, "Error when waiting for cluster to be ready.\n");
        exit(1);
    }
    if (nmdGetNameNodeHttpAddress(cluster, &nnPort, &nnHost)) {
        fprintf(stderr, "Error when retrieving namenode host address.\n");
        exit(1);
    }
    
    hdfsFS fs = hdfsConnectAsUserNewInstance(nnHost, nnPort, argv[1]);
    if(!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(-1);
    }
    
    {
        // Write tests
        rwTemplate = strdup("/tmp/helloWorldXXXXXX");
        if (!rwTemplate) {
            fprintf(stderr, "Failed to create rwTemplate!\n");
            exit(1);
        }
        rwPath = mktemp(rwTemplate);
        // hdfsOpenFile
        hdfsFile writeFile = hdfsOpenFile(fs, rwPath,
                                          O_WRONLY|O_CREAT, 0, 0, 0);

        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", rwPath);
            exit(1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", rwPath);
        // hdfsWrite
        num_written_bytes = hdfsWrite(fs, writeFile, (void*)fileContents,
                                      (int) strlen(fileContents) + 1);
        if (num_written_bytes != strlen(fileContents) + 1) {
            fprintf(stderr, "Failed to write correct number of bytes - "
                    "expected %d, got %d\n",
                    (int)(strlen(fileContents) + 1), (int) num_written_bytes);
            exit(1);
        }
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
        
        // hdfsTell
        tOffset currentPos = -1;
        if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
            fprintf(stderr,
                    "Failed to get current file position correctly. Got %"
                    PRId64 "!\n", currentPos);
            exit(1);
        }
        fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);
        
        hdfsCloseFile(fs, writeFile);
        // Done test write
    }
    
    sleep(1);
    
    {
        //Read tests
        int available = 0, exists = 0;
        
        // hdfsExists
        exists = hdfsExists(fs, rwPath);
        if (exists) {
            fprintf(stderr, "Failed to validate existence of %s\n", rwPath);
            exists = hdfsExists(fs, rwPath);
            if (exists) {
                fprintf(stderr,
                        "Still failed to validate existence of %s\n", rwPath);
                exit(1);
            }
        }
        
        hdfsFile readFile = hdfsOpenFile(fs, rwPath, O_RDONLY, 0, 0, 0);
        if (!readFile) {
            fprintf(stderr, "Failed to open %s for reading!\n", rwPath);
            exit(1);
        }
        if (!hdfsFileIsOpenForRead(readFile)) {
            fprintf(stderr, "hdfsFileIsOpenForRead: we just opened a file "
                    "with O_RDONLY, and it did not show up as 'open for "
                    "read'\n");
            exit(1);
        }
        
        available = hdfsAvailable(fs, readFile);
        fprintf(stderr, "hdfsAvailable: %d\n", available);
        
        // hdfsSeek, hdfsTell
        tOffset seekPos = 1;
        if(hdfsSeek(fs, readFile, seekPos)) {
            fprintf(stderr, "Failed to seek %s for reading!\n", rwPath);
            exit(1);
        }
        
        tOffset currentPos = -1;
        if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
            fprintf(stderr,
                    "Failed to get current file position correctly! Got %"
                    PRId64 "!\n", currentPos);

            exit(1);
        }
        fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);
        
        if(hdfsSeek(fs, readFile, 0)) {
            fprintf(stderr, "Failed to seek %s for reading!\n", rwPath);
            exit(1);
        }
        
        // hdfsRead
        memset(buffer, 0, sizeof(buffer));
        tSize num_read_bytes = hdfsRead(fs, readFile, buffer, sizeof(buffer));
        if (strncmp(fileContents, buffer, strlen(fileContents)) != 0) {
            fprintf(stderr, "Failed to read (direct). "
                    "Expected %s but got %s (%d bytes)\n",
                    fileContents, buffer, num_read_bytes);
            exit(1);
        }
        fprintf(stderr, "Read following %d bytes:\n%s\n",
                num_read_bytes, buffer);
        
        if (hdfsSeek(fs, readFile, 0L)) {
            fprintf(stderr, "Failed to seek to file start!\n");
            exit(1);
        }
        
        // hdfsPread
        memset(buffer, 0, strlen(fileContents + 1));
        num_read_bytes = hdfsPread(fs, readFile, 0, buffer, sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n",
                num_read_bytes, buffer);
        
        hdfsCloseFile(fs, readFile);
        // Done test read
    }
    
    int totalResult = 0;
    int result = 0;
    {
        //Generic file-system operations
        char *srcPath = rwPath;
        char buffer[256];
        const char *resp;
        rwTemplate2 = strdup("/tmp/helloWorld2XXXXXX");
        if (!rwTemplate2) {
            fprintf(stderr, "Failed to create rwTemplate2!\n");
            exit(1);
        }
        char *dstPath = mktemp(rwTemplate2);
        newDirTemplate = strdup("/tmp/newdirXXXXXX");
        if (!newDirTemplate) {
            fprintf(stderr, "Failed to create newDirTemplate!\n");
            exit(1);
        }
        char *newDirectory = mktemp(newDirTemplate);
        
        // hdfsRename
        fprintf(stderr, "hdfsRename: %s\n",
                ((result = hdfsRename(fs, rwPath, dstPath)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsRename back: %s\n",
                ((result = hdfsRename(fs, dstPath, srcPath)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        // hdfsCreateDirectory
        fprintf(stderr, "hdfsCreateDirectory: %s\n",
                ((result = hdfsCreateDirectory(fs, newDirectory)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        // hdfsSetReplication
        fprintf(stderr, "hdfsSetReplication: %s\n",
                ((result = hdfsSetReplication(fs, srcPath, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;

        // hdfsGetWorkingDirectory, hdfsSetWorkingDirectory
        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n",
                ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ?
                 buffer : "Failed!"));
        totalResult += (resp ? 0 : 1);

        const char* path[] = {"/foo", "/foo/bar", "foobar", "//foo/bar//foobar",
                              "foo//bar", "foo/bar///", "/", "////"};
        int i;
        for (i = 0; i < 8; i++) {
            fprintf(stderr, "hdfsSetWorkingDirectory: %s, %s\n",
                    ((result = hdfsSetWorkingDirectory(fs, path[i])) ?
                     "Failed!" : "Success!"),
                    hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer)));
            totalResult += result;
        }

        fprintf(stderr, "hdfsSetWorkingDirectory: %s\n",
                ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n",
                ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ?
                 buffer : "Failed!"));
        totalResult += (resp ? 0 : 1);

        // hdfsGetPathInfo
        hdfsFileInfo *fileInfo = NULL;
        if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
            fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
            fprintf(stderr, "Name: %s, ", fileInfo->mName);
            fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
            fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
            fprintf(stderr, "BlockSize: %"PRId64", ", fileInfo->mBlockSize);
            fprintf(stderr, "Size: %"PRId64", ", fileInfo->mSize);
            fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod));
            fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
            fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
            char permissions[10];
            permission_disp(fileInfo->mPermissions, permissions);
            fprintf(stderr, "Permissions: %d (%s)\n",
                    fileInfo->mPermissions, permissions);
            hdfsFreeFileInfo(fileInfo, 1);
        } else {
            totalResult++;
            fprintf(stderr, "hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
        }
        
        // hdfsListDirectory
        hdfsFileInfo *fileList = 0;
        int numEntries = 0;
        if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
            int i = 0;
            for(i=0; i < numEntries; ++i) {
                fprintf(stderr, "Name: %s, ", fileList[i].mName);
                fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
                fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
                fprintf(stderr, "BlockSize: %"PRId64", ", fileList[i].mBlockSize);
                fprintf(stderr, "Size: %"PRId64", ", fileList[i].mSize);
                fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
                fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
                fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
                char permissions[10];
                permission_disp(fileList[i].mPermissions, permissions);
                fprintf(stderr, "Permissions: %d (%s)\n",
                        fileList[i].mPermissions, permissions);
            }
            hdfsFreeFileInfo(fileList, numEntries);
        } else {
            if (errno) {
                totalResult++;
                fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
            } else {
                fprintf(stderr, "Empty directory!\n");
            }
        }
        
        char *newOwner = "root";
        // Setting tmp dir to 777 so later when connectAsUser nobody,
        // we can write to it
        short newPerm = 0666;
        
        // hdfsChown
        fprintf(stderr, "hdfsChown: %s\n",
                ((result = hdfsChown(fs, rwPath, NULL, "users")) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsChown: %s\n",
                ((result = hdfsChown(fs, rwPath, newOwner, NULL)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        // hdfsChmod
        fprintf(stderr, "hdfsChmod: %s\n",
                ((result = hdfsChmod(fs, rwPath, newPerm)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        sleep(2);
        tTime newMtime = time(NULL);
        tTime newAtime = time(NULL);
        
        // utime write
        fprintf(stderr, "hdfsUtime: %s\n",
                ((result = hdfsUtime(fs, rwPath, newMtime, newAtime)) ?
                 "Failed!" : "Success!"));        
        totalResult += result;
        
        // chown/chmod/utime read
        hdfsFileInfo *finfo = hdfsGetPathInfo(fs, rwPath);
        
        fprintf(stderr, "hdfsChown read: %s\n",
                ((result = (strcmp(finfo->mOwner, newOwner) != 0)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        fprintf(stderr, "hdfsChmod read: %s\n",
                ((result = (finfo->mPermissions != newPerm)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        // will later use /tmp/ as a different user so enable it
        fprintf(stderr, "hdfsChmod: %s\n",
                ((result = hdfsChmod(fs, slashTmp, 0777)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        fprintf(stderr,"newMTime=%ld\n",newMtime);
        fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);
        
        
        fprintf(stderr, "hdfsUtime read (mtime): %s\n",
                ((result = (finfo->mLastMod != newMtime / 1000)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        // Clean up
        hdfsFreeFileInfo(finfo, 1);
        fprintf(stderr, "hdfsDelete: %s\n",
                ((result = hdfsDelete(fs, newDirectory, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n",
                ((result = hdfsDelete(fs, srcPath, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsExists: %s\n",
                ((result = hdfsExists(fs, newDirectory)) ?
                 "Success!" : "Failed!"));
        totalResult += (result ? 0 : 1);
        // Done test generic operations
    }
    
    {
        // Test Appends
        appendTemplate = strdup("/tmp/appendsXXXXXX");
        if (!appendTemplate) {
            fprintf(stderr, "Failed to create appendTemplate!\n");
            exit(1);
        }
        char *appendPath = mktemp(appendTemplate);
        const char* helloBuffer = "Hello,";
        hdfsFile writeFile = NULL;
        
        // Create
        writeFile = hdfsOpenFile(fs, appendPath, O_WRONLY, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
            exit(1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", appendPath);
        
        num_written_bytes = hdfsWrite(fs, writeFile, helloBuffer,
                                      (int) strlen(helloBuffer));
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
        hdfsCloseFile(fs, writeFile);
        
        fprintf(stderr, "hdfsSetReplication: %s\n",
                ((result = hdfsSetReplication(fs, appendPath, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        
        // Re-Open for Append
        writeFile = hdfsOpenFile(fs, appendPath, O_WRONLY | O_APPEND, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
            exit(1);
        }
        fprintf(stderr, "Opened %s for appending successfully...\n",
                appendPath);
        
        helloBuffer = " World";
        num_written_bytes = hdfsWrite(fs, writeFile, helloBuffer,
                                      (int)strlen(helloBuffer) + 1);
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
        
        hdfsCloseFile(fs, writeFile);

        // Check size
        hdfsFileInfo *finfo = hdfsGetPathInfo(fs, appendPath);
        fprintf(stderr, "fileinfo->mSize: == total %s\n",
                ((result = (finfo->mSize == strlen("Hello, World") + 1)) ?
                 "Success!" : "Failed!"));
        totalResult += (result ? 0 : 1);
        
        // Read and check data
        hdfsFile readFile = hdfsOpenFile(fs, appendPath, O_RDONLY, 0, 0, 0);
        if (!readFile) {
            fprintf(stderr, "Failed to open %s for reading!\n", appendPath);
            exit(1);
        }
        
        tSize num_read_bytes = hdfsRead(fs, readFile, buffer, sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n",
                num_read_bytes, buffer);
        fprintf(stderr, "read == Hello, World %s\n",
                (result = (strcmp(buffer, "Hello, World") == 0)) ?
                "Success!" : "Failed!");
        hdfsCloseFile(fs, readFile);
        
        // Cleanup
        fprintf(stderr, "hdfsDelete: %s\n",
                ((result = hdfsDelete(fs, appendPath, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        // Done test appends
    }
    
    totalResult += (hdfsDisconnect(fs) != 0);
    
    {
        //
        // Now test as connecting as a specific user
        // This only meant to test that we connected as that user, not to test
        // the actual fs user capabilities. Thus just create a file and read
        // the owner is correct.
        const char *tuser = "******";
        userTemplate = strdup("/tmp/usertestXXXXXX");
        if (!userTemplate) {
            fprintf(stderr, "Failed to create userTemplate!\n");
            exit(1);
        }
        char* userWritePath = mktemp(userTemplate);
        hdfsFile writeFile = NULL;
        
        fs = hdfsConnectAsUserNewInstance("default", 50070, tuser);
        if(!fs) {
            fprintf(stderr,
                    "Oops! Failed to connect to hdfs as user %s!\n",tuser);
            exit(1);
        }
        
        writeFile = hdfsOpenFile(fs, userWritePath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", userWritePath);
            exit(1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n",
                userWritePath);
        
        num_written_bytes = hdfsWrite(fs, writeFile, fileContents,
                                      (int)strlen(fileContents) + 1);
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);
        hdfsCloseFile(fs, writeFile);
        
        hdfsFileInfo *finfo = hdfsGetPathInfo(fs, userWritePath);
        if (finfo) {
            fprintf(stderr, "hdfs new file user is correct: %s\n",
                    ((result = (strcmp(finfo->mOwner, tuser) != 0)) ?
                     "Failed!" : "Success!"));
        } else {
            fprintf(stderr,
                    "hdfsFileInfo returned by hdfsGetPathInfo is NULL\n");
            result = -1;
        }
        totalResult += result;
        
        // Cleanup
        fprintf(stderr, "hdfsDelete: %s\n",
                ((result = hdfsDelete(fs, userWritePath, 1)) ?
                 "Failed!" : "Success!"));
        totalResult += result;
        // Done test specific user
    }

    totalResult += (hdfsDisconnect(fs) != 0);
    
    // Shutdown the native minidfscluster
    nmdShutdown(cluster);
    nmdFree(cluster);
    
    fprintf(stderr, "totalResult == %d\n", totalResult);
    if (totalResult != 0) {
        return -1;
    } else {
        return 0;
    }
}
Пример #22
0
int main(int argc, char **argv) {
    const char *writePath = "/tmp/testfile.txt";
    const char *fileContents = "Hello, World!";
    const char *readPath = "/tmp/testfile.txt";
    const char *srcPath = "/tmp/testfile.txt";
    const char *dstPath = "/tmp/testfile2.txt";
    const char *slashTmp = "/tmp";
    const char *newDirectory = "/tmp/newdir";
    const char *newOwner = "root";
    const char *tuser = "******";
    const char *appendPath = "/tmp/appends";
    const char *userPath = "/tmp/usertestfile.txt";

    char buffer[32], buffer2[256], rdbuffer[32];
    tSize num_written_bytes, num_read_bytes;
    hdfsFS fs, lfs;
    hdfsFile writeFile, readFile, localFile, appendFile, userFile;
    tOffset currentPos, seekPos;
    int exists, totalResult, result, numEntries, i, j;
    const char *resp;
    hdfsFileInfo *fileInfo, *fileList, *finfo;
    char *buffer3;
    char permissions[10];
    char ***hosts;
    short newPerm = 0666;
    tTime newMtime, newAtime;

    fs = hdfsConnectNewInstance("default", 0);
    if(!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(-1);
    } 
 
    lfs = hdfsConnectNewInstance(NULL, 0);
    if(!lfs) {
        fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
        exit(-1);
    } 

    {
        //Write tests
        
        writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", writePath);
            exit(-1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", writePath);
        num_written_bytes =
          hdfsWrite(fs, writeFile, (void*)fileContents,
            (tSize)(strlen(fileContents)+1));
        if (num_written_bytes != strlen(fileContents) + 1) {
          fprintf(stderr, "Failed to write correct number of bytes - expected %d, got %d\n",
                  (int)(strlen(fileContents) + 1), (int)num_written_bytes);
            exit(-1);
        }
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

        currentPos = -1;
        if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
            fprintf(stderr, 
                    "Failed to get current file position correctly! Got %" PRId64 "!\n",
                    currentPos);
            exit(-1);
        }
        fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);

        if (hdfsFlush(fs, writeFile)) {
            fprintf(stderr, "Failed to 'flush' %s\n", writePath); 
            exit(-1);
        }
        fprintf(stderr, "Flushed %s successfully!\n", writePath); 

        if (hdfsHFlush(fs, writeFile)) {
            fprintf(stderr, "Failed to 'hflush' %s\n", writePath);
            exit(-1);
        }
        fprintf(stderr, "HFlushed %s successfully!\n", writePath);

        hdfsCloseFile(fs, writeFile);
    }

    {
        //Read tests
        
        exists = hdfsExists(fs, readPath);

        if (exists) {
          fprintf(stderr, "Failed to validate existence of %s\n", readPath);
          exit(-1);
        }

        readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
        if (!readFile) {
            fprintf(stderr, "Failed to open %s for reading!\n", readPath);
            exit(-1);
        }

        if (!hdfsFileIsOpenForRead(readFile)) {
            fprintf(stderr, "hdfsFileIsOpenForRead: we just opened a file "
                    "with O_RDONLY, and it did not show up as 'open for "
                    "read'\n");
            exit(-1);
        }

        fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));

        seekPos = 1;
        if(hdfsSeek(fs, readFile, seekPos)) {
            fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
            exit(-1);
        }

        currentPos = -1;
        if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
            fprintf(stderr, 
                    "Failed to get current file position correctly! Got %" PRId64 "!\n",
                    currentPos);
            exit(-1);
        }
        fprintf(stderr, "Current position: %" PRId64 "\n", currentPos);

        if (!hdfsFileUsesDirectRead(readFile)) {
          fprintf(stderr, "Direct read support incorrectly not detected "
                  "for HDFS filesystem\n");
          exit(-1);
        }

        fprintf(stderr, "Direct read support detected for HDFS\n");

        // Test the direct read path
        if(hdfsSeek(fs, readFile, 0)) {
            fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
            exit(-1);
        }
        memset(buffer, 0, sizeof(buffer));
        num_read_bytes = hdfsRead(fs, readFile, (void*)buffer,
                sizeof(buffer));
        if (strncmp(fileContents, buffer, strlen(fileContents)) != 0) {
            fprintf(stderr, "Failed to read (direct). Expected %s but got %s (%d bytes)\n",
                    fileContents, buffer, num_read_bytes);
            exit(-1);
        }
        fprintf(stderr, "Read (direct) following %d bytes:\n%s\n",
                num_read_bytes, buffer);
        if (hdfsSeek(fs, readFile, 0L)) {
            fprintf(stderr, "Failed to seek to file start!\n");
            exit(-1);
        }

        // Disable the direct read path so that we really go through the slow
        // read path
        hdfsFileDisableDirectRead(readFile);

        num_read_bytes = hdfsRead(fs, readFile, (void*)buffer, 
                sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n", 
                num_read_bytes, buffer);

        memset(buffer, 0, strlen(fileContents + 1));

        num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer, 
                sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n", 
                num_read_bytes, buffer);

        hdfsCloseFile(fs, readFile);

        // Test correct behaviour for unsupported filesystems
        localFile = hdfsOpenFile(lfs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!localFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", writePath);
            exit(-1);
        }

        num_written_bytes = hdfsWrite(lfs, localFile, (void*)fileContents,
                                      (tSize)(strlen(fileContents) + 1));

        hdfsCloseFile(lfs, localFile);
        localFile = hdfsOpenFile(lfs, writePath, O_RDONLY, 0, 0, 0);

        if (hdfsFileUsesDirectRead(localFile)) {
          fprintf(stderr, "Direct read support incorrectly detected for local "
                  "filesystem\n");
          exit(-1);
        }

        hdfsCloseFile(lfs, localFile);
    }

    totalResult = 0;
    result = 0;
    {
        //Generic file-system operations

        fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsMove(local-local): %s\n", ((result = hdfsMove(lfs, srcPath, lfs, dstPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsMove(remote-local): %s\n", ((result = hdfsMove(fs, srcPath, lfs, srcPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsRename: %s\n", ((result = hdfsRename(fs, dstPath, srcPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsSetReplication: %s\n", ((result = hdfsSetReplication(fs, srcPath, 2)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer2, sizeof(buffer2))) != 0 ? buffer2 : "Failed!"));
        totalResult += (resp ? 0 : 1);
        fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer2, sizeof(buffer2))) != 0 ? buffer2 : "Failed!"));
        totalResult += (resp ? 0 : 1);

        fprintf(stderr, "hdfsGetDefaultBlockSize: %" PRId64 "\n", hdfsGetDefaultBlockSize(fs));
        fprintf(stderr, "hdfsGetCapacity: %" PRId64 "\n", hdfsGetCapacity(fs));
        fprintf(stderr, "hdfsGetUsed: %" PRId64 "\n", hdfsGetUsed(fs));

        fileInfo = NULL;
        if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
            fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
            fprintf(stderr, "Name: %s, ", fileInfo->mName);
            fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
            fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
            fprintf(stderr, "BlockSize: %" PRId64 ", ", fileInfo->mBlockSize);
            fprintf(stderr, "Size: %" PRId64 ", ", fileInfo->mSize);
            fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod)); 
            fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
            fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
            permission_disp(fileInfo->mPermissions, permissions);
            fprintf(stderr, "Permissions: %d (%s)\n", fileInfo->mPermissions, permissions);
            hdfsFreeFileInfo(fileInfo, 1);
        } else {
            totalResult++;
            fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
        }

        fileList = 0;
        fileList = hdfsListDirectory(fs, newDirectory, &numEntries);
        if (!(fileList == NULL && numEntries == 0 && !errno)) {
            fprintf(stderr, "waah! hdfsListDirectory for empty %s - FAILED!\n", newDirectory);
            totalResult++;
        } else {
            fprintf(stderr, "hdfsListDirectory for empty %s - SUCCESS!\n", newDirectory);
        }

        fileList = 0;
        if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
            for(i=0; i < numEntries; ++i) {
                fprintf(stderr, "Name: %s, ", fileList[i].mName);
                fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
                fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
                fprintf(stderr, "BlockSize: %" PRId64 ", ", fileList[i].mBlockSize);
                fprintf(stderr, "Size: %" PRId64 ", ", fileList[i].mSize);
                fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
                fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
                fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
                permission_disp(fileList[i].mPermissions, permissions);
                fprintf(stderr, "Permissions: %d (%s)\n", fileList[i].mPermissions, permissions);
            }
            hdfsFreeFileInfo(fileList, numEntries);
        } else {
            if (errno) {
                totalResult++;
                fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
            } else {
                fprintf(stderr, "Empty directory!\n");
            }
        }

        hosts = hdfsGetHosts(fs, srcPath, 0, 1);
        if(hosts) {
            fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
            i=0; 
            while(hosts[i]) {
                j = 0;
                while(hosts[i][j]) {
                    fprintf(stderr, 
                            "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
                    ++j;
                }
                ++i;
            }
        } else {
            totalResult++;
            fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
        }
       
        // setting tmp dir to 777 so later when connectAsUser nobody, we can write to it

        // chown write
        fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, NULL, "users")) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, newOwner, NULL)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        // chmod write
        fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, writePath, newPerm)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;



        sleep(2);
        newMtime = time(NULL);
        newAtime = time(NULL);

        // utime write
        fprintf(stderr, "hdfsUtime: %s\n", ((result = hdfsUtime(fs, writePath, newMtime, newAtime)) != 0 ? "Failed!" : "Success!"));

        totalResult += result;

        // chown/chmod/utime read
        finfo = hdfsGetPathInfo(fs, writePath);

        fprintf(stderr, "hdfsChown read: %s\n", ((result = (strcmp(finfo->mOwner, newOwner))) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsChmod read: %s\n", ((result = (finfo->mPermissions != newPerm)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        // will later use /tmp/ as a different user so enable it
        fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, "/tmp/", 0777)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr,"newMTime=%ld\n",newMtime);
        fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);


        fprintf(stderr, "hdfsUtime read (mtime): %s\n", ((result = (finfo->mLastMod != newMtime)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;

        // No easy way to turn on access times from hdfs_test right now
        //        fprintf(stderr, "hdfsUtime read (atime): %s\n", ((result = (finfo->mLastAccess != newAtime)) != 0 ? "Failed!" : "Success!"));
        //        totalResult += result;

        hdfsFreeFileInfo(finfo, 1);

        // Clean up
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, newDirectory, 1)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, srcPath, 1)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, srcPath, 1)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, dstPath, 1)) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsExists: %s\n", ((result = hdfsExists(fs, newDirectory)) != 0 ? "Success!" : "Failed!"));
        totalResult += (result ? 0 : 1);
    }

    {
      // TEST APPENDS

      // CREATE
      appendFile = hdfsOpenFile(fs, appendPath, O_WRONLY, 0, 0, 0);
      if(!appendFile) {
        fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
        exit(-1);
      }
      fprintf(stderr, "Opened %s for writing successfully...\n", appendPath);

      buffer3 = "Hello,";
      num_written_bytes = hdfsWrite(fs, appendFile, (void*)buffer3,
        (tSize)strlen(buffer3));
      fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

      if (hdfsFlush(fs, appendFile)) {
        fprintf(stderr, "Failed to 'flush' %s\n", appendPath); 
        exit(-1);
        }
      fprintf(stderr, "Flushed %s successfully!\n", appendPath); 

      hdfsCloseFile(fs, appendFile);

      // RE-OPEN
      appendFile = hdfsOpenFile(fs, appendPath, O_WRONLY|O_APPEND, 0, 0, 0);
      if(!appendFile) {
        fprintf(stderr, "Failed to open %s for writing!\n", appendPath);
        exit(-1);
      }
      fprintf(stderr, "Opened %s for writing successfully...\n", appendPath);

      buffer3 = " World";
      num_written_bytes = hdfsWrite(fs, appendFile, (void*)buffer3,
        (tSize)(strlen(buffer3) + 1));
      fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

      if (hdfsFlush(fs, appendFile)) {
        fprintf(stderr, "Failed to 'flush' %s\n", appendPath); 
        exit(-1);
      }
      fprintf(stderr, "Flushed %s successfully!\n", appendPath); 

      hdfsCloseFile(fs, appendFile);

      // CHECK size
      finfo = hdfsGetPathInfo(fs, appendPath);
      fprintf(stderr, "fileinfo->mSize: == total %s\n", ((result = (finfo->mSize == (tOffset)(strlen("Hello, World") + 1))) == 1 ? "Success!" : "Failed!"));
      totalResult += (result ? 0 : 1);

      // READ and check data
      readFile = hdfsOpenFile(fs, appendPath, O_RDONLY, 0, 0, 0);
      if (!readFile) {
        fprintf(stderr, "Failed to open %s for reading!\n", appendPath);
        exit(-1);
      }

      num_read_bytes = hdfsRead(fs, readFile, (void*)rdbuffer, sizeof(rdbuffer));
      fprintf(stderr, "Read following %d bytes:\n%s\n", 
              num_read_bytes, rdbuffer);

      fprintf(stderr, "read == Hello, World %s\n", ((result = (strcmp(rdbuffer, "Hello, World"))) == 0 ? "Success!" : "Failed!"));

      hdfsCloseFile(fs, readFile);

      // DONE test appends
    }
      
      
    totalResult += (hdfsDisconnect(fs) != 0);

    {
      //
      // Now test as connecting as a specific user
      // This is only meant to test that we connected as that user, not to test
      // the actual fs user capabilities. Thus just create a file and read
      // the owner is correct.

      fs = hdfsConnectAsUserNewInstance("default", 0, tuser);
      if(!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs as user %s!\n",tuser);
        exit(-1);
      } 

        userFile = hdfsOpenFile(fs, userPath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!userFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", userPath);
            exit(-1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", userPath);

        num_written_bytes = hdfsWrite(fs, userFile, (void*)fileContents,
          (tSize)(strlen(fileContents)+1));
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

        if (hdfsFlush(fs, userFile)) {
            fprintf(stderr, "Failed to 'flush' %s\n", userPath); 
            exit(-1);
        }
        fprintf(stderr, "Flushed %s successfully!\n", userPath); 

        hdfsCloseFile(fs, userFile);

        finfo = hdfsGetPathInfo(fs, userPath);
        fprintf(stderr, "hdfs new file user is correct: %s\n", ((result = (strcmp(finfo->mOwner, tuser))) != 0 ? "Failed!" : "Success!"));
        totalResult += result;
    }
    
    totalResult += (hdfsDisconnect(fs) != 0);

    if (totalResult != 0) {
        return -1;
    } else {
        return 0;
    }
}
Пример #23
0
 ssize_t read(void* data, size_t size) final {
     return hdfsRead(fs_, file_, data, size);
 }
Пример #24
0
int libhdfsconnector::mergeFile()
{
    if (nodeID == 0)
    {
        if (!fs)
        {
            fprintf(stderr, "Could not connect to hdfs on");
            return RETURN_FAILURE;
        }

        fprintf(stderr, "merging %d file(s) into %s\n", clusterCount, fileName);
        fprintf(stderr, "Opening %s for writing!\n", fileName);

        hdfsFile writeFile = hdfsOpenFile(fs, fileName, O_CREAT | O_WRONLY, 0, filereplication, 0);

        if (!writeFile)
        {
            fprintf(stderr, "Failed to open %s for writing!\n", fileName);
            return EXIT_FAILURE;
        }

        tSize totalBytesWritten = 0;
        for (unsigned node = 0; node < clusterCount; node++)
        {
            if (node > 0)
            {
                writeFile = hdfsOpenFile(fs, fileName, O_WRONLY | O_APPEND, 0, filereplication, 0);
                fprintf(stderr, "Re-opening %s for append!\n", fileName);
            }

            unsigned bytesWrittenSinceLastFlush = 0;

            string filepartname;

            createFilePartName(&filepartname, fileName, node, clusterCount);

            if (hdfsExists(fs, filepartname.c_str()) == 0)
            {

                fprintf(stderr, "Opening readfile  %s\n", filepartname.c_str());
                hdfsFile readFile = hdfsOpenFile(fs, filepartname.c_str(), O_RDONLY, 0, 0, 0);
                if (!readFile)
                {
                    fprintf(stderr, "Failed to open %s for reading!\n", fileName);
                    return EXIT_FAILURE;
                }

                unsigned char buffer[bufferSize + 1];

                while (hdfsAvailable(fs, readFile))
                {
                    tSize num_read_bytes = hdfsRead(fs, readFile, buffer, bufferSize);

                    if (num_read_bytes <= 0)
                        break;

                    tSize bytesWritten = 0;
                    try
                    {
                        bytesWritten = hdfsWrite(fs, writeFile, (void*) buffer, num_read_bytes);
                        totalBytesWritten += bytesWritten;
                        bytesWrittenSinceLastFlush += bytesWritten;

                        if (bytesWrittenSinceLastFlush >= flushThreshold)
                        {
                            if (hdfsFlush(fs, writeFile))
                            {
                                fprintf(stderr, "Failed to 'flush' %s\n", fileName);
                                return EXIT_FAILURE;
                            }
                            bytesWrittenSinceLastFlush = 0;
                        }
                    } catch (...)
                    {
                        fprintf(stderr, "Issue detected during HDFSWrite\n");
                        fprintf(stderr, "Bytes written in current iteration: %d\n", bytesWritten);
                        return EXIT_FAILURE;
                    }
                }

                if (hdfsFlush(fs, writeFile))
                {
                    fprintf(stderr, "Failed to 'flush' %s\n", fileName);
                    return EXIT_FAILURE;
                }

                fprintf(stderr, "Closing readfile  %s\n", filepartname.c_str());
                hdfsCloseFile(fs, readFile);

                if (cleanmerge)
                {
#ifdef HADOOP_GT_21
                    hdfsDelete(fs, filepartname.c_str(), 0);
#else
                    hdfsDelete(fs, filepartname.c_str());
#endif
                }
            }
            else
            {
                fprintf(stderr, "Could not merge, part %s was not located\n", filepartname.c_str());
                return EXIT_FAILURE;
            }

            fprintf(stderr, "Closing writefile %s\n", fileName);
            if (hdfsCloseFile(fs, writeFile) != 0)
                fprintf(stderr, "Could not close writefile %s\n", fileName);
        }

        if (cleanmerge)
        {
            string filecontainer;
            filecontainer.assign(fileName);
            filecontainer.append("-parts");
#ifdef HADOOP_GT_21
                    hdfsDelete(fs, filecontainer.c_str(), 0);
#else
                    hdfsDelete(fs, filecontainer.c_str());
#endif
        }
    }
    return EXIT_SUCCESS;
}
Пример #25
0
qioerr hdfs_preadv (void* file, const struct iovec *vector, int count, off_t offset, ssize_t* num_read_out, void* fs)
{
  ssize_t got;
  ssize_t got_total;
  qioerr err_out = 0;
  int i;

  STARTING_SLOW_SYSCALL;

#ifdef HDFS3

  const hdfs_file orig_hfl = *to_hdfs_file(file);
  const hdfs_fs orig_hfs = *to_hdfs_fs(fs);

  hdfsFS hfs = hdfsConnect(orig_hfs.fs_name, orig_hfs.fs_port);
  hdfsFile hfl = hdfsOpenFile(hfs, orig_hfl.pathnm, O_RDONLY, 0, 0, 0);

  //assert connection
  CREATE_ERROR((hfs == NULL), err_out, ECONNREFUSED, "Unable to read HDFS file", error);

  if(hfl == NULL) {
    err_out = qio_mkerror_errno();
    goto error;
  }

#endif

  err_out = 0;
  got_total = 0;
  for(i = 0; i < count; i++) {

#ifdef HDFS3
    hdfsSeek(hfs, hfl, offset+got_total);
    got = hdfsRead(hfs, hfl, (void*)vector[i].iov_base, vector[i].iov_len);
#else
    got = hdfsPread(to_hdfs_fs(fs)->hfs, to_hdfs_file(file)->file, offset + got_total, (void*)vector[i].iov_base, vector[i].iov_len);
#endif

    if( got != -1 ) {
      got_total += got;
    } else {
      err_out = qio_mkerror_errno();
      break;
    }
    if(got != (ssize_t)vector[i].iov_len ) {
      break;
    }
  }

  if( err_out == 0 && got_total == 0 && sys_iov_total_bytes(vector, count) != 0 )
    err_out = qio_int_to_err(EEOF);

  *num_read_out = got_total;

#ifdef HDFS3
  got = hdfsCloseFile(hfs, hfl);
  if(got == -1) { err_out = qio_mkerror_errno(); }

  got = hdfsDisconnect(hfs);
  if(got == -1) { err_out = qio_mkerror_errno(); }

#endif

  DONE_SLOW_SYSCALL;

#ifdef HDFS3
error:
#endif
  return err_out;
}
Пример #26
0
static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs,
                                const struct tlhPaths *paths)
{
    char tmp[4096];
    hdfsFile file;
    int ret, expected, numEntries;
    hdfsFileInfo *fileInfo;
    struct hdfsReadStatistics *readStats = NULL;

    if (hdfsExists(fs, paths->prefix) == 0) {
        EXPECT_ZERO(hdfsDelete(fs, paths->prefix, 1));
    }
    EXPECT_ZERO(hdfsCreateDirectory(fs, paths->prefix));

    EXPECT_ZERO(doTestGetDefaultBlockSize(fs, paths->prefix));

    /* There should be no entry in the directory. */
    errno = EACCES; // see if errno is set to 0 on success
    EXPECT_NULL_WITH_ERRNO(hdfsListDirectory(fs, paths->prefix, &numEntries), 0);
    if (numEntries != 0) {
        fprintf(stderr, "hdfsListDirectory set numEntries to "
                "%d on empty directory.", numEntries);
    }

    /* There should not be any file to open for reading. */
    EXPECT_NULL(hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0));

    /* hdfsOpenFile should not accept mode = 3 */
    EXPECT_NULL(hdfsOpenFile(fs, paths->file1, 3, 0, 0, 0));

    file = hdfsOpenFile(fs, paths->file1, O_WRONLY, 0, 0, 0);
    EXPECT_NONNULL(file);

    /* TODO: implement writeFully and use it here */
    expected = (int)strlen(paths->prefix);
    ret = hdfsWrite(fs, file, paths->prefix, expected);
    if (ret < 0) {
        ret = errno;
        fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
        return ret;
    }
    if (ret != expected) {
        fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
                "it wrote %d\n", ret, expected);
        return EIO;
    }
    EXPECT_ZERO(hdfsFlush(fs, file));
    EXPECT_ZERO(hdfsHSync(fs, file));
    EXPECT_ZERO(hdfsCloseFile(fs, file));

    /* There should be 1 entry in the directory. */
    EXPECT_NONNULL(hdfsListDirectory(fs, paths->prefix, &numEntries));
    if (numEntries != 1) {
        fprintf(stderr, "hdfsListDirectory set numEntries to "
                "%d on directory containing 1 file.", numEntries);
    }

    /* Let's re-open the file for reading */
    file = hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0);
    EXPECT_NONNULL(file);

    EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
    errno = 0;
    EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalBytesRead);
    EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalLocalBytesRead);
    EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalShortCircuitBytesRead);
    hdfsFileFreeReadStatistics(readStats);
    /* TODO: implement readFully and use it here */
    ret = hdfsRead(fs, file, tmp, sizeof(tmp));
    if (ret < 0) {
        ret = errno;
        fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
        return ret;
    }
    if (ret != expected) {
        fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
                "it read %d\n", ret, expected);
        return EIO;
    }
    EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
    errno = 0;
    EXPECT_UINT64_EQ((uint64_t)expected, readStats->totalBytesRead);
    hdfsFileFreeReadStatistics(readStats);
    EXPECT_ZERO(hdfsFileClearReadStatistics(file));
    EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
    EXPECT_UINT64_EQ((uint64_t)0, readStats->totalBytesRead);
    hdfsFileFreeReadStatistics(readStats);
    EXPECT_ZERO(memcmp(paths->prefix, tmp, expected));
    EXPECT_ZERO(hdfsCloseFile(fs, file));

    // TODO: Non-recursive delete should fail?
    //EXPECT_NONZERO(hdfsDelete(fs, prefix, 0));
    EXPECT_ZERO(hdfsCopy(fs, paths->file1, fs, paths->file2));

    EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, NULL));
    EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, "doop"));
    fileInfo = hdfsGetPathInfo(fs, paths->file2);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
    EXPECT_ZERO(hdfsFileIsEncrypted(fileInfo));
    hdfsFreeFileInfo(fileInfo, 1);

    EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha", "doop2"));
    fileInfo = hdfsGetPathInfo(fs, paths->file2);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
    EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
    hdfsFreeFileInfo(fileInfo, 1);

    EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha2", NULL));
    fileInfo = hdfsGetPathInfo(fs, paths->file2);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
    EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
    hdfsFreeFileInfo(fileInfo, 1);

    snprintf(tmp, sizeof(tmp), "%s/nonexistent-file-name", paths->prefix);
    EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, tmp, "ha3", NULL), ENOENT);
    return 0;
}
Пример #27
0
static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs)
{
    char prefix[256], tmp[256];
    hdfsFile file;
    int ret, expected;
    hdfsFileInfo *fileInfo;
    
    snprintf(prefix, sizeof(prefix), "/tlhData%04d", ti->threadIdx);
    
    if (hdfsExists(fs, prefix) == 0) {
        EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
    }
    EXPECT_ZERO(hdfsCreateDirectory(fs, prefix));
    snprintf(tmp, sizeof(tmp), "%s/file", prefix);
    
    EXPECT_NONNULL(hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0));
    
    file = hdfsOpenFile(fs, tmp, O_WRONLY, 0, 0, 0);
    EXPECT_NONNULL(file);
    
    /* TODO: implement writeFully and use it here */
    expected = (int)strlen(prefix);
    ret = hdfsWrite(fs, file, prefix, expected);
    if (ret < 0) {
        ret = errno;
        fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
        return ret;
    }
    if (ret != expected) {
        fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
                "it wrote %d\n", ret, expected);
        return EIO;
    }
    EXPECT_ZERO(hdfsFlush(fs, file));
    EXPECT_ZERO(hdfsCloseFile(fs, file));
    
    /* Let's re-open the file for reading */
    file = hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0);
    EXPECT_NONNULL(file);
    
    /* TODO: implement readFully and use it here */
    ret = hdfsRead(fs, file, tmp, sizeof(tmp));
    if (ret < 0) {
        ret = errno;
        fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
        return ret;
    }
    if (ret != expected) {
        fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
                "it read %d\n", ret, expected);
        return EIO;
    }
    EXPECT_ZERO(memcmp(prefix, tmp, expected));
    EXPECT_ZERO(hdfsCloseFile(fs, file));
        
    snprintf(tmp, sizeof(tmp), "%s/file", prefix);
    EXPECT_NONZERO(hdfsChown(fs, tmp, NULL, NULL));
    EXPECT_ZERO(hdfsChown(fs, tmp, NULL, "doop"));
    fileInfo = hdfsGetPathInfo(fs, tmp);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
    hdfsFreeFileInfo(fileInfo, 1);
    
    EXPECT_ZERO(hdfsChown(fs, tmp, "ha", "doop2"));
    fileInfo = hdfsGetPathInfo(fs, tmp);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
    EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
    hdfsFreeFileInfo(fileInfo, 1);
    
    EXPECT_ZERO(hdfsChown(fs, tmp, "ha2", NULL));
    fileInfo = hdfsGetPathInfo(fs, tmp);
    EXPECT_NONNULL(fileInfo);
    EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
    EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
    hdfsFreeFileInfo(fileInfo, 1);
    
    EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
    return 0;
}
Пример #28
0
int main(int argc, char **argv) {

    hdfsFS fs = hdfsConnect("default", 0);
    if(!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs!\n");
        exit(-1);
    } 
 
    hdfsFS lfs = hdfsConnect(NULL, 0);
    if(!lfs) {
        fprintf(stderr, "Oops! Failed to connect to 'local' hdfs!\n");
        exit(-1);
    } 
 
        const char* writePath = "/tmp/testfile.txt";
    {
        //Write tests
        
        
        hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", writePath);
            exit(-1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", writePath);

        char* buffer = "Hello, World!";
        tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

        tOffset currentPos = -1;
        if ((currentPos = hdfsTell(fs, writeFile)) == -1) {
            fprintf(stderr, 
                    "Failed to get current file position correctly! Got %ld!\n",
                    currentPos);
            exit(-1);
        }
        fprintf(stderr, "Current position: %ld\n", currentPos);

        if (hdfsFlush(fs, writeFile)) {
            fprintf(stderr, "Failed to 'flush' %s\n", writePath); 
            exit(-1);
        }
        fprintf(stderr, "Flushed %s successfully!\n", writePath); 

        hdfsCloseFile(fs, writeFile);
    }

    {
        //Read tests
        
        const char* readPath = "/tmp/testfile.txt";
        int exists = hdfsExists(fs, readPath);

        if (exists) {
          fprintf(stderr, "Failed to validate existence of %s\n", readPath);
          exit(-1);
        }

        hdfsFile readFile = hdfsOpenFile(fs, readPath, O_RDONLY, 0, 0, 0);
        if (!readFile) {
            fprintf(stderr, "Failed to open %s for reading!\n", readPath);
            exit(-1);
        }

        fprintf(stderr, "hdfsAvailable: %d\n", hdfsAvailable(fs, readFile));

        tOffset seekPos = 1;
        if(hdfsSeek(fs, readFile, seekPos)) {
            fprintf(stderr, "Failed to seek %s for reading!\n", readPath);
            exit(-1);
        }

        tOffset currentPos = -1;
        if((currentPos = hdfsTell(fs, readFile)) != seekPos) {
            fprintf(stderr, 
                    "Failed to get current file position correctly! Got %ld!\n", 
                    currentPos);
            exit(-1);
        }
        fprintf(stderr, "Current position: %ld\n", currentPos);

        static char buffer[32];
        tSize num_read_bytes = hdfsRead(fs, readFile, (void*)buffer, 
                sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n", 
                num_read_bytes, buffer);

        num_read_bytes = hdfsPread(fs, readFile, 0, (void*)buffer, 
                sizeof(buffer));
        fprintf(stderr, "Read following %d bytes:\n%s\n", 
                num_read_bytes, buffer);

        hdfsCloseFile(fs, readFile);
    }

    int totalResult = 0;
    int result = 0;
    {
        //Generic file-system operations

        const char* srcPath = "/tmp/testfile.txt";
        const char* dstPath = "/tmp/testfile2.txt";

        fprintf(stderr, "hdfsCopy(remote-local): %s\n", ((result = hdfsCopy(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsMove(local-local): %s\n", ((result = hdfsMove(lfs, srcPath, lfs, dstPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsMove(remote-local): %s\n", ((result = hdfsMove(fs, srcPath, lfs, srcPath)) ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsRename: %s\n", ((result = hdfsRename(fs, dstPath, srcPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsCopy(remote-remote): %s\n", ((result = hdfsCopy(fs, srcPath, fs, dstPath)) ? "Failed!" : "Success!"));
        totalResult += result;

        const char* slashTmp = "/tmp";
        const char* newDirectory = "/tmp/newdir";
        fprintf(stderr, "hdfsCreateDirectory: %s\n", ((result = hdfsCreateDirectory(fs, newDirectory)) ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsSetReplication: %s\n", ((result = hdfsSetReplication(fs, srcPath, 2)) ? "Failed!" : "Success!"));
        totalResult += result;

        char buffer[256];
        const char *resp;
        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
        totalResult += (resp ? 0 : 1);
        fprintf(stderr, "hdfsSetWorkingDirectory: %s\n", ((result = hdfsSetWorkingDirectory(fs, slashTmp)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsGetWorkingDirectory: %s\n", ((resp = hdfsGetWorkingDirectory(fs, buffer, sizeof(buffer))) ? buffer : "Failed!"));
        totalResult += (resp ? 0 : 1);

        fprintf(stderr, "hdfsGetDefaultBlockSize: %ld\n", hdfsGetDefaultBlockSize(fs));
        fprintf(stderr, "hdfsGetCapacity: %ld\n", hdfsGetCapacity(fs));
        fprintf(stderr, "hdfsGetUsed: %ld\n", hdfsGetUsed(fs));

        hdfsFileInfo *fileInfo = NULL;
        if((fileInfo = hdfsGetPathInfo(fs, slashTmp)) != NULL) {
            fprintf(stderr, "hdfsGetPathInfo - SUCCESS!\n");
            fprintf(stderr, "Name: %s, ", fileInfo->mName);
            fprintf(stderr, "Type: %c, ", (char)(fileInfo->mKind));
            fprintf(stderr, "Replication: %d, ", fileInfo->mReplication);
            fprintf(stderr, "BlockSize: %ld, ", fileInfo->mBlockSize);
            fprintf(stderr, "Size: %ld, ", fileInfo->mSize);
            fprintf(stderr, "LastMod: %s", ctime(&fileInfo->mLastMod)); 
            fprintf(stderr, "Owner: %s, ", fileInfo->mOwner);
            fprintf(stderr, "Group: %s, ", fileInfo->mGroup);
            char permissions[10];
            permission_disp(fileInfo->mPermissions, permissions);
            fprintf(stderr, "Permissions: %d (%s)\n", fileInfo->mPermissions, permissions);
            hdfsFreeFileInfo(fileInfo, 1);
        } else {
            totalResult++;
            fprintf(stderr, "waah! hdfsGetPathInfo for %s - FAILED!\n", slashTmp);
        }

        hdfsFileInfo *fileList = 0;
        int numEntries = 0;
        if((fileList = hdfsListDirectory(fs, slashTmp, &numEntries)) != NULL) {
            int i = 0;
            for(i=0; i < numEntries; ++i) {
                fprintf(stderr, "Name: %s, ", fileList[i].mName);
                fprintf(stderr, "Type: %c, ", (char)fileList[i].mKind);
                fprintf(stderr, "Replication: %d, ", fileList[i].mReplication);
                fprintf(stderr, "BlockSize: %ld, ", fileList[i].mBlockSize);
                fprintf(stderr, "Size: %ld, ", fileList[i].mSize);
                fprintf(stderr, "LastMod: %s", ctime(&fileList[i].mLastMod));
                fprintf(stderr, "Owner: %s, ", fileList[i].mOwner);
                fprintf(stderr, "Group: %s, ", fileList[i].mGroup);
                char permissions[10];
                permission_disp(fileList[i].mPermissions, permissions);
                fprintf(stderr, "Permissions: %d (%s)\n", fileList[i].mPermissions, permissions);
            }
            hdfsFreeFileInfo(fileList, numEntries);
        } else {
            if (errno) {
                totalResult++;
                fprintf(stderr, "waah! hdfsListDirectory - FAILED!\n");
            } else {
                fprintf(stderr, "Empty directory!\n");
            }
        }

        char*** hosts = hdfsGetHosts(fs, srcPath, 0, 1);
        if(hosts) {
            fprintf(stderr, "hdfsGetHosts - SUCCESS! ... \n");
            int i=0; 
            while(hosts[i]) {
                int j = 0;
                while(hosts[i][j]) {
                    fprintf(stderr, 
                            "\thosts[%d][%d] - %s\n", i, j, hosts[i][j]);
                    ++j;
                }
                ++i;
            }
        } else {
            totalResult++;
            fprintf(stderr, "waah! hdfsGetHosts - FAILED!\n");
        }
       
        char *newOwner = "root";
        // setting tmp dir to 777 so later when connectAsUser nobody, we can write to it
        short newPerm = 0666;

        // chown write
        fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, NULL, "users")) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsChown: %s\n", ((result = hdfsChown(fs, writePath, newOwner, NULL)) ? "Failed!" : "Success!"));
        totalResult += result;
        // chmod write
        fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, writePath, newPerm)) ? "Failed!" : "Success!"));
        totalResult += result;



        sleep(2);
        tTime newMtime = time(NULL);
        tTime newAtime = time(NULL);

        // utime write
        fprintf(stderr, "hdfsUtime: %s\n", ((result = hdfsUtime(fs, writePath, newMtime, newAtime)) ? "Failed!" : "Success!"));

        totalResult += result;

        // chown/chmod/utime read
        hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);

        fprintf(stderr, "hdfsChown read: %s\n", ((result = (strcmp(finfo->mOwner, newOwner) != 0)) ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr, "hdfsChmod read: %s\n", ((result = (finfo->mPermissions != newPerm)) ? "Failed!" : "Success!"));
        totalResult += result;

        // will later use /tmp/ as a different user so enable it
        fprintf(stderr, "hdfsChmod: %s\n", ((result = hdfsChmod(fs, "/tmp/", 0777)) ? "Failed!" : "Success!"));
        totalResult += result;

        fprintf(stderr,"newMTime=%ld\n",newMtime);
        fprintf(stderr,"curMTime=%ld\n",finfo->mLastMod);


        fprintf(stderr, "hdfsUtime read (mtime): %s\n", ((result = (finfo->mLastMod != newMtime)) ? "Failed!" : "Success!"));
        totalResult += result;

        // No easy way to turn on access times from hdfs_test right now
        //        fprintf(stderr, "hdfsUtime read (atime): %s\n", ((result = (finfo->mLastAccess != newAtime)) ? "Failed!" : "Success!"));
        //        totalResult += result;

        hdfsFreeFileInfo(finfo, 1);

        // Clean up
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, newDirectory)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(fs, srcPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, srcPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsDelete: %s\n", ((result = hdfsDelete(lfs, dstPath)) ? "Failed!" : "Success!"));
        totalResult += result;
        fprintf(stderr, "hdfsExists: %s\n", ((result = hdfsExists(fs, newDirectory)) ? "Success!" : "Failed!"));
        totalResult += (result ? 0 : 1);
    }


    totalResult += (hdfsDisconnect(fs) != 0);

    {
      //
      // Now test as connecting as a specific user
      // This is only meant to test that we connected as that user, not to test
      // the actual fs user capabilities. Thus just create a file and read
      // the owner is correct.

      const char *tuser = "******";
      const char* writePath = "/tmp/usertestfile.txt";
      const char **groups =  (const char**)malloc(sizeof(char*)* 2);
      groups[0] = "users";
      groups[1] = "nobody";

      fs = hdfsConnectAsUser("default", 0, tuser, groups, 2);
      if(!fs) {
        fprintf(stderr, "Oops! Failed to connect to hdfs as user %s!\n",tuser);
        exit(-1);
      } 

        hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
        if(!writeFile) {
            fprintf(stderr, "Failed to open %s for writing!\n", writePath);
            exit(-1);
        }
        fprintf(stderr, "Opened %s for writing successfully...\n", writePath);

        char* buffer = "Hello, World!";
        tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
        fprintf(stderr, "Wrote %d bytes\n", num_written_bytes);

        if (hdfsFlush(fs, writeFile)) {
            fprintf(stderr, "Failed to 'flush' %s\n", writePath); 
            exit(-1);
        }
        fprintf(stderr, "Flushed %s successfully!\n", writePath); 

        hdfsCloseFile(fs, writeFile);

        hdfsFileInfo *finfo = hdfsGetPathInfo(fs, writePath);
        fprintf(stderr, "hdfs new file user is correct: %s\n", ((result = (strcmp(finfo->mOwner, tuser) != 0)) ? "Failed!" : "Success!"));
        totalResult += result;
    }
    
    totalResult += (hdfsDisconnect(fs) != 0);

    if (totalResult != 0) {
        return -1;
    } else {
        return 0;
    }
}
Пример #29
0
int MaprCopyingInputStream::Read(void * buffer, int size){
  CHECK(input_->is_open_);
  return hdfsRead(input_->fs_, input_->file_, buffer, size);
}