Skip to content

Commit 9f36adf

Browse files
authored
Merge pull request #111 from ahmet2mir/cifs-share-dir
Initial support for CIFS only to create directories within root share
2 parents 2e5c6b0 + e3ac3f7 commit 9f36adf

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

netshare/drivers/cifs.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,29 @@ func (c cifsDriver) Mount(r volume.MountRequest) volume.Response {
6767
c.m.Lock()
6868
defer c.m.Unlock()
6969
hostdir := mountpoint(c.root, r.Name)
70-
source := c.fixSource(r.Name, r.ID)
70+
source := c.fixSource(r.Name)
7171
host := c.parseHost(r.Name)
7272

73+
resolvedName, resOpts := resolveName(r.Name)
74+
7375
log.Infof("Mount: %s, ID: %s", r.Name, r.ID)
7476

77+
// Support adhoc mounts (outside of docker volume create)
78+
// need to adjust source for ShareOpt
79+
if resOpts != nil {
80+
if share, found := resOpts[ShareOpt]; found {
81+
source = c.fixSource(share)
82+
}
83+
}
84+
7585
if c.mountm.HasMount(r.Name) && c.mountm.Count(r.Name) > 0 {
7686
log.Infof("Using existing CIFS volume mount: %s", hostdir)
7787
c.mountm.Increment(r.Name)
78-
return volume.Response{Mountpoint: hostdir}
88+
if err := run(fmt.Sprintf("mountpoint -q %s", hostdir)); err != nil {
89+
log.Infof("Existing CIFS volume not mounted, force remount.")
90+
} else {
91+
return volume.Response{Mountpoint: hostdir}
92+
}
7993
}
8094

8195
log.Infof("Mounting CIFS volume %s on %s", source, hostdir)
@@ -88,14 +102,23 @@ func (c cifsDriver) Mount(r volume.MountRequest) volume.Response {
88102
return volume.Response{Err: err.Error()}
89103
}
90104
c.mountm.Add(r.Name, hostdir)
105+
106+
if c.mountm.GetOption(resolvedName, ShareOpt) != "" && c.mountm.GetOptionAsBool(resolvedName, CreateOpt) {
107+
log.Infof("Mount: Share and Create options enabled - using %s as sub-dir mount", resolvedName)
108+
datavol := filepath.Join(hostdir, resolvedName)
109+
if err := createDest(filepath.Join(hostdir, resolvedName)); err != nil {
110+
return volume.Response{Err: err.Error()}
111+
}
112+
hostdir = datavol
113+
}
91114
return volume.Response{Mountpoint: hostdir}
92115
}
93116

94117
func (c cifsDriver) Unmount(r volume.UnmountRequest) volume.Response {
95118
c.m.Lock()
96119
defer c.m.Unlock()
97120
hostdir := mountpoint(c.root, r.Name)
98-
source := c.fixSource(r.Name, r.ID)
121+
source := c.fixSource(r.Name)
99122

100123
if c.mountm.HasMount(r.Name) {
101124
if c.mountm.Count(r.Name) > 1 {
@@ -121,7 +144,7 @@ func (c cifsDriver) Unmount(r volume.UnmountRequest) volume.Response {
121144
return volume.Response{}
122145
}
123146

124-
func (c cifsDriver) fixSource(name, id string) string {
147+
func (c cifsDriver) fixSource(name string) string {
125148
if c.mountm.HasOption(name, ShareOpt) {
126149
return "//" + c.mountm.GetOption(name, ShareOpt)
127150
}

0 commit comments

Comments
 (0)