Categorygithub.com/cloudsoda/go-smb2
modulepackage
0.0.0-20231124195312-f3ec8ae2c891
Repository: https://github.com/cloudsoda/go-smb2.git
Documentation: pkg.go.dev

# README

smb2

Build Status Go Reference

Description

An SMB2/3 client implementation. This is a fork of the project github.com/hirochachacha/go-smb2. Any releases will be pre-1.0.0 for some time as features and bug fixes are implemented.

Installation

go get github.com/cloudsoda/go-smb2

Documentation

https://pkg.go.dev/github.com/cloudsoda/go-smb2

Examples

List share names

package main

import (
	"fmt"
	"net"

	"github.com/cloudsoda/go-smb2"
)

func main() {
	d := &smb2.Dialer{
		Initiator: &smb2.NTLMInitiator{
			User:     "USERNAME",
			Password: "PASSWORD",
		},
	}

	s, err := d.Dial(context.Background(), "SERVERNAME:445")
	if err != nil {
		panic(err)
	}
	defer s.Logoff()

	names, err := s.ListSharenames()
	if err != nil {
		panic(err)
	}

	for _, name := range names {
		fmt.Println(name)
	}
}

File manipulation

package main

import (
	"io"
	"net"

	"github.com/cloudsoda/go-smb2"
)

func main() {
	d := &smb2.Dialer{
		Initiator: &smb2.NTLMInitiator{
			User:     "USERNAME",
			Password: "PASSWORD",
		},
	}

	s, err := d.Dial(context.Background(), "SERVERNAME:445")
	if err != nil {
		panic(err)
	}
	defer s.Logoff()

	fs, err := s.Mount("SHARENAME")
	if err != nil {
		panic(err)
	}
	defer fs.Umount()

	f, err := fs.Create("hello.txt")
	if err != nil {
		panic(err)
	}
	defer fs.Remove("hello.txt")
	defer f.Close()

	_, err = f.Write([]byte("Hello world!"))
	if err != nil {
		panic(err)
	}

	_, err = f.Seek(0, io.SeekStart)
	if err != nil {
		panic(err)
	}

	bs, err := io.ReadAll(f)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(bs))
}

Check error types

package main

import (
	"context"
	"fmt"
	"net"
	"os"

	"github.com/cloudsoda/go-smb2"
)

func main() {
	d := &smb2.Dialer{
		Initiator: &smb2.NTLMInitiator{
			User:     "USERNAME",
			Password: "PASSWORD",
		},
	}

	s, err := d.Dial(context.Background(), "SERVERNAME:445")
	if err != nil {
		panic(err)
	}
	defer s.Logoff()

	fs, err := s.Mount("SHARENAME")
	if err != nil {
		panic(err)
	}
	defer fs.Umount()

	_, err = fs.Open("notExist.txt")

	fmt.Println(os.IsNotExist(err)) // true
	fmt.Println(os.IsExist(err))    // false

	fs.WriteFile("hello2.txt", []byte("test"), 0444)
	err = fs.WriteFile("hello2.txt", []byte("test2"), 0444)
	fmt.Println(os.IsPermission(err)) // true

	ctx, cancel := context.WithTimeout(context.Background(), 0)
	defer cancel()

	_, err = fs.WithContext(ctx).Open("hello.txt")

	fmt.Println(os.IsTimeout(err)) // true
}

Glob and WalkDir through FS interface

package main

import (
	"fmt"
	"net"
	iofs "io/fs"

	"github.com/cloudsoda/go-smb2"
)

func main() {
	d := &smb2.Dialer{
		Initiator: &smb2.NTLMInitiator{
			User:     "USERNAME",
			Password: "PASSWORD",
		},
	}

	s, err := d.Dial(context.Background(), "SERVERNAME:445")
	if err != nil {
		panic(err)
	}
	defer s.Logoff()

	fs, err := s.Mount("SHARENAME")
	if err != nil {
		panic(err)
	}
	defer fs.Umount()

	matches, err := iofs.Glob(fs.DirFS("."), "*")
	if err != nil {
		panic(err)
	}
	for _, match := range matches {
		fmt.Println(match)
	}

	err = iofs.WalkDir(fs.DirFS("."), ".", func(path string, d iofs.DirEntry, err error) error {
		fmt.Println(path, d, err)

		return nil
	})
	if err != nil {
		panic(err)
	}
}

# Functions

No description provided by the author
Match reports whether name matches the shell file name pattern.
The mount will apply character mapping to file names with reserved characters equivalent to samba's `mapchars` option.
The mount will apply character mapping to file names with reserved characters equivalent to samba's 'mapposix' option.

# Constants

Don't map reserved characters.
Map reserved characters using the Services for Mac scheme.
Map reserved characters using the Services for Unix scheme.
deprecated constant.
No description provided by the author

# Variables

ErrBadPattern indicates a pattern was malformed.
normalize path arguments automatically.

# Structs

Dialer contains options for func (*Dialer) Dial.
No description provided by the author
No description provided by the author
InternalError represents internal error.
InvalidResponseError represents a data sent by the server is corrupted or unexpected.
Negotiator contains options for func (*Dialer) Dial.
NTLMInitiator implements session-setup through NTLMv2.
ResponseError represents a error with a nt status code sent by the server.
Session represents a SMB session.
Share represents a SMB tree connection with VFS interface.
TransportError represents a error come from net.Conn layer.

# Interfaces

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
Mapping strategies that can be used when a reserved character is encountered in a file name.
configures a Session.Mount() call.
No description provided by the author
No description provided by the author
No description provided by the author