浏览代码

Added wip samba wrapper

Toby Chui 1 年之前
父节点
当前提交
286fe904bc
共有 2 个文件被更改,包括 110 次插入12 次删除
  1. 98 0
      mod/fileservers/servers/samba/samba.go
  2. 12 12
      network.go

+ 98 - 0
mod/fileservers/servers/samba/samba.go

@@ -0,0 +1,98 @@
+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
+}

+ 12 - 12
network.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"encoding/json"
-	"fmt"
 	"net/http"
 	"strconv"
 	"strings"
@@ -19,7 +18,6 @@ import (
 	ssdp "imuslab.com/arozos/mod/network/ssdp"
 	upnp "imuslab.com/arozos/mod/network/upnp"
 	"imuslab.com/arozos/mod/network/websocket"
-	"imuslab.com/arozos/mod/permission"
 	prout "imuslab.com/arozos/mod/prouter"
 	"imuslab.com/arozos/mod/utils"
 	"imuslab.com/arozos/mod/www"
@@ -328,16 +326,18 @@ func FileServerInit() {
 	})
 
 	//NFS
-	NFSManager = nfsserv.NewNfsServer(nfsserv.Option{
-		UserManager:      userHandler,
-		ListeningPort:    2049,
-		AllowAccessGroup: []*permission.PermissionGroup{},
-		Logger:           nil,
-	})
-	err := NFSManager.Start()
-	if err != nil {
-		fmt.Println(err.Error())
-	}
+	/*
+		NFSManager = nfsserv.NewNfsServer(nfsserv.Option{
+			UserManager:      userHandler,
+			ListeningPort:    2049,
+			AllowAccessGroup: []*permission.PermissionGroup{},
+			Logger:           nil,
+		})
+		err := NFSManager.Start()
+		if err != nil {
+			fmt.Println(err.Error())
+		}
+	*/
 
 	//Register Endpoints
 	//WebDAV