fuse_impls_readdir.c
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:4k
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include "fuse_dfs.h"
- #include "fuse_impls.h"
- #include "fuse_stat_struct.h"
- #include "fuse_connect.h"
- int dfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
- off_t offset, struct fuse_file_info *fi)
- {
- TRACE1("readdir",path)
- (void) offset;
- (void) fi;
- // retrieve dfs specific data
- dfs_context *dfs = (dfs_context*)fuse_get_context()->private_data;
- // check params and the context var
- assert(dfs);
- assert(path);
- assert(buf);
- int path_len = strlen(path);
- hdfsFS userFS;
- // if not connected, try to connect and fail out if we can't.
- if ((userFS = doConnectAsUser(dfs->nn_hostname,dfs->nn_port))== NULL) {
- syslog(LOG_ERR, "ERROR: could not connect to dfs %s:%dn", __FILE__, __LINE__);
- return -EIO;
- }
- // call dfs to read the dir
- int numEntries = 0;
- hdfsFileInfo *info = hdfsListDirectory(userFS,path,&numEntries);
- userFS = NULL;
- // NULL means either the directory doesn't exist or maybe IO error.
- if (NULL == info) {
- return -ENOENT;
- }
- int i ;
- for (i = 0; i < numEntries; i++) {
- // check the info[i] struct
- if (NULL == info[i].mName) {
- syslog(LOG_ERR,"ERROR: for <%s> info[%d].mName==NULL %s:%d", path, i, __FILE__,__LINE__);
- continue;
- }
- struct stat st;
- fill_stat_structure(&info[i], &st);
- // hack city: todo fix the below to something nicer and more maintainable but
- // with good performance
- // strip off the path but be careful if the path is solely '/'
- // NOTE - this API started returning filenames as full dfs uris
- const char *const str = info[i].mName + dfs->dfs_uri_len + path_len + ((path_len == 1 && *path == '/') ? 0 : 1);
- // pack this entry into the fuse buffer
- int res = 0;
- if ((res = filler(buf,str,&st,0)) != 0) {
- syslog(LOG_ERR, "ERROR: readdir filling the buffer %d %s:%dn",res, __FILE__, __LINE__);
- }
- }
- // insert '.' and '..'
- const char *const dots [] = { ".",".."};
- for (i = 0 ; i < 2 ; i++)
- {
- struct stat st;
- memset(&st, 0, sizeof(struct stat));
- // set to 0 to indicate not supported for directory because we cannot (efficiently) get this info for every subdirectory
- st.st_nlink = 0;
- // setup stat size and acl meta data
- st.st_size = 512;
- st.st_blksize = 512;
- st.st_blocks = 1;
- st.st_mode = (S_IFDIR | 0777);
- st.st_uid = default_id;
- st.st_gid = default_id;
- // todo fix below times
- st.st_atime = 0;
- st.st_mtime = 0;
- st.st_ctime = 0;
- const char *const str = dots[i];
- // flatten the info using fuse's function into a buffer
- int res = 0;
- if ((res = filler(buf,str,&st,0)) != 0) {
- syslog(LOG_ERR, "ERROR: readdir filling the buffer %d %s:%d", res, __FILE__, __LINE__);
- }
- }
- // free the info pointers
- hdfsFreeFileInfo(info,numEntries);
- return 0;
- }