package storage import ( "errors" "fmt" "sync" "gitea.avt.data-center.id/othman.suseno/atlas/internal/models" ) var ( ErrSMBShareNotFound = errors.New("SMB share not found") ErrSMBShareExists = errors.New("SMB share already exists") ) // SMBStore manages SMB shares type SMBStore struct { mu sync.RWMutex shares map[string]*models.SMBShare nextID int } // NewSMBStore creates a new SMB share store func NewSMBStore() *SMBStore { return &SMBStore{ shares: make(map[string]*models.SMBShare), nextID: 1, } } // List returns all SMB shares func (s *SMBStore) List() []models.SMBShare { s.mu.RLock() defer s.mu.RUnlock() shares := make([]models.SMBShare, 0, len(s.shares)) for _, share := range s.shares { shares = append(shares, *share) } return shares } // Get returns a share by ID func (s *SMBStore) Get(id string) (*models.SMBShare, error) { s.mu.RLock() defer s.mu.RUnlock() share, ok := s.shares[id] if !ok { return nil, ErrSMBShareNotFound } return share, nil } // GetByName returns a share by name func (s *SMBStore) GetByName(name string) (*models.SMBShare, error) { s.mu.RLock() defer s.mu.RUnlock() for _, share := range s.shares { if share.Name == name { return share, nil } } return nil, ErrSMBShareNotFound } // Create creates a new SMB share func (s *SMBStore) Create(name, path, dataset, description string, readOnly, guestOK bool, validUsers []string) (*models.SMBShare, error) { s.mu.Lock() defer s.mu.Unlock() // Check if name already exists for _, share := range s.shares { if share.Name == name { return nil, ErrSMBShareExists } } id := fmt.Sprintf("smb-%d", s.nextID) s.nextID++ share := &models.SMBShare{ ID: id, Name: name, Path: path, Dataset: dataset, Description: description, ReadOnly: readOnly, GuestOK: guestOK, ValidUsers: validUsers, Enabled: true, } s.shares[id] = share return share, nil } // Update updates an existing share func (s *SMBStore) Update(id, description string, readOnly, guestOK, enabled bool, validUsers []string) error { s.mu.Lock() defer s.mu.Unlock() share, ok := s.shares[id] if !ok { return ErrSMBShareNotFound } share.Description = description share.ReadOnly = readOnly share.GuestOK = guestOK share.Enabled = enabled if validUsers != nil { share.ValidUsers = validUsers } return nil } // Delete removes a share func (s *SMBStore) Delete(id string) error { s.mu.Lock() defer s.mu.Unlock() if _, ok := s.shares[id]; !ok { return ErrSMBShareNotFound } delete(s.shares, id) return nil }