<commit_before>package share

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/jcelliott/lumber"

	"github.com/nanobox-io/nanobox/commands/server"
	"github.com/nanobox-io/nanobox/models"
	"github.com/nanobox-io/nanobox/util"
)

type Request struct {
	Path    string
	UID     int
	GID     int
	MountIP string
}

// EXPORTSFILE ...
var EXPORTSFILE = "/etc/exports"

func Exists(path string) bool {
	// read exports file
	existingFile, err := ioutil.ReadFile(EXPORTSFILE)
	if err != nil {
		// if i cant read the etc exports it doesnt exist
		return false
	}

	// get the provider because i need the mount ip
	provider, err := models.LoadProvider()
	if err != nil {
		return false
	}

	lineCheck := fmt.Sprintf("%s -alldirs -mapall=%v:%v", provider.MountIP, uid(), gid())

	lines := strings.Split(string(existingFile), "\n")

	for _, line := range lines {
		// get existing line
		if strings.Contains(line, lineCheck) {
			return strings.Contains(line, path+" ") || strings.Contains(line, path+"\" ")
		}
	}
	return false
}

func Add(path string) error {

	// get the provider because i need the mount ip
	provider, err := models.LoadProvider()
	if err != nil {
		return err
	}

	// create a rpc request
	req := Request{
		Path:    path,
		UID:     uid(),
		GID:     gid(),
		MountIP: provider.MountIP,
	}

	resp := &Response{}

	// in testing we will call the rpc function directly
	if flag.Lookup("test.v") != nil {
		shareRPC := &ShareRPC{}
		err := shareRPC.Add(req, resp)
		if err != nil || !resp.Success {
			err = fmt.Errorf("failed to add share %v %v", err, resp.Message)
		}
		return err
	}

	// have the server run the share command
	err = server.ClientRun("ShareRPC.Add", req, resp)
	if err != nil || !resp.Success {
		err = fmt.Errorf("failed to add share %v %v", err, resp.Message)
	}
	return err
}

// the rpc function run from the server
func (sh *ShareRPC) Add(req Request, resp *Response) error {

	// read exports file
	existingFile, err := ioutil.ReadFile(EXPORTSFILE)
	if err != nil {
		// if the file didnt exist lets create an empty existingFile
		existingFile = []byte("")
	}

	lineCheck := fmt.Sprintf("%s -alldirs -mapall=%v:%v", req.MountIP, req.UID, req.GID)

	lines := strings.Split(string(existingFile), "\n")

	found := false
	for i, line := range lines {
		// get existing line
		if strings.Contains(line, lineCheck) {
			// add our path to the line
			lines[i] = fmt.Sprintf("\"%s\" %s", req.Path, line)
			lines[i] = cleanLine(lines[i], lineCheck)
			found = true
			break
		}
	}
	if !found {
		lines = append(lines, fmt.Sprintf("%s %s", req.Path, lineCheck))
	}
<commit_msg>fix an issue on darwin where duplicate shares were created then removed<commit_after>package share

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/jcelliott/lumber"

	"github.com/nanobox-io/nanobox/commands/server"
	"github.com/nanobox-io/nanobox/models"
	"github.com/nanobox-io/nanobox/util"
)

type Request struct {
	Path    string
	UID     int
	GID     int
	MountIP string
}

// EXPORTSFILE ...
var EXPORTSFILE = "/etc/exports"

func Exists(path string) bool {
	// read exports file
	existingFile, err := ioutil.ReadFile(EXPORTSFILE)
	if err != nil {
		// if i cant read the etc exports it doesnt exist
		return false
	}

	// get the provider because i need the mount ip
	provider, err := models.LoadProvider()
	if err != nil {
		return false
	}

	lineCheck := fmt.Sprintf("%s -alldirs -mapall=%v:%v", provider.MountIP, uid(), gid())

	lines := strings.Split(string(existingFile), "\n")

	for _, line := range lines {
		// get existing line
		if strings.Contains(line, lineCheck) {
			return strings.Contains(line, path+" ") || strings.Contains(line, path+"\" ")
		}
	}
	return false
}

func Add(path string) error {

	// get the provider because i need the mount ip
	provider, err := models.LoadProvider()
	if err != nil {
		return err
	}

	// create a rpc request
	req := Request{
		Path:    path,
		UID:     uid(),
		GID:     gid(),
		MountIP: provider.MountIP,
	}

	resp := &Response{}

	// in testing we will call the rpc function directly
	if flag.Lookup("test.v") != nil {
		shareRPC := &ShareRPC{}
		err := shareRPC.Add(req, resp)
		if err != nil || !resp.Success {
			err = fmt.Errorf("failed to add share %v %v", err, resp.Message)
		}
		return err
	}

	// have the server run the share command
	err = server.ClientRun("ShareRPC.Add", req, resp)
	if err != nil || !resp.Success {
		err = fmt.Errorf("failed to add share %v %v", err, resp.Message)
	}
	return err
}

// the rpc function run from the server
func (sh *ShareRPC) Add(req Request, resp *Response) error {
	fmt.Printf("req: %#v\n", req)

	// read exports file
	existingFile, err := ioutil.ReadFile(EXPORTSFILE)
	if err != nil {
		// if the file didnt exist lets create an empty existingFile
		existingFile = []byte("")
	}

	lineCheck := fmt.Sprintf("%s -alldirs -mapall=%v:%v", req.MountIP, req.UID, req.GID)

	lines := strings.Split(string(existingFile), "\n")

	found := false
	for i, line := range lines {
		// get existing line
		if strings.Contains(line, lineCheck) {
			// add our path to the line
			// check to see if this path has already been added
			if !(strings.Contains(line, req.Path+" ") || strings.Contains(line, req.Path+"\" ")) {
				lines[i] = fmt.Sprintf("\"%s\" %s", req.Path, line)
			}
			

			lines[i] = cleanLine(lines[i], lineCheck)
			found = true
			break
		}
	}
	if !found {
		lines = append(lines, fmt.Sprintf("\"%s\" %s", req.Path, lineCheck))
	}

{'fraction_non_alphanumeric': 0.14760614272809394, 'fraction_numerical': 0.0, 'mean_word_length': 3.2291825821237587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540580', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1930, 'n_words': 583}