Example #1
0
//Get list of files/directories for a given directory-path.
//path: The path of the directory.
//Returns a slice of FileInfo struct pointer, or nil on error.
func (fs *Fs) ListDirectory(path string) ([]*FileInfo, error) {
	var num int
	p := C.CString(path)
	defer C.free(unsafe.Pointer(p))
	info, _ := C.hdfsListDirectory(fs.cptr, p, (*C.int)(unsafe.Pointer(&num)))
	// Note: 如果一个目录是空的, C.hdfsListDirectory 也会返回 NULL,
	//       这样, 下面就会返回一个error. 但其实没有错误.
	//       yiwang和zyxar讨论过这个情况, 他也认同,
	//       但是他也没有办法区分 C.hdfsListDirectory 的返回歧义.
	if info == nil {
		return nil, fmt.Errorf("error in listing directory %s", path)
	}
	defer C.hdfsFreeFileInfo(info, C.int(num))
	ret := make([]*FileInfo, num)
	var cinfo *C.hdfsFileInfo
	for i := 0; i < num; i++ {
		cinfo = (*C.hdfsFileInfo)(unsafe.Pointer(uintptr(unsafe.Pointer(info)) + uintptr(i)*unsafe.Sizeof(C.hdfsFileInfo{})))
		ret[i] = new(FileInfo)
		ret[i].meta = hdfsFileInfo{*cinfo}
		ret[i].Kind = byte(cinfo.mKind)
		ret[i].Name = C.GoString(cinfo.mName)
		ret[i].LastMod = time.Unix(int64(cinfo.mLastMod), int64(0))
		ret[i].Size = int64(cinfo.mSize)
		ret[i].Replication = int16(cinfo.mReplication)
		ret[i].BlockSize = int64(cinfo.mBlockSize)
		ret[i].Owner = C.GoString(cinfo.mOwner)
		ret[i].Group = C.GoString(cinfo.mGroup)
		ret[i].Permissions = int16(cinfo.mPermissions)
		ret[i].LastAccess = time.Unix(int64(cinfo.mLastAccess), int64(0))
	}
	return ret, nil
}
Example #2
0
//Get information about a path as a single FileInfo struct pointer.
//path: The path of the file.
//Returns a pointer to FileInfo object, or nil on error.
func (fs *Fs) GetPathInfo(path string) (*FileInfo, error) {
	p := C.CString(path)
	defer C.free(unsafe.Pointer(p))
	info, err := C.hdfsGetPathInfo(fs.cptr, p)
	if info == nil {
		return nil, err
	}
	defer C.hdfsFreeFileInfo(info, C.int(1))
	ret := new(FileInfo)
	ret.meta = hdfsFileInfo{*info}
	ret.Kind = byte(info.mKind)
	ret.Name = C.GoString(info.mName)
	ret.LastMod = time.Unix(int64(info.mLastMod), int64(0))
	ret.Size = int64(info.mSize)
	ret.Replication = int16(info.mReplication)
	ret.BlockSize = int64(info.mBlockSize)
	ret.Owner = C.GoString(info.mOwner)
	ret.Group = C.GoString(info.mGroup)
	ret.Permissions = int16(info.mPermissions)
	ret.LastAccess = time.Unix(int64(info.mLastAccess), int64(0))
	return ret, nil
}