Files
Gogs/internal/osutil/osutil.go
Joe Chen 591810e405 web_editor: prohibit CRUD to symbolic files (#7981)
Fixes
[GHSA-wj44-9vcg-wjq7](https://github.com/gogs/gogs/security/advisories/GHSA-wj44-9vcg-wjq7)

---------

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
2025-06-08 18:28:28 -04:00

68 lines
1.3 KiB
Go

// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package osutil
import (
"os"
"os/user"
)
// IsFile returns true if given path exists as a file (i.e. not a directory).
func IsFile(path string) bool {
f, e := os.Stat(path)
if e != nil {
return false
}
return !f.IsDir()
}
// IsDir returns true if given path is a directory, and returns false when it's
// a file or does not exist.
func IsDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}
// IsExist returns true if a file or directory exists.
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// IsSymlink returns true if given path is a symbolic link.
func IsSymlink(path string) bool {
if !IsExist(path) {
return false
}
fileInfo, err := os.Lstat(path)
if err != nil {
return false
}
return fileInfo.Mode()&os.ModeSymlink != 0
}
// CurrentUsername returns the username of the current user.
func CurrentUsername() string {
username := os.Getenv("USER")
if len(username) > 0 {
return username
}
username = os.Getenv("USERNAME")
if len(username) > 0 {
return username
}
if user, err := user.Current(); err == nil {
username = user.Username
}
return username
}