Commit c67f523d authored by AlexStocks's avatar AlexStocks

Add: filepath

parent 69652f5e
......@@ -63,7 +63,7 @@ func (d *Page) HasData() bool {
return d.GetDataSize() > 0
}
// New will create an instance
// NewPage will create an instance
func NewPage(requestOffset int, pageSize int,
data []interface{}, totalSize int) *Page {
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 gxfilepath
import (
"fmt"
"os"
)
// Exists returns whether the given file or directory exists
func Exists(path string) (bool, error) {
_, err := os.Lstat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// FileExists checks whether a file exists in the given path. It also fails if
// the path points to a directory or there is an error when trying to check the file.
func FileExists(path string) (bool, error) {
info, err := os.Lstat(path)
if err != nil {
return false, err
}
if info.IsDir() {
return false, fmt.Errorf("%q is a directory", path)
}
return true, nil
}
// DirExists checks whether a directory exists in the given path. It also fails
// if the path is a file rather a directory or there is an error checking whether it exists.
func DirExists(path string) (bool, error) {
info, err := os.Lstat(path)
if err != nil {
return false, err
}
if !info.IsDir() {
return false, fmt.Errorf("%q is a file", path)
}
return true, nil
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 gxfilepath
import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestFileExists(t *testing.T) {
file := "./path_test.go"
ok, err := Exists(file)
assert.True(t, ok)
assert.Nil(t, err)
ok, err = DirExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
ok, err = FileExists(file)
assert.True(t, ok)
assert.Nil(t, err)
file = "./path_test1.go"
ok, err = Exists(file)
assert.False(t, ok)
assert.Nil(t, err)
ok, err = DirExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
ok, err = FileExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
}
func TestDirExists(t *testing.T) {
file := "./"
ok, err := Exists(file)
assert.True(t, ok)
assert.Nil(t, err)
ok, err = DirExists(file)
assert.True(t, ok)
assert.Nil(t, err)
ok, err = FileExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
file = "./go"
ok, err = Exists(file)
assert.False(t, ok)
assert.Nil(t, err)
ok, err = DirExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
ok, err = FileExists(file)
assert.False(t, ok)
assert.NotNil(t, err)
}
......@@ -32,6 +32,10 @@ import (
"github.com/shirou/gopsutil/process"
)
import (
"github.com/dubbogo/gost/path/filepath"
)
var (
CurrentPID = os.Getpid()
)
......@@ -55,20 +59,8 @@ func GetMemoryStat() (total, used, free uint64, usedPercent float64) {
return stat.Total, stat.Used, stat.Free, stat.UsedPercent
}
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func IsCgroup() bool {
ok, _ := exists(cgroupMemLimitPath)
ok, _ := gxfilepath.Exists(cgroupMemLimitPath)
if ok {
return true
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment