// Copyright 2024 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 txtar

import (
	
	
	
	
	
	
	
)

// FS returns the file system form of an Archive.
// It returns an error if any of the file names in the archive
// are not valid file system names.
// The archive must not be modified while the FS is in use.
//
// If the file system detects that it has been modified, calls to the
// file system return an ErrModified error.
func ( *Archive) (fs.FS, error) {
	// Create a filesystem with a root directory.
	 := &node{fileinfo: fileinfo{path: ".", mode: readOnlyDir}}
	 := &filesystem{, map[string]*node{.path: }}

	if  := initFiles();  != nil {
		return nil, fmt.Errorf("cannot create fs.FS from txtar.Archive: %s", )
	}
	return , nil
}

const (
	readOnly    fs.FileMode = 0o444 // read only mode
	readOnlyDir             = readOnly | fs.ModeDir
)

// ErrModified indicates that file system returned by FS
// noticed that the underlying archive has been modified
// since the call to FS. Detection of modification is best effort,
// to help diagnose misuse of the API, and is not guaranteed.
var ErrModified error = errors.New("txtar.Archive has been modified during txtar.FS")

// A filesystem is a simple in-memory file system for txtar archives,
// represented as a map from valid path names to information about the
// files or directories they represent.
//
// File system operations are read only. Modifications to the underlying
// *Archive may race. To help prevent this, the filesystem tries
// to detect modification during Open and return ErrModified if it
// is able to detect a modification.
type filesystem struct {
	ar    *Archive
	nodes map[string]*node
}

// node is a file or directory in the tree of a filesystem.
type node struct {
	fileinfo               // fs.FileInfo and fs.DirEntry implementation
	idx      int           // index into ar.Files (for files)
	entries  []fs.DirEntry // subdirectories and files (for directories)
}

var _ fs.FS = (*filesystem)(nil)
var _ fs.DirEntry = (*node)(nil)

// initFiles initializes fsys from fsys.ar.Files. Returns an error if there are any
// invalid file names or collisions between file or directories.
func ( *filesystem) error {
	for ,  := range .ar.Files {
		 := .Name
		if !fs.ValidPath() {
			return fmt.Errorf("file %q is an invalid path", )
		}

		 := &node{idx: , fileinfo: fileinfo{path: , size: len(.Data), mode: readOnly}}
		if  := insert(, );  != nil {
			return 
		}
	}
	return nil
}

// insert adds node n as an entry to its parent directory within the filesystem.
func ( *filesystem,  *node) error {
	if  := .nodes[.path];  != nil {
		return fmt.Errorf("duplicate path %q", .path)
	}
	.nodes[.path] = 

	// fsys.nodes contains "." to prevent infinite loops.
	,  := directory(, path.Dir(.path))
	if  != nil {
		return 
	}
	.entries = append(.entries, )
	return nil
}

// directory returns the directory node with the path dir and lazily-creates it
// if it does not exist.
func ( *filesystem,  string) (*node, error) {
	if  := .nodes[];  != nil && .IsDir() {
		return , nil // pre-existing directory
	}

	 := &node{fileinfo: fileinfo{path: , mode: readOnlyDir}}
	if  := insert(, );  != nil {
		return nil, 
	}
	return , nil
}

// dataOf returns the data associated with the file t.
// May return ErrModified if fsys.ar has been modified.
func ( *filesystem,  *node) ([]byte, error) {
	if .idx >= len(.ar.Files) {
		return nil, ErrModified
	}

	 := .ar.Files[.idx]
	if .Name != .path || len(.Data) != .size {
		return nil, ErrModified
	}
	return .Data, nil
}

func ( *filesystem) ( string) (fs.File, error) {
	if !fs.ValidPath() {
		return nil, &fs.PathError{Op: "open", Path: , Err: fs.ErrInvalid}
	}

	 := .nodes[]
	switch {
	case  == nil:
		return nil, &fs.PathError{Op: "open", Path: , Err: fs.ErrNotExist}
	case .IsDir():
		return &openDir{fileinfo: .fileinfo, entries: .entries}, nil
	default:
		,  := dataOf(, )
		if  != nil {
			return nil, 
		}
		return &openFile{fileinfo: .fileinfo, data: }, nil
	}
}

func ( *filesystem) ( string) ([]byte, error) {
	,  := .Open()
	if  != nil {
		return nil, 
	}
	if ,  := .(*openFile);  {
		return slices.Clone(.data), nil
	}
	return nil, &fs.PathError{Op: "read", Path: , Err: fs.ErrInvalid}
}

// A fileinfo implements fs.FileInfo and fs.DirEntry for a given archive file.
type fileinfo struct {
	path string // unique path to the file or directory within a filesystem
	size int
	mode fs.FileMode
}

var _ fs.FileInfo = (*fileinfo)(nil)
var _ fs.DirEntry = (*fileinfo)(nil)

func ( *fileinfo) () string               { return path.Base(.path) }
func ( *fileinfo) () int64                { return int64(.size) }
func ( *fileinfo) () fs.FileMode          { return .mode }
func ( *fileinfo) () fs.FileMode          { return .mode.Type() }
func ( *fileinfo) () time.Time         { return time.Time{} }
func ( *fileinfo) () bool                { return .mode&fs.ModeDir != 0 }
func ( *fileinfo) () any                   { return nil }
func ( *fileinfo) () (fs.FileInfo, error) { return , nil }

// An openFile is a regular (non-directory) fs.File open for reading.
type openFile struct {
	fileinfo
	data   []byte
	offset int64
}

var _ fs.File = (*openFile)(nil)

func ( *openFile) () (fs.FileInfo, error) { return &.fileinfo, nil }
func ( *openFile) () error               { return nil }
func ( *openFile) ( []byte) (int, error) {
	if .offset >= int64(len(.data)) {
		return 0, io.EOF
	}
	if .offset < 0 {
		return 0, &fs.PathError{Op: "read", Path: .path, Err: fs.ErrInvalid}
	}
	 := copy(, .data[.offset:])
	.offset += int64()
	return , nil
}

func ( *openFile) ( int64,  int) (int64, error) {
	switch  {
	case 0:
		// offset += 0
	case 1:
		 += .offset
	case 2:
		 += int64(len(.data))
	}
	if  < 0 ||  > int64(len(.data)) {
		return 0, &fs.PathError{Op: "seek", Path: .path, Err: fs.ErrInvalid}
	}
	.offset = 
	return , nil
}

func ( *openFile) ( []byte,  int64) (int, error) {
	if  < 0 ||  > int64(len(.data)) {
		return 0, &fs.PathError{Op: "read", Path: .path, Err: fs.ErrInvalid}
	}
	 := copy(, .data[:])
	if  < len() {
		return , io.EOF
	}
	return , nil
}

// A openDir is a directory fs.File (so also an fs.ReadDirFile) open for reading.
type openDir struct {
	fileinfo
	entries []fs.DirEntry
	offset  int
}

var _ fs.ReadDirFile = (*openDir)(nil)

func ( *openDir) () (fs.FileInfo, error) { return &.fileinfo, nil }
func ( *openDir) () error               { return nil }
func ( *openDir) ( []byte) (int, error) {
	return 0, &fs.PathError{Op: "read", Path: .path, Err: fs.ErrInvalid}
}

func ( *openDir) ( int) ([]fs.DirEntry, error) {
	 := len(.entries) - .offset
	if  == 0 &&  > 0 {
		return nil, io.EOF
	}
	if  > 0 &&  >  {
		 = 
	}
	 := make([]fs.DirEntry, )
	copy(, .entries[.offset:.offset+])
	.offset += 
	return , nil
}