// Removes all files and folders at a given path, handles
// long paths for windows automatically.
18945
func fsRemoveAll(ctx context.Context, dirPath string) (err error) {
	if dirPath == "" {
		logger.LogIf(ctx, errInvalidArgument)
		return errInvalidArgument
	}

	if err = checkPathLength(dirPath); err != nil {
		logger.LogIf(ctx, err)
		return err
	}

	if err = removeAll(dirPath); err != nil {
		if os.IsPermission(err) {
			logger.LogIf(ctx, errVolumeAccessDenied)
			return errVolumeAccessDenied
		} else if isSysErrNotEmpty(err) {
			logger.LogIf(ctx, errVolumeNotEmpty)
			return errVolumeNotEmpty
		}
		logger.LogIf(ctx, err)
		return err
	}

	return nil
}
313807
func Remove(path string) error {
	dur := 500 * time.Millisecond
	conn, err := winio.DialPipe(path, &dur)
	if err == nil {
		return conn.Close()
	}

	os.Remove(path)
	return nil
}
1175798
func CleanupRoute(route SteplibRoute) error {
	pth := path.Join(GetCollectionsDirPath(), route.FolderAlias)
	if err := command.RemoveDir(pth); err != nil {
		return err
	}
	return RemoveRoute(route)
}
849498
func (l *WAL) Remove(ctx context.Context, files []string) error {
	if !l.enabled {
		return nil
	}

	span, _ := tracing.StartSpanFromContext(ctx)
	defer span.Finish()

	l.mu.Lock()
	defer l.mu.Unlock()

	for i, fn := range files {
		span.LogKV(fmt.Sprintf("path-%d", i), fn)
		os.RemoveAll(fn)
	}

	// Refresh the on-disk size stats
	segments, err := SegmentFileNames(l.path)
	if err != nil {
		return err
	}
	l.tracker.SetSegments(uint64(len(segments)))

	var totalOldDiskSize int64
	for _, seg := range segments {
		stat, err := os.Stat(seg)
		if err != nil {
			return err
		}

		totalOldDiskSize += stat.Size()
	}
	l.tracker.SetOldSegmentSize(uint64(totalOldDiskSize))
	return nil
}
1304150
func cleanAndExpandPath(path string) string {
	if path == "" {
		return ""
	}

	// Expand initial ~ to OS specific home directory.
	if strings.HasPrefix(path, "~") {
		var homeDir string
		user, err := user.Current()
		if err == nil {
			homeDir = user.HomeDir
		} else {
			homeDir = os.Getenv("HOME")
		}

		path = strings.Replace(path, "~", homeDir, 1)
	}

	// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
	// but the variables can still be expanded via POSIX-style $VARIABLE.
	return filepath.Clean(os.ExpandEnv(path))
}
742676
func RemoveAll(fs Filesystem, path string) error {
	if err := fs.Remove(path); err == nil || os.IsNotExist(err) {
		return nil
	}

	// We could not delete it, so might be a directory
	fis, err := fs.ReadDir(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}

	// Remove contents & return first error.
	err = nil
	for _, fi := range fis {
		err1 := RemoveAll(fs, path+string(fs.PathSeparator())+fi.Name())
		if err == nil {
			err = err1
		}
	}

	// Remove directory itself.
	err1 := fs.Remove(path)
	if err1 == nil || os.IsNotExist(err1) {
		return nil
	}
	if err == nil {
		err = err1
	}
	return err
}
420061
func (b *FileBackend) cleanupLogicalPath(path string) error {
	nodes := strings.Split(path, fmt.Sprintf("%c", os.PathSeparator))
	for i := len(nodes) - 1; i > 0; i-- {
		fullPath := filepath.Join(b.path, filepath.Join(nodes[:i]...))

		dir, err := os.Open(fullPath)
		if err != nil {
			if dir != nil {
				dir.Close()
			}
			if os.IsNotExist(err) {
				return nil
			} else {
				return err
			}
		}

		list, err := dir.Readdir(1)
		dir.Close()
		if err != nil && err != io.EOF {
			return err
		}

		// If we have no entries, it's an empty directory; remove it
		if err == io.EOF || list == nil || len(list) == 0 {
			err = os.Remove(fullPath)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
529482
func (d *Dir) Cleanup(ctx context.Context, fi *dokan.FileInfo) {
	var err error
	if fi != nil {
		d.folder.fs.logEnterf(ctx, "Dir Cleanup %q delete=%v", d.name,
			fi.IsDeleteOnClose())
	} else {
		d.folder.fs.logEnterf(ctx, "Dir Cleanup %q", d.name)
	}
	defer func() { d.folder.reportErr(ctx, libkbfs.WriteMode, err) }()

	if fi != nil && fi.IsDeleteOnClose() && d.parent != nil {
		// renameAndDeletionLock should be the first lock to be grabbed in libdokan.
		d.folder.fs.renameAndDeletionLock.Lock()
		defer d.folder.fs.renameAndDeletionLock.Unlock()
		d.folder.fs.vlog.CLogf(
			ctx, libkb.VLog1, "Removing (Delete) dir in cleanup %s", d.name)

		err = d.folder.fs.config.KBFSOps().RemoveDir(ctx, d.parent, d.name)
	}

	if d.refcount.Decrease() {
		d.folder.forgetNode(ctx, d.node)
	}
}
1022462
func (s *FileSystemStorage) Delete(filepath string) error {
	return os.Remove(s.Path(filepath))
}
811862
func RemoveAllRelative(path string, root *os.File) error {
	fi, err := LstatRelative(path, root)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}
	fileAttributes := fi.Sys().(*syscall.Win32FileAttributeData).FileAttributes
	if fileAttributes&syscall.FILE_ATTRIBUTE_DIRECTORY == 0 || fileAttributes&syscall.FILE_ATTRIBUTE_REPARSE_POINT != 0 {
		// If this is a reparse point, it can't have children. Simple remove will do.
		err := RemoveRelative(path, root)
		if err == nil || os.IsNotExist(err) {
			return nil
		}
		return err
	}

	// It is necessary to use os.Open as Readdirnames does not work with
	// OpenRelative. This is safe because the above lstatrelative fails
	// if the target is outside the root, and we know this is not a
	// symlink from the above FILE_ATTRIBUTE_REPARSE_POINT check.
	fd, err := os.Open(filepath.Join(root.Name(), path))
	if err != nil {
		if os.IsNotExist(err) {
			// Race. It was deleted between the Lstat and Open.
			// Return nil per RemoveAll's docs.
			return nil
		}
		return err
	}

	// Remove contents & return first error.
	for {
		names, err1 := fd.Readdirnames(100)
		for _, name := range names {
			err1 := RemoveAllRelative(path+string(os.PathSeparator)+name, root)
			if err == nil {
				err = err1
			}
		}
		if err1 == io.EOF {
			break
		}
		// If Readdirnames returned an error, use it.
		if err == nil {
			err = err1
		}
		if len(names) == 0 {
			break
		}
	}
	fd.Close()

	// Remove directory.
	err1 := RemoveRelative(path, root)
	if err1 == nil || os.IsNotExist(err1) {
		return nil
	}
	if err == nil {
		err = err1
	}
	return err
}
1053142
func (ns *NameStrategy) filterDirs(path string) []string {
	allDirs := strings.Split(path, GoSeperator)
	dirs := make([]string, 0, len(allDirs))
	for _, p := range allDirs {
		if ns.IgnoreWords == nil || !ns.IgnoreWords[p] {
			dirs = append(dirs, importPathNameSanitizer.Replace(p))
		}
	}
	return dirs
}
519750
func clean(fileName string) string {
	return path.Clean(string(os.PathSeparator) + fileName)
}
1200627
func (o *snapshotter) Cleanup(ctx context.Context) error {
	cleanup, err := o.cleanupDirectories(ctx)
	if err != nil {
		return err
	}

	for _, dir := range cleanup {
		if err := os.RemoveAll(dir); err != nil {
			log.G(ctx).WithError(err).WithField("path", dir).Warn("failed to remove directory")
		}
	}

	return nil
}
229637
func trimPaths(paths []cty.Path) []cty.Path {
	var trimmed []cty.Path
	for _, path := range paths {
		path = trimPath(path)
		if len(path) > 0 {
			trimmed = append(trimmed, path)
		}
	}
	return trimmed
}
528046
func (g *Group) DELETE(path string, handler HandlerFunc) {
	g.Handle("DELETE", path, handler)
}
1328664
func (fs *Filespace) RemoveAll(subPath string) error {
	return fs.root.Remove(subPath, false)
}
914602
func IsCleanSlashPath(p string) bool {
	if p == "" {
		return false
	}
	if strings.ContainsRune(p, '\\') {
		return false
	}
	if p != path.Clean(p) {
		return false
	}
	if p[0] == '/' || p == ".." || strings.HasPrefix(p, "../") {
		return false
	}
	return true
}