/*! \details This function opens a directory. * * \return a pointer to the directory or NULL with errno (see \ref ERRNO) set to: * - ENOMEM: not enough memory * - ENOENT: \a dirname was not found * - EACCES: read access to \a dirname is not allowed * - ENAMETOOLONG: \a dirname exceeds \a PATH_MAX or an element of \a dirname exceeds \a NAME_MAX * */ DIR * opendir(const char * dirname){ int err; DIR * dirp; const sysfs_t * fs; if ( sysfs_ispathinvalid(dirname) == true ){ return NULL; } fs = sysfs_find(dirname, false); //see which filesystem we are working with first if ( fs == NULL ){ //if no filesystem is found for the path, return no entity errno = ENOENT; return NULL; } dirp = malloc(sizeof(DIR)); if ( dirp == NULL ){ errno = ENOMEM; return NULL; } //Open the directory and check for errors err = fs->opendir(fs->cfg, &(dirp->handle), sysfs_stripmountpath(fs, dirname)); if ( err < 0 ){ free(dirp); return NULL; } //Initialize the DIR structure dirp->fs = fs; dirp->loc = 0; //Return the pointer to the table return dirp; }
int rootfs_stat(const void * cfg, const char * path, struct stat * st){ char buffer[PATH_MAX]; const sysfs_t * fs; int is_root = 0; if( path[0] == 0 ){ is_root = 1; } strcpy(buffer, "/"); strcat(buffer, path); //find the mount point of the path fs = sysfs_find(buffer, false); if ( fs == NULL ){ errno = ENOENT; return -1; } //If the fs is not root, but the mount path says root then the path doesn't exist if( (is_root == 0) ){ if( (fs->mount_path[1] == 0) ){ errno = ENOENT; return -1; } } st->st_mode = S_IFDIR | fs->access; return 0; }
int lstat(const char * path, struct stat *buf){ const sysfs_t * fs; if ( sysfs_ispathinvalid(path) == true ){ return -1; } fs = sysfs_find(path, true); if ( fs != NULL ){ return fs->lstat(fs->cfg, sysfs_stripmountpath(fs, path), buf); } errno = ENOENT; return -1; }
/*! \cond */ int _stat(const char * path, struct stat *buf){ const sysfs_t * fs; if ( sysfs_ispathinvalid(path) == true ){ return -1; } fs = sysfs_find(path, true); if ( fs != NULL ){ int ret = fs->stat(fs->config, sysfs_stripmountpath(fs, path), buf); SYSFS_PROCESS_RETURN(ret); return ret; } errno = ENOENT; return -1; }