// ReadSymlinkedDirectory returns the target directory of a symlink.
// The target of the symbolic link may not be a file.
978055
func ReadSymlinkedDirectory(path string) (string, error) {
	var realPath string
	var err error
	if realPath, err = filepath.Abs(path); err != nil {
		return "", fmt.Errorf("unable to get absolute path for %s: %s", path, err)
	}
	if realPath, err = filepath.EvalSymlinks(realPath); err != nil {
		return "", fmt.Errorf("failed to canonicalise path for %s: %s", path, err)
	}
	realPathInfo, err := os.Stat(realPath)
	if err != nil {
		return "", fmt.Errorf("failed to stat target '%s' of '%s': %s", realPath, path, err)
	}
	if !realPathInfo.Mode().IsDir() {
		return "", fmt.Errorf("canonical path points to a file '%s'", realPath)
	}
	return realPath, nil
}
845108
func (n *mutDir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
	node, err := n.creat(ctx, req.NewName, symlinkType)
	if err != nil {
		Logger.Printf("mutDir.Symlink(%q): %v", req.NewName, err)
		return nil, fuse.EIO
	}
	mf := node.(*mutFile)
	mf.symLink = true
	mf.target = req.Target

	claim := schema.NewSetAttributeClaim(mf.permanode, "camliSymlinkTarget", req.Target)
	_, err = n.fs.client.UploadAndSignBlob(ctx, claim)
	if err != nil {
		Logger.Printf("mutDir.Symlink(%q) upload error: %v", req.NewName, err)
		return nil, fuse.EIO
	}

	return node, nil
}
471386
func (fs *fakeFs) ReadDir(dirname string) ([]os.FileInfo, error) {
	return fs.a.ReadDir(dirname)
}
212003
func (s *Symlink) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (link string, err error) {
	s.parent.folder.fs.vlog.CLogf(ctx, libkb.VLog1, "Symlink Readlink")
	defer func() { err = s.parent.folder.processError(ctx, libkbfs.ReadMode, err) }()

	_, de, err := s.parent.folder.fs.config.KBFSOps().Lookup(ctx, s.parent.node, s.name)
	if err != nil {
		return "", err
	}

	if de.Type != data.Sym {
		return "", fuse.Errno(syscall.EINVAL)
	}
	return de.SymPath, nil
}
576406
func (fs *Filespace) ReadDir(subPath string) ([]os.FileInfo, error) {
	return ioutil.ReadDir(fs.path + subPath)
}
666795
func copySymLink(src, dst string) (err error) {
	link, err := os.Readlink(src)
	if err != nil {
		return err
	}

	return os.Symlink(link, dst)
}
30648
func (ff FilesystemView) NewSymlinkedView(source, linkname string) FilesystemView {
	prefix := source
	if ff.sourcePrefix != "" {
		prefix = filepath.Join(ff.sourcePrefix, source)
	}
	return FilesystemView{root: linkname, blacklist: ff.blacklist, sourcePrefix: prefix}
}
956923
func symlinkDirHelper(pathname string, de *Dirent, o *Options) (bool, error) {
	// Only need to Stat entry if platform did not already have os.ModeDir
	// set, such as would be the case for unix like operating systems.
	// (This guard eliminates extra os.Stat check on Windows.)
	if !de.IsDir() {
		// Remove symlinks from the pathname (replaced with a more direct path)
		pathname, err := evalSymlinksHelper(pathname)
		if err != nil {
			if action := o.ErrorCallback(pathname, err); action == SkipNode {
				err = nil
				return true, nil
			}
			return false, err
		}

		fi, err := os.Stat(pathname)
		if err != nil {
			if action := o.ErrorCallback(pathname, err); action == SkipNode {
				return true, nil
			}
			return false, err
		}
		de.modeType = fi.Mode() & os.ModeType
	}
	return false, nil
}
1274173
func (d *Driver) recreateSymlinks() error {
	// List all the directories under the home directory
	dirs, err := ioutil.ReadDir(d.home)
	if err != nil {
		return fmt.Errorf("error reading driver home directory %q: %v", d.home, err)
	}
	// This makes the link directory if it doesn't exist
	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
	if err != nil {
		return err
	}
	if err := idtools.MkdirAllAs(path.Join(d.home, linkDir), 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
		return err
	}
	for _, dir := range dirs {
		// Skip over the linkDir and anything that is not a directory
		if dir.Name() == linkDir || !dir.Mode().IsDir() {
			continue
		}
		// Read the "link" file under each layer to get the name of the symlink
		data, err := ioutil.ReadFile(path.Join(d.dir(dir.Name()), "link"))
		if err != nil {
			return fmt.Errorf("error reading name of symlink for %q: %v", dir, err)
		}
		linkPath := path.Join(d.home, linkDir, strings.Trim(string(data), "\n"))
		// Check if the symlink exists, and if it doesn't create it again with the name we
		// got from the "link" file
		_, err = os.Stat(linkPath)
		if err != nil && os.IsNotExist(err) {
			if err := os.Symlink(path.Join("..", dir.Name(), "diff"), linkPath); err != nil {
				return err
			}
		} else if err != nil {
			return fmt.Errorf("error trying to stat %q: %v", linkPath, err)
		}
	}
	return nil
}
941879
func readDirN(dirPath string, count int) (entries []string, err error) {
	d, err := os.Open(dirPath)
	if err != nil {
		// File is really not found.
		if os.IsNotExist(err) {
			return nil, errFileNotFound
		}

		// File path cannot be verified since one of the parents is a file.
		if strings.Contains(err.Error(), "not a directory") {
			return nil, errFileNotFound
		}
		return nil, err
	}
	defer d.Close()

	st, err := d.Stat()
	if err != nil {
		return nil, err
	}
	// Not a directory return error.
	if !st.IsDir() {
		return nil, errFileAccessDenied
	}

	data := &syscall.Win32finddata{}

	remaining := count
	done := false
	for !done {
		e := syscall.FindNextFile(syscall.Handle(d.Fd()), data)
		if e != nil {
			if e == syscall.ERROR_NO_MORE_FILES {
				break
			} else {
				err = &os.PathError{
					Op:   "FindNextFile",
					Path: dirPath,
					Err:  e,
				}
				return
			}
		}
		name := syscall.UTF16ToString(data.FileName[0:])
		if name == "." || name == ".." { // Useless names
			continue
		}
		switch {
		case data.FileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0:
			// If its symbolic link, follow the link using os.Stat()
			var fi os.FileInfo
			fi, err = os.Stat(pathJoin(dirPath, name))
			if err != nil {
				// If file does not exist, we continue and skip it.
				// Could happen if it was deleted in the middle while
				// this list was being performed.
				if os.IsNotExist(err) {
					continue
				}
				return nil, err
			}
			if fi.IsDir() {
				entries = append(entries, name+slashSeparator)
			} else if fi.Mode().IsRegular() {
				entries = append(entries, name)
			}
		case data.FileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY != 0:
			entries = append(entries, name+slashSeparator)
		default:
			entries = append(entries, name)
		}
		if remaining > 0 {
			remaining--
			done = remaining == 0
		}
	}
	return entries, nil
}
17876
func ReadDir(dirname string) ([]os.FileInfo, error) {
	list, err := ioutil_base.ReadDir(dirname)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to read dir %q", dirname)
	}

	return list, nil
}
779325
func (fs MapFS) ReadDir(p string) ([]os.FileInfo, error) {
	e, ok := fs[fixpath(p)]
	if !ok {
		return nil, os.ErrNotExist
	}

	fis := make([]os.FileInfo, len(e.entries))
	for i, e := range e.entries {
		fis[i] = os.FileInfo(e)
	}
	return fis, nil
}
943783
func makeSymlinks(targetName string, commandFns []func() *cobra.Command) error {
	wd, err := os.Getwd()
	if err != nil {
		return err
	}

	var errs bool
	for _, commandFn := range commandFns {
		command := commandFn()
		link := path.Join(wd, command.Name())

		err := os.Symlink(targetName, link)
		if err != nil {
			errs = true
			fmt.Println(err)
		}
	}

	if errs {
		return errors.New("Error creating one or more symlinks")
	}
	return nil
}
621051
func evalSymlinksAndAbs(path string, err error) (string, error) {
	if err == nil {
		path, err = filepath.EvalSymlinks(path)
		if err == nil {
			path, err = filepath.Abs(path)
		}
	}
	return path, err
}
798863
func ReadDir(r Driver, dirname string) ([]os.FileInfo, error) {
	f, err := r.Open(dirname)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dirs, err := f.Readdir(-1)
	if err != nil {
		return nil, err
	}

	sort.Sort(fileInfos(dirs))
	return dirs, nil
}
1112842
func (sc *Client) ResolveSymlink(path string) string {
	pointer, err := sc.ReadLink(path)
	if err != nil {
		return path
	}
	return pointer
}
1067754
func (bs *blobStore) readlink(ctx context.Context, path string) (digest.Digest, error) {
	content, err := bs.driver.GetContent(ctx, path)
	if err != nil {
		return "", err
	}

	linked, err := digest.Parse(string(content))
	if err != nil {
		return "", err
	}

	return linked, nil
}