package samba import ( "bufio" "fmt" "os" "strings" ) /* Samba Share Warpper Note that this module only provide exposing of local disk / storage. This module do not handle providing the virtualized interface for samba */ type SambaConfigEditor struct { SambaConfigPath string } // AppendSambaShareConfig appends the Samba share configuration to smb.conf func (s *SambaConfigEditor) AppendSambaShareConfig(config string) error { file, err := os.OpenFile("/etc/samba/smb.conf", os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return err } defer file.Close() if _, err := file.WriteString(config); err != nil { return err } return nil } // RemoveSambaShareConfig removes the Samba share configuration from smb.conf func (s *SambaConfigEditor) RemoveSambaShareConfig(shareName string) error { // Open the smb.conf file for reading file, err := os.Open("/etc/samba/smb.conf") if err != nil { return err } defer file.Close() // Create a temporary file to store modified smb.conf tmpFile, err := os.CreateTemp("", "smb.conf.*.tmp") if err != nil { return err } defer tmpFile.Close() // Create a scanner to read the smb.conf file line by line scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() // Check if the line contains the share name if strings.HasPrefix(line, "["+shareName+"]") { // Skip the lines until the next section for scanner.Scan() { if strings.HasPrefix(scanner.Text(), "[") { break } } continue // Skip writing the share configuration to the temporary file } // Write the line to the temporary file _, err := fmt.Fprintln(tmpFile, line) if err != nil { return err } } // Check for scanner errors if err := scanner.Err(); err != nil { return err } // Close the original smb.conf file if err := file.Close(); err != nil { return err } // Close the temporary file if err := tmpFile.Close(); err != nil { return err } // Replace the original smb.conf file with the temporary file if err := os.Rename(tmpFile.Name(), "/etc/samba/smb.conf"); err != nil { return err } return nil }