/*
Deletes the specified files, but only if they are new (have not yet
been incref'd).
*/
354503
func (fd *IndexFileDeleter) deleteNewFiles(files []string) {
	// assert locked
	for _, filename := range files {
		// NOTE: it's very unusual yet possible for the
		// refCount to be present and 0: it can happen if you
		// open IW on a crashed index, and it removes a bunch
		// of unref'd files, and then you add new docs / do
		// merging, and it reuses that segment name.
		// TestCrash.testCrashAfterReopen can hit this:
		if rf, ok := fd.refCounts[filename]; !ok || rf.count == 0 {
			if fd.infoStream.IsEnabled("IFD") {
				fd.infoStream.Message("IFD", "delete new file '%v'", filename)
			}
			fd.deleteFile(filename)
		}
	}
}
63427
func Recycle(dir string) error {
	return newWalker(func(path string, info os.FileInfo) error {
		// Leave the root dir alone
		if path == dir {
			return nil
		}

		// Delete all subfiles/subdirs
		return os.Remove(path)
	}).Walk(dir)
}
394181
func (e *Engine) cleanup() error {
	allfiles, err := ioutil.ReadDir(e.path)
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return err
	}

	ext := fmt.Sprintf(".%s", TmpTSMFileExtension)
	for _, f := range allfiles {
		// Check to see if there are any `.tmp` directories that were left over from failed shard snapshots
		if f.IsDir() && strings.HasSuffix(f.Name(), ext) {
			if err := os.RemoveAll(filepath.Join(e.path, f.Name())); err != nil {
				return fmt.Errorf("error removing tmp snapshot directory %q: %s", f.Name(), err)
			}
		}
	}

	return e.cleanupTempTSMFiles()
}
440270
func (s *sentDownloadState) cleanup(folder string) []protocol.FileDownloadProgressUpdate {
	fs, ok := s.folderStates[folder]
	if ok {
		updates := fs.destroy()
		delete(s.folderStates, folder)
		return updates
	}
	return nil
}
894250
func (d *DiskCache) Del(pathStr string) {
	pathStr = path.Join(d.baseDir, pathStr)
	matches, err := filepath.Glob(pathStr)
	if err != nil {
		return
	}

	// When recursively deleting directories from the cache, it's possible
	// that d.fileList will become out of sync with reality. But that's ok
	// since it just exists to prevent disk access on cache misses, so will
	// just result in a bit of extra disk access.

	d.lock.Lock()
	if pathStr == "*" {
		// Due to the above comment, special case when wiping out the entire
		// cache.
		d.fileList = make(map[string]int)
	}

	for _, match := range matches {
		delete(d.fileList, match)
		err = os.RemoveAll(match)
		if err != nil {
			// log removal failure
		}
	}
	d.lock.Unlock()

}
692965
func (file *DataFile) Clear() (err error) {
	if err = file.Close(); err != nil {
		return
	} else if err = os.Truncate(file.Path, 0); err != nil {
		return
	} else if file.Fh, err = os.OpenFile(file.Path, os.O_CREATE|os.O_RDWR, 0600); err != nil {
		return
	} else if err = file.overwriteWithZero(0, file.Growth); err != nil {
		return
	} else if file.Buf, err = gommap.Map(file.Fh); err != nil {
		return
	}
	file.Used, file.Size = 0, file.Growth
	tdlog.Infof("%s cleared: %d of %d bytes in-use", file.Path, file.Used, file.Size)
	return
}
197991
func (s *sentFolderDownloadState) destroy() []protocol.FileDownloadProgressUpdate {
	updates := make([]protocol.FileDownloadProgressUpdate, 0, len(s.files))
	for name, info := range s.files {
		updates = append(updates, protocol.FileDownloadProgressUpdate{
			Name:       name,
			Version:    info.version,
			UpdateType: protocol.UpdateTypeForget,
		})
		delete(s.files, name)
	}
	return updates
}
468507
func deleteFile(filename string) error {
	if fileExists(filename) {
		return os.Remove(filename)
	}
	return nil
}
966295
func Delete(path string, h interface{}, m ...interface{}) {
	Default.Delete(path, h, m...)
}
1368347
func (d *FSDirectory) DeleteFile(name string) (err error) {
	d.EnsureOpen()
	if err = os.Remove(filepath.Join(d.path, name)); err == nil {
		d.staleFilesLock.Lock()
		defer d.staleFilesLock.Unlock()
		delete(d.staleFiles, name)
	}
	return
}
360504
func (c *Conf) Cleanup() (err error) {
	err = os.RemoveAll(c.dirPath)
	return
}
681169
func (f *FileStorage) Flush() error {
	defer f.subscription.NotifyAll(0)
	return f.file.Sync()
}
989162
func (s *Store) Delete(ctx context.Context, name string) error {
	path := filepath.Join(s.path, filepath.Clean(name))
	out.Debug(ctx, "fs.Delete(%s) - %s", name, path)

	if err := os.Remove(path); err != nil {
		return err
	}

	return s.removeEmptyParentDirectories(path)
}
157376
func DeferDelete(path string) {
	tempFilesMu.Lock()
	tempFiles = append(tempFiles, path)
	tempFilesMu.Unlock()
}
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)
	}
}
403890
func (f *FileStorage) Clean() error {
	f.subscription.WaitAll() // Wait until every subscriber ends waiting every result
	return os.Remove(f.file.Name())
}
729881
func (self *TimedRotatingFileHandler) getFilesToDelete() ([]string, error) {
	dirName, baseName := filepath.Split(self.GetFilePath())
	fileInfos, err := ioutil.ReadDir(dirName)
	if err != nil {
		return nil, err
	}
	prefix := baseName + "."
	pattern, err := regexp.Compile(self.extMatch)
	if err != nil {
		return nil, err
	}
	var fileNames []string
	for _, info := range fileInfos {
		fileName := info.Name()
		if strings.HasPrefix(fileName, prefix) {
			suffix := fileName[len(prefix):]
			if pattern.MatchString(suffix) {
				fileNames = append(fileNames, fileName)
			}
		}
	}
	// no need to sort fileNames since ioutil.ReadDir() returns sorted list.
	var result []string
	if uint32(len(fileNames)) < self.backupCount {
		return result, nil
	}
	result = fileNames[:uint32(len(fileNames))-self.backupCount]
	for i := 0; i < len(result); i++ {
		result[i] = filepath.Join(dirName, result[i])
	}
	return result, nil
}