posix: Deprecate custom removeAll/mkdirAll implementations. (#4808)
Since go1.8 os.RemoveAll and os.MkdirAll both support long path names i.e UNC path on windows. The code we are carrying was directly borrowed from `pkg/os` package and doesn't need to be in our repo anymore. As a side affect this also addresses our codecoverage issue. Refer #4658master
parent
b69aa9c4d0
commit
d864e00e24
@ -1,56 +0,0 @@ |
||||
/* |
||||
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package cmd |
||||
|
||||
import ( |
||||
"path/filepath" |
||||
"runtime" |
||||
"strings" |
||||
) |
||||
|
||||
// preparePath rewrites path to handle any OS specific details.
|
||||
func preparePath(path string) string { |
||||
if runtime.GOOS == globalWindowsOSName { |
||||
// Microsoft Windows supports long path names using
|
||||
// uniform naming convention (UNC).
|
||||
return UNCPath(path) |
||||
} |
||||
return path |
||||
} |
||||
|
||||
// UNCPath converts a absolute windows path to a UNC long path.
|
||||
func UNCPath(path string) string { |
||||
// Clean the path for any trailing "/".
|
||||
path = filepath.Clean(path) |
||||
|
||||
// UNC can NOT use "/", so convert all to "\".
|
||||
path = filepath.FromSlash(path) |
||||
|
||||
// If prefix is "\\", we already have a UNC path or server.
|
||||
if strings.HasPrefix(path, `\\`) { |
||||
|
||||
// If already long path, just keep it
|
||||
if strings.HasPrefix(path, `\\?\`) { |
||||
return path |
||||
} |
||||
|
||||
// Trim "\\" from path and add UNC prefix.
|
||||
return `\\?\UNC\` + strings.TrimPrefix(path, `\\`) |
||||
} |
||||
path = `\\?\` + path |
||||
return path |
||||
} |
@ -0,0 +1,59 @@ |
||||
// +build windows
|
||||
|
||||
/* |
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package lock |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
) |
||||
|
||||
func TestFixLongPath(t *testing.T) { |
||||
// 248 is long enough to trigger the longer-than-248 checks in
|
||||
// fixLongPath, but short enough not to make a path component
|
||||
// longer than 255, which is illegal on Windows. (which
|
||||
// doesn't really matter anyway, since this is purely a string
|
||||
// function we're testing, and it's not actually being used to
|
||||
// do a system call)
|
||||
veryLong := "l" + strings.Repeat("o", 248) + "ng" |
||||
for _, test := range []struct{ in, want string }{ |
||||
// Short; unchanged:
|
||||
{`C:\short.txt`, `C:\short.txt`}, |
||||
{`C:\`, `C:\`}, |
||||
{`C:`, `C:`}, |
||||
// The "long" substring is replaced by a looooooong
|
||||
// string which triggers the rewriting. Except in the
|
||||
// cases below where it doesn't.
|
||||
{`C:\long\foo.txt`, `\\?\C:\long\foo.txt`}, |
||||
{`C:/long/foo.txt`, `\\?\C:\long\foo.txt`}, |
||||
{`C:\long\foo\\bar\.\baz\\`, `\\?\C:\long\foo\bar\baz`}, |
||||
{`\\unc\path`, `\\unc\path`}, |
||||
{`long.txt`, `long.txt`}, |
||||
{`C:long.txt`, `C:long.txt`}, |
||||
{`c:\long\..\bar\baz`, `c:\long\..\bar\baz`}, |
||||
{`\\?\c:\long\foo.txt`, `\\?\c:\long\foo.txt`}, |
||||
{`\\?\c:\long/foo.txt`, `\\?\c:\long/foo.txt`}, |
||||
} { |
||||
in := strings.Replace(test.in, "long", veryLong, -1) |
||||
want := strings.Replace(test.want, "long", veryLong, -1) |
||||
if got := fixLongPath(in); got != want { |
||||
got = strings.Replace(got, veryLong, "long", -1) |
||||
t.Errorf("fixLongPath(%q) = %q; want %q", test.in, got, test.want) |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue