Source file src/os/removeall_noat.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (js && wasm) || plan9
     6  
     7  package os
     8  
     9  import (
    10  	"io"
    11  	"runtime"
    12  	"syscall"
    13  )
    14  
    15  func removeAll(path string) error {
    16  	if path == "" {
    17  		// fail silently to retain compatibility with previous behavior
    18  		// of RemoveAll. See issue 28830.
    19  		return nil
    20  	}
    21  
    22  	// Consistency with Root.RemoveAll: Strip trailing /s from the path,
    23  	// so RemoveAll("not_a_directory/") succeeds.
    24  	for len(path) > 1 && IsPathSeparator(path[len(path)-1]) {
    25  		path = path[:len(path)-1]
    26  	}
    27  
    28  	// The rmdir system call permits removing "." on Plan 9,
    29  	// so we don't permit it to remain consistent with the
    30  	// "at" implementation of RemoveAll.
    31  	if endsWithDot(path) {
    32  		return &PathError{Op: "RemoveAll", Path: path, Err: syscall.EINVAL}
    33  	}
    34  
    35  	// Simple case: if Remove works, we're done.
    36  	err := Remove(path)
    37  	if err == nil || IsNotExist(err) {
    38  		return nil
    39  	}
    40  
    41  	// Otherwise, is this a directory we need to recurse into?
    42  	dir, serr := Lstat(path)
    43  	if serr != nil {
    44  		if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
    45  			return nil
    46  		}
    47  		return serr
    48  	}
    49  	if !dir.IsDir() {
    50  		// Not a directory; return the error from Remove.
    51  		return err
    52  	}
    53  
    54  	// Remove contents & return first error.
    55  	err = nil
    56  	for {
    57  		fd, err := Open(path)
    58  		if err != nil {
    59  			if IsNotExist(err) {
    60  				// Already deleted by someone else.
    61  				return nil
    62  			}
    63  			return err
    64  		}
    65  
    66  		const reqSize = 1024
    67  		var names []string
    68  		var readErr error
    69  
    70  		for {
    71  			numErr := 0
    72  			names, readErr = fd.Readdirnames(reqSize)
    73  
    74  			for _, name := range names {
    75  				err1 := RemoveAll(path + string(PathSeparator) + name)
    76  				if err == nil {
    77  					err = err1
    78  				}
    79  				if err1 != nil {
    80  					numErr++
    81  				}
    82  			}
    83  
    84  			// If we can delete any entry, break to start new iteration.
    85  			// Otherwise, we discard current names, get next entries and try deleting them.
    86  			if numErr != reqSize {
    87  				break
    88  			}
    89  		}
    90  
    91  		// Removing files from the directory may have caused
    92  		// the OS to reshuffle it. Simply calling Readdirnames
    93  		// again may skip some entries. The only reliable way
    94  		// to avoid this is to close and re-open the
    95  		// directory. See issue 20841.
    96  		fd.Close()
    97  
    98  		if readErr == io.EOF {
    99  			break
   100  		}
   101  		// If Readdirnames returned an error, use it.
   102  		if err == nil {
   103  			err = readErr
   104  		}
   105  		if len(names) == 0 {
   106  			break
   107  		}
   108  
   109  		// We don't want to re-open unnecessarily, so if we
   110  		// got fewer than request names from Readdirnames, try
   111  		// simply removing the directory now. If that
   112  		// succeeds, we are done.
   113  		if len(names) < reqSize {
   114  			err1 := Remove(path)
   115  			if err1 == nil || IsNotExist(err1) {
   116  				return nil
   117  			}
   118  
   119  			if err != nil {
   120  				// We got some error removing the
   121  				// directory contents, and since we
   122  				// read fewer names than we requested
   123  				// there probably aren't more files to
   124  				// remove. Don't loop around to read
   125  				// the directory again. We'll probably
   126  				// just get the same error.
   127  				return err
   128  			}
   129  		}
   130  	}
   131  
   132  	// Remove directory.
   133  	err1 := Remove(path)
   134  	if err1 == nil || IsNotExist(err1) {
   135  		return nil
   136  	}
   137  	if runtime.GOOS == "windows" && IsPermission(err1) {
   138  		if fs, err := Stat(path); err == nil {
   139  			if err = Chmod(path, FileMode(0200|int(fs.Mode()))); err == nil {
   140  				err1 = Remove(path)
   141  			}
   142  		}
   143  	}
   144  	if err == nil {
   145  		err = err1
   146  	}
   147  	return err
   148  }
   149  

View as plain text