Coder Social home page Coder Social logo

Comments (3)

pzhlkj6612 avatar pzhlkj6612 commented on July 22, 2024

Gatekeeper on macOS

阅读 potionfactory/LetsMove#56sevengo8378/blog#1


macOS 的 Gatekeeper 安全机制会对不信任的软件包( Bundle ,在这里指 .app 文件夹)进行一些处理。来自于不信任来源的 .dmg 镜像文件中的软件包,或者从来自于不信任来源的 .zip 压缩文件中提取的软件包会被认为是不被信任的。你可以用ls -l@命令查看这样的 .dmg 镜像文件、 .zip 压缩文件以及 .app 软件包的拓展文件属性( Extended file attributes ):

% ls -l@ /.../DMG_NAME.dmg /.../ZIP_NAME.zip /.../APPNAME.app
-rw-r--r--@ 1 USER_NAME  staff  ... DMG_NAME.dmg
	...
	com.apple.quarantine	      57 
-rw-r--r--@ 1 USER_NAME  staff  ... ZIP_NAME.zip
	...
	com.apple.quarantine	      57 
drwxr-xr-x@ 3 USER_NAME  staff  ... APPNAME.app
	com.apple.quarantine	      57 

当这样的软件包被打开时,会弹出“无法确认开发者的身份”安全警告。在 Finder 中右键-“打开”该软件包,可以在警告弹出后继续运行程序。如果该软件包本身没有被用户在 Finder 中进行移动,在程序被执行之前,整个软件包将会被挂载到一个随机目录中( Path randomization ),接着再运行程序。随机目录的路径形式为/private/var/folders/hs/.../T/AppTranslocation/...,软件包中的程序所在路径为/private/var/folders/hs/.../T/AppTranslocation/.../d/APPNAME.app/Contents/MacOS

% df -ha
Filesystem        ...  Mounted on
...
/.../APPNAME.app  ...  /private/var/folders/hs/.../T/AppTranslocation/...

这个随机目录是只读的(read-only;同时也要注意nullfs):

% mount
...
/Volumes/DMG_NAME/APPNAME.app on /private/var/folders/hs/.../T/AppTranslocation/... (nullfs, local, nodev, nosuid, read-only, noowners, quarantine, nobrowse, mounted by USER_NAME)

这就是 B4X 在 macOS 上报错“无法为配置创建目录”的原因。代码:

QString SettingManager::MakeSureConfigPathAvailable()
{
QString StrDataDir = QCoreApplication::applicationDirPath() + "/data";
//如果settings 目录不存在则创建目录
QDir DataDir(StrDataDir);
if(!DataDir.exists())
{
if(!DataDir.mkpath(StrDataDir))
{
BesMessageBox::information(tr("提示"),tr("无法为配置创建目录")+":" + StrDataDir);
return "";
}
}
//得到目标路径
return StrDataDir + "/setting.xml";
}


不过,只要使用 Finder 进行一次软件包的移动,就可以使软件包不被挂载到随机目录中,进而让程序对所在目录拥有“写”权限。这可能是 Finder 对文件的一些标志( Flag )进行了修改。

使用 Finder 将软件包从 .dmg 镜像文件中提取到任意位置也算作“使用 Finder 进行一次软件包的移动”。

不过,在程序所在目录存储数据仍然是不被推荐的。


* 可以尝试使用 Qt 的 (QDir(QCoreApplication::applicationDirPath())).entryInfoList({"."}).first().permissions() 获得程序所在目录的权限。对于在随机目录运行的软件包中的程序,结果为:

7555:/private/var/folders/hs/.../T/AppTranslocation/.../d/APPNAME.app/Contents/MacOS

对于在正常目录运行的软件包中的程序,结果为:

7755:/.../APPNAME.app/Contents/MacOS

结果中“ 7555 ”和“ 7755 ”的差异是比较奇怪的,因为在 Qt 的文档中有:

Warning: ... the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned...

但是从上文的结果看, OwnerUser 差了“写”权限。

直接用ls -la查看权限:

% ls -la /private/var/folders/hs/.../T/AppTranslocation/.../d/APPNAME.app/Contents/MacOS
drwxr-xr-x@ 3 mac  staff  ... .
drwxr-xr-x@ 8 mac  staff  ... ..
-rwxr-xr-x@ 1 mac  staff  ... APPNAME
...
% ls -la /.../APPNAME.app/Contents/MacOS
drwxr-xr-x@ 3 mac  staff  ... .
drwxr-xr-x@ 8 mac  staff  ... ..
-rwxr-xr-x@ 1 mac  staff  ... APPNAME
...

并没有区别。

from beslyric-for-x.

pzhlkj6612 avatar pzhlkj6612 commented on July 22, 2024

AppImage, SquashFS and FUSE on Ubuntu

linuxdeployqt 进行依赖收集和打包的应用程序,生成的 AppImage 能够以单文件的形式在各个 Linux 发行版运行。现在的 AppImage 是一种使用 SquashFS 文件系统的镜像文件,当它被执行时,其中的内容将会由 FUSE 挂载到/tmp/下的一个随机目录中,接着再运行程序。随机目录的路径形式为/tmp/.mount_APPNAME...,镜像文件中的程序所在路径为/tmp/.mount_APPNAME.../usr/bin

$ df -haT
Filesystem             Type                         ... Mounted on
...
AppImage_NAME.AppImage fuse.AppImage_NAME.AppImage  ... /tmp/.mount_APPNAME...

这个随机目录是只读的(ro):

$ cat /proc/mounts
...
AppImage_NAME.AppImage /tmp/.mount_APPNAME... fuse.AppImage_NAME.AppImage ro,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0

这是 B4X 在 Ubuntu 上报错“无法为配置创建目录”的原因。

当然,在程序所在目录存储数据是不被推荐的。


* 可以尝试使用 Qt 的 (QDir(QCoreApplication::applicationDirPath())).entryInfoList({"."}) 获得程序所在目录的信息,结果为空。查阅文档:

...
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.

ls -la查看权限:

$ ls -la /tmp/.mount_APPNAME...
total ...
lrwxrwxrwx 1 root root ... AppRun -> usr/bin/APPNAME
-rw-rw-r-- 1 root root ... .DirIcon
-rw-rw-r-- 1 root root ... APPNAME.desktop
...
drwxrwxr-x 7 root root ... usr
$ sudo su
# ls -la /tmp/.mount_APPNAME...
ls: cannot access '/tmp/.mount_APPNAME...': Permission denied

这可能是由于 FUSE 本身的限制导致的。

from beslyric-for-x.

BensonLaur avatar BensonLaur commented on July 22, 2024

关于应该在哪里存储应用数据,这一点也一直比较头疼。

windows

针对用户 C# 参考名 cmd 变量名 例子
全局多用户共用 Environment.SpecialFolder.CommonApplicationData %programdata% C:\ProgramData
当前漫游用户 Environment.SpecialFolder.ApplicationData %ApplicationData% C:\Users\UserName\AppData\Roaming
当前非漫游用户 Environement.SpecialFolder.LocalApplicationData %localappdata% C:\Users\UserName\AppData\Local

参考1参考2参考3

windows 这些目录应该比较推荐的, 应该也没有 权限问题,可以考虑 [%AppData% | C:/Users/UserName/AppData/Roaming] /AppData

mac os

似乎可以考虑 [ ~ | ${HOME} | /Users/UserName ] /Library/Application Support/AppData , 不过还是得解决前面提到的 Gatekeeper 问题
参考1参考2

linux

类似 mac os, 放在 [ ~ | $HOME | /home/UserName ] /<APPNAME> ,权限问题还不太清楚


总的来说,可以考虑的各个平台的存储程序数据的路径有:

平台 可参考路径
linux [ ~ | $HOME | /home/ ] /AppData
mac [ ~ | ${HOME} | /Users/UserName ] /Library/Application Support/AppData
windows [%AppData% | C:/Users/UserName/AppData/Roaming ]/AppData

参考1参考2

from beslyric-for-x.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.