Source File
dir.go
Belonging Package
os
// Copyright 2016 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package osimport ()type readdirMode intconst (readdirName readdirMode = iotareaddirDirEntryreaddirFileInfo)// Readdir reads the contents of the directory associated with file and// returns a slice of up to n [FileInfo] values, as would be returned// by [Lstat], in directory order. Subsequent calls on the same file will yield// further FileInfos.//// If n > 0, Readdir returns at most n FileInfo structures. In this case, if// Readdir returns an empty slice, it will return a non-nil error// explaining why. At the end of a directory, the error is [io.EOF].//// If n <= 0, Readdir returns all the FileInfo from the directory in// a single slice. In this case, if Readdir succeeds (reads all// the way to the end of the directory), it returns the slice and a// nil error. If it encounters an error before the end of the// directory, Readdir returns the FileInfo read until that point// and a non-nil error.//// Most clients are better served by the more efficient ReadDir method.func ( *File) ( int) ([]FileInfo, error) {if == nil {return nil, ErrInvalid}, , , := .readdir(, readdirFileInfo)if == nil {// Readdir has historically always returned a non-nil empty slice, never nil,// even on error (except misuse with nil receiver above).// Keep it that way to avoid breaking overly sensitive callers.= []FileInfo{}}return ,}// Readdirnames reads the contents of the directory associated with file// and returns a slice of up to n names of files in the directory,// in directory order. Subsequent calls on the same file will yield// further names.//// If n > 0, Readdirnames returns at most n names. In this case, if// Readdirnames returns an empty slice, it will return a non-nil error// explaining why. At the end of a directory, the error is [io.EOF].//// If n <= 0, Readdirnames returns all the names from the directory in// a single slice. In this case, if Readdirnames succeeds (reads all// the way to the end of the directory), it returns the slice and a// nil error. If it encounters an error before the end of the// directory, Readdirnames returns the names read until that point and// a non-nil error.func ( *File) ( int) ( []string, error) {if == nil {return nil, ErrInvalid}, _, _, = .readdir(, readdirName)if == nil {// Readdirnames has historically always returned a non-nil empty slice, never nil,// even on error (except misuse with nil receiver above).// Keep it that way to avoid breaking overly sensitive callers.= []string{}}return ,}// A DirEntry is an entry read from a directory// (using the [ReadDir] function or a [File.ReadDir] method).type DirEntry = fs.DirEntry// ReadDir reads the contents of the directory associated with the file f// and returns a slice of [DirEntry] values in directory order.// Subsequent calls on the same file will yield later DirEntry records in the directory.//// If n > 0, ReadDir returns at most n DirEntry records.// In this case, if ReadDir returns an empty slice, it will return an error explaining why.// At the end of a directory, the error is [io.EOF].//// If n <= 0, ReadDir returns all the DirEntry records remaining in the directory.// When it succeeds, it returns a nil error (not io.EOF).func ( *File) ( int) ([]DirEntry, error) {if == nil {return nil, ErrInvalid}, , , := .readdir(, readdirDirEntry)if == nil {// Match Readdir and Readdirnames: don't return nil slices.= []DirEntry{}}return ,}// testingForceReadDirLstat forces ReadDir to call Lstat, for testing that code path.// This can be difficult to provoke on some Unix systems otherwise.var testingForceReadDirLstat bool// ReadDir reads the named directory,// returning all its directory entries sorted by filename.// If an error occurs reading the directory,// ReadDir returns the entries it was able to read before the error,// along with the error.func ( string) ([]DirEntry, error) {, := openDir()if != nil {return nil,}defer .Close(), := .ReadDir(-1)slices.SortFunc(, func(, DirEntry) int {return bytealg.CompareString(.Name(), .Name())})return ,}// CopyFS copies the file system fsys into the directory dir,// creating dir if necessary.//// Files are created with mode 0o666 plus any execute permissions// from the source, and directories are created with mode 0o777// (before umask).//// CopyFS will not overwrite existing files. If a file name in fsys// already exists in the destination, CopyFS will return an error// such that errors.Is(err, fs.ErrExist) will be true.//// Symbolic links in fsys are not supported. A *PathError with Err set// to ErrInvalid is returned when copying from a symbolic link.//// Symbolic links in dir are followed.//// Copying stops at and returns the first error encountered.func ( string, fs.FS) error {return fs.WalkDir(, ".", func( string, fs.DirEntry, error) error {if != nil {return}, := filepathlite.Localize()if != nil {return}:= joinPath(, )if .IsDir() {return MkdirAll(, 0777)}// TODO(panjf2000): handle symlinks with the help of fs.ReadLinkFS// once https://go.dev/issue/49580 is done.// we also need filepathlite.IsLocal from https://go.dev/cl/564295.if !.Type().IsRegular() {return &PathError{Op: "CopyFS", Path: , Err: ErrInvalid}}, := .Open()if != nil {return}defer .Close(), := .Stat()if != nil {return}, := OpenFile(, O_CREATE|O_EXCL|O_WRONLY, 0666|.Mode()&0777)if != nil {return}if , := io.Copy(, ); != nil {.Close()return &PathError{Op: "Copy", Path: , Err: }}return .Close()})}
![]() |
The pages are generated with Golds v0.7.6. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |