HDFS-7838. Expose truncate API for libhdfs. (yliu)

This commit is contained in:
yliu 2015-03-17 07:24:20 +08:00
parent 77297017d8
commit 991ac04afc
4 changed files with 60 additions and 0 deletions

View File

@ -53,6 +53,8 @@ Release 2.7.0 - UNRELEASED
HDFS-6488. Support HDFS superuser in NFS gateway. (brandonli)
HDFS-7838. Expose truncate API for libhdfs. (yliu)
IMPROVEMENTS
HDFS-7752. Improve description for

View File

@ -1124,6 +1124,12 @@ done:
return file;
}
int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength)
{
errno = ENOTSUP;
return -1;
}
tSize hdfsWrite(hdfsFS fs, hdfsFile file, const void* buffer, tSize length)
{
if (length == 0) {

View File

@ -1037,6 +1037,43 @@ done:
return file;
}
int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength)
{
jobject jFS = (jobject)fs;
jthrowable jthr;
jvalue jVal;
jobject jPath = NULL;
JNIEnv *env = getJNIEnv();
if (!env) {
errno = EINTERNAL;
return -1;
}
/* Create an object of org.apache.hadoop.fs.Path */
jthr = constructNewObjectOfPath(env, path, &jPath);
if (jthr) {
errno = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
"hdfsTruncateFile(%s): constructNewObjectOfPath", path);
return -1;
}
jthr = invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
"truncate", JMETHOD2(JPARAM(HADOOP_PATH), "J", "Z"),
jPath, newlength);
destroyLocalReference(env, jPath);
if (jthr) {
errno = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
"hdfsTruncateFile(%s): FileSystem#truncate", path);
return -1;
}
if (jVal.z == JNI_TRUE) {
return 1;
}
return 0;
}
int hdfsUnbufferFile(hdfsFile file)
{
int ret;

View File

@ -395,6 +395,21 @@ extern "C" {
hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags,
int bufferSize, short replication, tSize blocksize);
/**
* hdfsTruncateFile - Truncate a hdfs file to given lenght.
* @param fs The configured filesystem handle.
* @param path The full path to the file.
* @param newlength The size the file is to be truncated to
* @return 1 if the file has been truncated to the desired newlength
* and is immediately available to be reused for write operations
* such as append.
* 0 if a background process of adjusting the length of the last
* block has been started, and clients should wait for it to
* complete before proceeding with further file updates.
* -1 on error.
*/
int hdfsTruncateFile(hdfsFS fs, const char* path, tOffset newlength);
/**
* hdfsUnbufferFile - Reduce the buffering done on a file.
*