ads

package
v0.0.0-...-48317ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2025 License: BSD-1-Clause Imports: 9 Imported by: 0

README

ads

Access NTFS(New Technology File System) ADS(Alternate Data Stream) using golang.

This package provides access data streams in NTFS for files and directories with names(a.k.a Alternate Data Stream) which can be accessed by appending ":[stream name]" after file name. This also appiles to directories and reparse points, which is normally not available with cmd with commonly known methods. Also, extracting data from alternative stream is a bit complicated with cmd.

Query ADS from file

Query name and size of ADS from file

import (
	"fmt"

	"github.com/go-sw/ntfs/ads"
)

func main() {
	targetPath := "test.txt"

	fileAds, err := ads.GetFileADS(targetPath)
	if err != nil {
		panic(err)
	}

	for name, size := range fileAds.StreamInfoMap {
		fmt.Printf("name: %s, size: %d\n", name, size)
	}
}

Write, remove, rename ADS from file

import (
	"fmt"
	"os"

	"github.com/go-sw/ntfs/ads"
)

func main() {
	targetPath := "test.txt"

	ads1, err := ads.OpenFileADS(targetPath, "ads1", os.O_CREATE|os_O_WRONLY)
	ads1.Write([]byte("test ads 1"))
	ads1.Close()
	ads2, err := ads.OpenFileADS(targetPath, "ads2", os.O_CREATE|os_O_WRONLY)
	ads2.Write([]byte("test ads 2"))
	ads2.Close()
	ads3, err := ads.OpenFileADS(targetPath, "ads3", os.O_CREATE|os_O_WRONLY)
	ads3.Write([]byte("test ads 3"))
	ads3.Close()
	ads4, err := ads.OpenFileADS(targetPath, "ads4", os.O_CREATE|os_O_WRONLY)
	ads4.Write([]byte("test ads 4"))
	ads4.Close()

	// create ADS handler for file
	fileAds, err := ads.GetFileADS(targetPath)
	if err != nil {
		panic(err)
	}

	// rename ADS "ads1" to "renamed1"
	err = fileAds.RenameADS("ads1", "renamed1", true)
	if err != nil {
		panic(err)
	}

	// remove ADS "ads2"
	err = fileAds.RemoveADS("ads2")
	if err != nil {
		panic(err)
	}

	// remove all ADS from "test.txt"
	err = fileAds.RemoveAllADS()
	if err != nil {
		panic(err)
	}
}

Documentation

Rendered for windows/amd64

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoADS       = errors.New("no alternate data stream found")
	ErrUnsupported = errors.New("file system does not support stream")
)

Functions

func OpenFileADS

func OpenFileADS(path string, name string, openFlag int) (*os.File, error)

OpenFileADS opens data stream of the name from the given file with specified flag(used in os.OpenFile()), should be closed with (*os.File).Close() after use.

Types

type FileADS

type FileADS struct {
	Path          string
	StreamInfoMap *xsync.Map[string, int64]
}

FileADS handles alternate data streams of a file.

func GetFileADS

func GetFileADS(path string) (*FileADS, error)

GetFileADS returns ADS handler with a map of alternate data streams from the specified file.

func (*FileADS) CollectADS

func (a *FileADS) CollectADS() error

CollectADS collects name and size of alternate data streams of the file.

func (*FileADS) RemoveADS

func (a *FileADS) RemoveADS(name string) error

RemoveADS removes alternate data stream with the name.

func (*FileADS) RemoveAllADS

func (a *FileADS) RemoveAllADS() error

RemoveAllADS removes all alternate data streams from the file, leaving only the unnamed data stream, in which data are normally stored.

func (*FileADS) RenameADS

func (a *FileADS) RenameADS(oldName, newName string, overwrite bool) error

RenameADS renames alternate data stream with oldName to newName. If stream with newName exists, it will be overwitten if overwrite is true, otherwise return an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL