Skip to content

Commit

Permalink
Android: Use mode a+rwx for directories and a+rw for files in externa…
Browse files Browse the repository at this point in the history
…l storage path.

Issue love2d/love-android#269.
  • Loading branch information
MikuAuahDark committed Apr 27, 2024
1 parent 0089749 commit 629fc1c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
26 changes: 20 additions & 6 deletions src/common/android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,24 @@ bool directoryExists(const char *path)

bool mkdir(const char *path)
{
int err = ::mkdir(path, 0770);
int err = ::mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
if (err == -1)
{
SDL_Log("Error: Could not create directory %s", path);
const char *error = strerror(errno);
SDL_Log("Error: Could not create directory '%s': %s", path, error);
return false;
}

return true;
}

bool chmod(const char *path, int mode)
{
int err = ::chmod(path, mode);
if (err == -1)
{
const char *error = strerror(errno);
SDL_Log("Error: Could not change mode '%s': %s", path, error);
return false;
}

Expand Down Expand Up @@ -216,11 +230,11 @@ bool createStorageDirectories()
return true;
}

void fixupPermissionSingleFile(const std::string &savedir, const std::string &path)
void fixupPermissionSingleFile(const std::string &savedir, const std::string &path, int mode)
{
std::string fixedSavedir = savedir.back() == '/' ? savedir : (savedir + "/");
std::string target = fixedSavedir + path;
::chmod(target.c_str(), 0660);
::chmod(target.c_str(), mode);
}

void fixupExternalStoragePermission(const std::string &savedir, const std::string &path)
Expand All @@ -242,15 +256,15 @@ void fixupExternalStoragePermission(const std::string &savedir, const std::strin
}

std::string fixedSavedir = savedir.back() == '/' ? savedir : (savedir + "/");
::chmod(savedir.c_str(), 0770);
chmod(savedir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);

for (const std::string &dir: pathsToFix)
{
const char *realPath = PHYSFS_getRealDir(dir.c_str());
if (!dir.empty() && strcmp(realPath, savedir.c_str()) == 0)
{
std::string target = fixedSavedir + dir;
::chmod(target.c_str(), 0770);
chmod(target.c_str(), S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/android.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool mkdir(const char *path);

bool createStorageDirectories();

void fixupPermissionSingleFile(const std::string &savedir, const std::string &path);
void fixupPermissionSingleFile(const std::string &savedir, const std::string &path, int mode = 0666);

void fixupExternalStoragePermission(const std::string &savedir, const std::string &path);

Expand Down
16 changes: 15 additions & 1 deletion src/modules/filesystem/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,21 @@ static bool createDirectoryRaw(const std::string &path)
std::wstring wpath = to_widestr(path);
return CreateDirectoryW(wpath.c_str(), nullptr) != 0;
#else
return mkdir(path.c_str(), S_IRWXU) == 0;
int mode = S_IRWXU;

#ifdef LOVE_ANDROID
// Need to create save directory with ugo+rwx and setgid bit if
// t.externalstorage is set and it's for save directory.
auto fs = Module::getInstance<Filesystem>(Module::M_FILESYSTEM);
if (fs != nullptr && fs->isAndroidSaveExternal())
{
const std::string &savedir = fs->getFullCommonPath(Filesystem::COMMONPATH_APP_SAVEDIR);
if (path.rfind(savedir, 0) == 0)
mode |= S_IRWXG | S_IRWXO | S_ISGID;
}
#endif

return mkdir(path.c_str(), mode) == 0;
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/filesystem/physfs/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ File::File(const std::string &filename, Mode mode)

#ifdef LOVE_ANDROID
// In Android with t.externalstorage = true, make sure the file opened or
// created in the save directory has permissions of ug+rw (0660) so that
// created in the save directory has permissions of ugo+rw (0666) so that
// it's accessible through MTP.
auto fs = Module::getInstance<love::filesystem::Filesystem>(Module::M_FILESYSTEM);
if (fs != nullptr && fs->isAndroidSaveExternal())
Expand Down
2 changes: 1 addition & 1 deletion src/modules/filesystem/physfs/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ bool Filesystem::createDirectory(const char *dir)

#ifdef LOVE_ANDROID
// In Android with t.externalstorage = true, make sure the directory
// created in the save directory has permissions of ug+rwx (0770) so that
// created in the save directory has permissions of ugo+rwx (0777) so that
// it's accessible through MTP.
if (isAndroidSaveExternal())
love::android::fixupExternalStoragePermission(
Expand Down

0 comments on commit 629fc1c

Please sign in to comment.