//Write data into an open file. //file: The file handle. //buffer: The data. //length: The no. of bytes to write. //Returns the number of bytes written; or error. func (fs *Fs) Write(file *File, buffer []byte, length int) (uint32, error) { file.Lock() defer file.Unlock() ret, err := C.hdfsWrite(fs.cptr, file.cptr, (unsafe.Pointer(&buffer[0])), C.tSize(length)) if err != nil && ret == C.tSize(-1) { return 0, err } return uint32(ret), nil }
//Positional read of data from an open file. //file: The file handle. //position: Position from which to read. //buffer: The buffer to copy read bytes into. //length: The length of the buffer. //Returns the number of bytes actually read, possibly less than length; or error. func (fs *Fs) Pread(file *File, position int64, buffer []byte, length int) (uint32, error) { file.RLock() defer file.RUnlock() ret, err := C.hdfsPread(fs.cptr, file.cptr, C.tOffset(position), (unsafe.Pointer(&buffer[0])), C.tSize(length)) if err != nil && ret == C.tSize(-1) { return 0, err } return uint32(ret), nil }
//Open a hdfs file in given mode. //path: The full path to the file. //flags: - an | of bits/fcntl.h file flags - supported flags are O_RDONLY, O_WRONLY (meaning create or overwrite i.e., implies O_TRUNCAT), O_WRONLY|O_APPEND. Other flags are generally ignored other than (O_RDWR || (O_EXCL & O_CREAT)) which return nil and set error equal ENOTSUP. //bufferSize: Size of buffer for read/write - pass 0 if you want to use the default configured values. //replication: Block replication - pass 0 if you want to use the default configured values. //blocksize: Size of block - pass 0 if you want to use the default configured values. //Returns the handle to the open file or nil on error. func (fs *Fs) OpenFile(path string, flags int, buffersize int, replication int, blocksize uint32) (*File, error) { p := C.CString(path) defer C.free(unsafe.Pointer(p)) file, err := C.hdfsOpenFile(fs.cptr, p, C.int(flags), C.int(buffersize), C.short(replication), C.tSize(blocksize)) if err != nil && file == (C.hdfsFile)(unsafe.Pointer(uintptr(0))) { return nil, err } return &File{file, new(sync.RWMutex)}, nil }