// NewStorage returns a new SFTP storage implementation at the provided
// TCP addr (host:port) in the named directory. An empty dir means ".".
// The provided SSH client configured is required.
345594
func NewStorage(addr, dir string, cc *ssh.ClientConfig) (*Storage, error) {
	if dir == "" {
		dir = "."
	}
	s := &Storage{
		addr: addr,
		root: dir,
		cc:   cc,
	}
	fs := files.NewStorage(sftpFS{s}, dir)
	s.Storage = fs
	s.SubFetcher = fs

	if err := s.adjustFDLimit(fs); err != nil {
		return nil, err
	}
	return s, nil
}
1134536
func NewServer() *Server {
	return &Server{
		clients:       make(map[uint64]*Client),
		clientUpdates: make(chan *clientUpdate),
		updates:       make(chan interface{}),
		quit:          make(chan struct{}),
	}
}
490057
func NewServer(name string, s func(...server.Option) server.Server) Option {
	return func(o *Options) {
		o.Servers[name] = s
	}
}
1006008
func NewServer(c Config) *Server {

	// os gives us a free Port when Port is ""
	ln, err := net.Listen("tcp", c.Port)
	if err != nil {
		log.Info.Panic(err)
	}

	_, port, _ := net.SplitHostPort(ln.Addr().String())

	s := Server{
		context:   c.Context,
		database:  c.Database,
		container: c.Container,
		device:    c.Device,
		Mux:       http.NewServeMux(),
		mutex:     c.Mutex,
		listener:  ln.(*net.TCPListener),
		port:      port,
		emitter:   c.Emitter,
	}

	s.setupEndpoints()

	return &s
}
688799
func newStorage(root string, maxFileSize int64, indexConf jsonconfig.Obj) (s *storage, err error) {
	fi, err := os.Stat(root)
	if os.IsNotExist(err) {
		return nil, fmt.Errorf("storage root %q doesn't exist", root)
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
	}
	if !fi.IsDir() {
		return nil, fmt.Errorf("storage root %q exists but is not a directory", root)
	}
	index, err := newIndex(root, indexConf)
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			index.Close()
		}
	}()
	if maxFileSize <= 0 {
		maxFileSize = defaultMaxFileSize
	}
	// Be consistent with trailing slashes.  Makes expvar stats for total
	// reads/writes consistent across diskpacked targets, regardless of what
	// people put in their low level config.
	root = strings.TrimRight(root, `\/`)
	s = &storage{
		root:         root,
		index:        index,
		maxFileSize:  maxFileSize,
		Generationer: local.NewGenerationer(root),
	}
	if err := s.openAllPacks(); err != nil {
		s.Close()
		return nil, err
	}
	s.mu.Lock()
	defer s.mu.Unlock()
	if _, _, err := s.StorageGeneration(); err != nil {
		return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
	}
	return s, nil
}
78149
func NewStorage(modelUUID string, session *mgo.Session) Storage {
	return stateStorage{modelUUID, session}
}
264451
func NewSocket(addr *net.UDPAddr, recvBuf int, reuseport bool) (net.PacketConn, error) {
	// default to AF_INET6 to be equivalent to net.ListenUDP()
	domain := unix.AF_INET6
	if addr.IP.To4() != nil {
		domain = unix.AF_INET
	}
	sockFD, err := unix.Socket(domain, unix.SOCK_DGRAM|syscall.SOCK_CLOEXEC|syscall.SOCK_NONBLOCK, 0)
	if err != nil {
		return nil, err
	}

	// unix.SO_REUSEPORT is not defined on linux 386/amd64, see
	// https://github.com/golang/go/issues/16075
	if reuseport {
		if err := unix.SetsockoptInt(sockFD, unix.SOL_SOCKET, 0xf, 1); err != nil {
			unix.Close(sockFD)
			return nil, err
		}
	}
	if err = unix.SetsockoptInt(sockFD, unix.SOL_SOCKET, unix.SO_RCVBUF, recvBuf); err != nil {
		unix.Close(sockFD)
		return nil, err
	}

	var sa unix.Sockaddr
	if domain == unix.AF_INET {
		sockaddr := &unix.SockaddrInet4{
			Port: addr.Port,
		}
		if copied := copy(sockaddr.Addr[:], addr.IP.To4()); copied != net.IPv4len {
			panic("did not copy enough bytes of ip address")
		}
		sa = sockaddr
	} else {
		sockaddr := &unix.SockaddrInet6{
			Port: addr.Port,
		}
		// addr.IP will be length 0 for "bind all interfaces"
		if copied := copy(sockaddr.Addr[:], addr.IP.To16()); !(copied == net.IPv6len || copied == 0) {
			panic("did not copy enough bytes of ip address")
		}
		sa = sockaddr
	}
	if err = unix.Bind(sockFD, sa); err != nil {
		unix.Close(sockFD)
		return nil, err
	}

	osFD := os.NewFile(uintptr(sockFD), "veneursock")
	// this will close the FD we passed to NewFile
	defer osFD.Close()

	// however, FilePacketConn duplicates the FD, so closing the File's FD does
	// not affect this object's FD
	ret, err := net.FilePacketConn(osFD)
	if err != nil {
		return nil, err
	}
	return ret, nil
}
685833
func NewServer(basePath, exportedName, network string) (*Server, error) {

	if len(exportedName) < 2 || strings.Contains(exportedName, "/") {
		return nil, ErrInvalidExportedName
	}
	if len(basePath) < 2 {
		return nil, ErrInvalidBasePath
	}
	if err := verifyExportsDir(basePath); err != nil {
		return nil, err
	}
	exportedNamePath := filepath.Join(exportsDir, exportedName)
	if err := verifyExportsDir(exportedNamePath); err != nil {
		return nil, err
	}
	if err := mp.Unmount(exportedNamePath); err != nil {
		glog.Errorf("Could not unmount export directory %s: %s", exportedNamePath, err)
		return nil, err
	}
	if _, _, err := net.ParseCIDR(network); err != nil {
		return nil, ErrInvalidNetwork
	}
	if err := start(); err != nil {
		return nil, err
	}
	return &Server{
		basePath:         basePath,
		exportedName:     exportedName,
		exportedNamePath: exportedNamePath,
		exportOptions:    "rw,insecure,no_subtree_check,async",
		clients:          make(map[string]struct{}),
		network:          network,
		volumes:          make(map[string]int32),
		exported:         make(map[string]struct{}),
		clientValidator:  nil,
	}, nil
}
1166448
func NewServer(store *store.MemoryStore, raft *raft.Node, securityConfig *ca.SecurityConfig, pg plugingetter.PluginGetter, dr *drivers.DriverProvider) *Server {
	return &Server{
		store:          store,
		dr:             dr,
		raft:           raft,
		securityConfig: securityConfig,
		pg:             pg,
	}
}
1203233
func NewSessionArchive(dataDir, serverID, namespace string, sessionID session.ID) (io.ReadCloser, error) {
	index, err := readSessionIndex(
		dataDir, []string{serverID}, namespace, sessionID)
	if err != nil {
		return nil, trace.Wrap(err)
	}

	// io.Pipe allows to generate the archive part by part
	// without writing to disk or generating it in memory
	// at the pace which reader is ready to consume it
	reader, writer := io.Pipe()
	tarball := tar.NewWriter(writer)
	go func() {
		if err := writeSessionArchive(index, tarball, writer); err != nil {
			log.Warningf("Failed to write archive: %v.", trace.DebugReport(err))
		}
	}()

	return reader, nil
}
1061264
func NewServer() *Server {
	if f := flag.Lookup("toolbox.trace"); f != nil {
		Trace, _ = strconv.ParseBool(f.Value.String())
	}

	s := &Server{
		sessions: make(map[uint64]*session),
		schemes:  make(map[string]FileHandler),
		chmod:    os.Chmod,
		chown:    os.Chown,
	}

	s.handlers = map[int32]func(*Packet) (interface{}, error){
		OpCreateSessionV4:  s.CreateSessionV4,
		OpDestroySessionV4: s.DestroySessionV4,
		OpGetattrV2:        s.GetattrV2,
		OpSetattrV2:        s.SetattrV2,
		OpOpen:             s.Open,
		OpClose:            s.Close,
		OpOpenV3:           s.OpenV3,
		OpReadV3:           s.ReadV3,
		OpWriteV3:          s.WriteV3,
	}

	for op := range s.handlers {
		s.Capabilities = append(s.Capabilities, Capability{Op: op, Flags: 0x1})
	}

	return s
}
1144283
func NewSQLStorage(dialect string, args ...interface{}) (*SQLStorage, error) {
	gormDB, err := gorm.Open(dialect, args...)
	if err != nil {
		return nil, err
	}
	return &SQLStorage{
		DB: *gormDB,
	}, nil
}
7300
func NewStorageProviderRegistry(reg storage.ProviderRegistry) storage.ProviderRegistry {
	return storage.ChainedProviderRegistry{reg, provider.CommonStorageProviders()}
}
771204
func NewServer(loader Loader) transport.Transport {
	return &server{
		loader,
		&handler{asClient: false},
	}
}
843188
func NewServer(archiver pkgarchive.Archiver, opts ServerOptions) Server {
	return newServer(archiver, opts)
}
548341
func (c *StorageCommandBase) NewStorageAPI() (*storage.Client, error) {
	root, err := c.NewAPIRoot()
	if err != nil {
		return nil, err
	}
	return storage.NewClient(root), nil
}
412298
func NewServer(apiDef *raml.APIDefinition, apiDocsDir string, targetDir string) *Server {
	resources := getServerResourcesDefs(apiDef)
	return &Server{
		apiDef:     apiDef,
		apiDocsDir: apiDocsDir,
		TargetDir:  targetDir,
		Resources:  resources,
	}
}