MCPcopy
hub / github.com/caddyserver/caddy / funcListFiles

Method funcListFiles

modules/caddyhttp/templates/tplcontext.go:393–423  ·  view source on GitHub ↗

funcListFiles reads and returns a slice of names from the given directory relative to the root of c.

(name string)

Source from the content-addressed store, hash-verified

391// funcListFiles reads and returns a slice of names from the given
392// directory relative to the root of c.
393func (c TemplateContext) funcListFiles(name string) ([]string, error) {
394 if c.Root == nil {
395 return nil, fmt.Errorf("root file system not specified")
396 }
397
398 dir, err := c.Root.Open(path.Clean(name))
399 if err != nil {
400 return nil, err
401 }
402 defer dir.Close()
403
404 stat, err := dir.Stat()
405 if err != nil {
406 return nil, err
407 }
408 if !stat.IsDir() {
409 return nil, fmt.Errorf("%v is not a directory", name)
410 }
411
412 dirInfo, err := dir.Readdir(0)
413 if err != nil {
414 return nil, err
415 }
416
417 names := make([]string, len(dirInfo))
418 for i, fileInfo := range dirInfo {
419 names[i] = fileInfo.Name()
420 }
421
422 return names, nil
423}
424
425// funcFileExists returns true if filename can be opened successfully.
426func (c TemplateContext) funcFileExists(filename string) (bool, error) {

Callers 1

TestFileListingFunction · 0.80

Calls 4

OpenMethod · 0.80
StatMethod · 0.80
CloseMethod · 0.45
NameMethod · 0.45

Tested by 1

TestFileListingFunction · 0.64