Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(macOS): add support for resolving macOS aliases #205

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ide
.vscode/
.history/
.idea/
.fleet/

Expand Down
5 changes: 3 additions & 2 deletions internal/cli/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/index"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
"github.com/Equationzhao/g/internal/shell"
"github.com/Equationzhao/g/internal/sorter"
Expand Down Expand Up @@ -893,8 +894,8 @@ var logic = func(context *cli.Context) error {
// dereference
if dereference {
for i := range infos {
if util.IsSymLink(infos[i]) {
symlinks, err := filepath.EvalSymlinks(infos[i].FullPath)
if util.IsSymLink(infos[i]) || osbased.IsMacOSAlias(infos[i].FullPath) {
symlinks, err := util.Evallinks(infos[i].FullPath)
if err != nil {
continue
}
Expand Down
7 changes: 4 additions & 3 deletions internal/content/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"unicode"

"github.com/Equationzhao/g/internal/display"
"github.com/Equationzhao/g/internal/osbased"

"github.com/Equationzhao/g/internal/global"
"github.com/shirou/gopsutil/v3/disk"
Expand Down Expand Up @@ -232,7 +233,7 @@ func (n *Name) Enable(renderer *render.Renderer) ContentOption {
classify = "/"
}
color, underline, bold, italics, faint, blink = style.Color, style.Underline, style.Bold, style.Italics, style.Faint, style.Blink
} else if util.IsSymLinkMode(mode) {
} else if util.IsSymLinkMode(mode) || osbased.IsMacOSAlias(info.FullPath) {
if n.statistics != nil {
n.statistics.link.Add(1)
}
Expand All @@ -247,7 +248,7 @@ func (n *Name) Enable(renderer *render.Renderer) ContentOption {
// color + arrow + color-end + color + path + color-end
if !n.noDeference {
if n.json { // "dereference": "symlinks"
symlinks, err := filepath.EvalSymlinks(info.FullPath)
symlinks, err := util.Evallinks(info.FullPath)
if err != nil {
info.Meta.Set("dereference_err", &display.ItemContent{Content: display.StringContent(err.Error())})
symlinks = n.checkDereferenceErr(err)
Expand All @@ -259,7 +260,7 @@ func (n *Name) Enable(renderer *render.Renderer) ContentOption {
checkNameDisplayEffect(arrowStyle, dereference)
_, _ = dereference.WriteString(arrowStyle.Icon)
_, _ = dereference.WriteString(renderer.Colorend())
symlinks, err := filepath.EvalSymlinks(info.FullPath)
symlinks, err := util.Evallinks(info.FullPath)
var linkStyle theme.Style
dereferenceMounts := ""
if err != nil {
Expand Down
87 changes: 87 additions & 0 deletions internal/osbased/filedetail_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,62 @@

package osbased

/*
#cgo CFLAGS: -mmacosx-version-min=10.9
#cgo LDFLAGS: -framework CoreFoundation -framework CoreServices

#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/xattr.h>

char *resolveAlias(const char *path) {
CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)path, strlen(path), false);
if (!url) {
return NULL;
}

CFErrorRef error = NULL;
CFDataRef bookmarkData = CFURLCreateBookmarkDataFromFile(NULL, url, &error);
CFRelease(url);
if (!bookmarkData) {
if (error != NULL) {
CFRelease(error);
}
return NULL;
}

Boolean bookmarkIsStale;
CFURLRef resolvedURL = CFURLCreateByResolvingBookmarkData(NULL, bookmarkData, kCFBookmarkResolutionWithoutUIMask, NULL, NULL, &bookmarkIsStale, &error);
CFRelease(bookmarkData);
if (!resolvedURL) {
if (error != NULL) {
CFRelease(error);
}
return NULL;
}

UInt8 buffer[PATH_MAX];
Boolean success = CFURLGetFileSystemRepresentation(resolvedURL, true, buffer, PATH_MAX);
CFRelease(resolvedURL);
if (!success) {
return NULL;
}

return strdup((const char*)buffer);
}
*/
import "C"
import (
"fmt"
"os"
"strconv"
"syscall"

"unsafe"
)

func Inode(info os.FileInfo) string {
Expand All @@ -32,3 +84,38 @@ func BlockSize(info os.FileInfo) int64 {

return stat.Blocks
}

func IsMacOSAlias(fullPath string) bool {
path := C.CString(fullPath)
defer C.free(unsafe.Pointer(path))

name := C.CString("com.apple.FinderInfo")
defer C.free(unsafe.Pointer(name))

buf := make([]byte, 32)
size := C.size_t(len(buf))

ret, _ := C.getxattr(
path,
name,
unsafe.Pointer(&buf[0]),
size,
0,
0,
)

return ret > 0 && (buf[0]&0x20) != 0
}

func ResolveAlias(fullPath string) (string, error) {
cPath := C.CString(fullPath)
defer C.free(unsafe.Pointer(cPath))

resolved := C.resolveAlias(cPath)
if resolved == nil {
return "", fmt.Errorf("failed to resolve macOS alias for %s", fullPath)
}
defer C.free(unsafe.Pointer(resolved))

return C.GoString(resolved), nil
}
10 changes: 10 additions & 0 deletions internal/osbased/filedetail_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ func BlockSize(info os.FileInfo) int64 {

return stat.Blocks
}

// always false on Linux
func IsMacOSAlias(_ string) bool {
return false
}

// ResolveAlias is a no-op on Linux.
func ResolveAlias(_ string) (string, error) {
return "", nil
}
10 changes: 10 additions & 0 deletions internal/osbased/filedetail_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ func LinkCount(info *item.FileInfo) uint64 {
func BlockSize(info os.FileInfo) int64 {
return 0
}

// always false on Windows
func IsMacOSAlias(_ string) bool {
return false
}

// ResolveAlias is a no-op on Windows.
func ResolveAlias(_ string) (string, error) {
return "", nil
}
20 changes: 20 additions & 0 deletions internal/util/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"fmt"
"io/fs"
"math/rand/v2"
"os"
Expand All @@ -9,9 +10,28 @@

"github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/spf13/afero"
)

func Evallinks(fullPath string) (string, error) {
// support symlinks and macOS Alias
if osbased.IsMacOSAlias(fullPath) {

Check failure on line 19 in internal/util/file.go

View workflow job for this annotation

GitHub Actions / build

undefined: osbased.IsMacOSAlias
for {
if osbased.IsMacOSAlias(fullPath) {

Check failure on line 21 in internal/util/file.go

View workflow job for this annotation

GitHub Actions / build

undefined: osbased.IsMacOSAlias
aliasTarget, err := osbased.ResolveAlias(fullPath)

Check failure on line 22 in internal/util/file.go

View workflow job for this annotation

GitHub Actions / build

undefined: osbased.ResolveAlias
if err != nil {
return "", fmt.Errorf("alias resolution failed: %w", err)
}
fullPath = aliasTarget
} else {
break
}
}
}
return filepath.EvalSymlinks(fullPath)
}

func IsSymLink(file os.FileInfo) bool {
return file.Mode()&os.ModeSymlink != 0
}
Expand Down
Loading