Skip to content

Commit

Permalink
SQLiteDatabase should try to set RunningBoard file attribute on all d…
Browse files Browse the repository at this point in the history
…atabase files, not just the main one

https://bugs.webkit.org/show_bug.cgi?id=259117
rdar://97212284

Reviewed by Ben Nham.

SQLiteDatabase should try to set RunningBoard file attribute on all database
files, not just the main one.

This is a follow-up to 265951@main. This is not perfect this we don't control
when the `-shm` file will be created for example. However, this is a best
effort until we have something better (like an API from SQLite).

* Source/WebCore/platform/sql/SQLiteDatabase.cpp:
(WebCore::SQLiteDatabase::open):
* Source/WebCore/platform/sql/SQLiteFileSystem.cpp:
(WebCore::SQLiteFileSystem::setCanSuspendLockedFileAttribute):
* Source/WebCore/platform/sql/SQLiteFileSystem.h:

Canonical link: https://commits.webkit.org/265962@main
  • Loading branch information
cdumez committed Jul 11, 2023
1 parent e57a429 commit b182e96
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
15 changes: 3 additions & 12 deletions Source/WebCore/platform/sql/SQLiteDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@
#define ENABLE_SQLITE_FAST_MALLOC (BENABLE(MALLOC_SIZE) && BENABLE(MALLOC_GOOD_SIZE))
#endif

#if PLATFORM(COCOA)
#include <sys/xattr.h>
#endif

namespace WebCore {

static const char notOpenErrorMessage[] = "database is not open";
Expand Down Expand Up @@ -159,15 +155,10 @@ bool SQLiteDatabase::open(const String& filename, OpenMode openMode, OptionSet<O
int result = SQLITE_OK;
{
SQLiteTransactionInProgressAutoCounter transactionCounter;
auto fsFilename = FileSystem::fileSystemRepresentation(filename);
result = sqlite3_open_v2(fsFilename.data(), &m_db, flags, nullptr);
result = sqlite3_open_v2(FileSystem::fileSystemRepresentation(filename).data(), &m_db, flags, nullptr);
#if PLATFORM(COCOA)
if (result == SQLITE_OK && options.contains(OpenOptions::CanSuspendWhileLocked)) {
char excluded = 0xff;
auto err = setxattr(fsFilename.data(), "com.apple.runningboard.can-suspend-locked", &excluded, sizeof(excluded), 0, 0);
if (err < 0)
RELEASE_LOG_ERROR(SQLDatabase, "SQLiteDatabase::open: setxattr failed: %" PUBLIC_LOG_STRING, strerror(errno));
}
if (result == SQLITE_OK && options.contains(OpenOptions::CanSuspendWhileLocked))
SQLiteFileSystem::setCanSuspendLockedFileAttribute(filename);
#else
UNUSED_PARAM(options);
#endif
Expand Down
17 changes: 17 additions & 0 deletions Source/WebCore/platform/sql/SQLiteFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include <pal/spi/ios/SQLite3SPI.h>
#endif

#if PLATFORM(COCOA)
#include <sys/xattr.h>
#endif

namespace WebCore {

static constexpr std::array<const char *, 3> databaseFileSuffixes { "", "-shm", "-wal" };
Expand Down Expand Up @@ -91,6 +95,19 @@ bool SQLiteFileSystem::deleteDatabaseFile(const String& filePath)
return !fileExists;
}

#if PLATFORM(COCOA)
void SQLiteFileSystem::setCanSuspendLockedFileAttribute(const String& filePath)
{
for (const auto* suffix : databaseFileSuffixes) {
String path = filePath + suffix;
char excluded = 0xff;
auto result = setxattr(FileSystem::fileSystemRepresentation(path).data(), "com.apple.runningboard.can-suspend-locked", &excluded, sizeof(excluded), 0, 0);
if (result < 0 && !strcmp(suffix, ""))
RELEASE_LOG_ERROR(SQLDatabase, "SQLiteFileSystem::setCanSuspendLockedFileAttribute: setxattr failed: %" PUBLIC_LOG_STRING, strerror(errno));
}
}
#endif

bool SQLiteFileSystem::moveDatabaseFile(const String& oldFilePath, const String& newFilePath)
{
bool allMoved = true;
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/platform/sql/SQLiteFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class SQLiteFileSystem {
// fileName - The file name.
WEBCORE_EXPORT static bool deleteDatabaseFile(const String& filePath);

#if PLATFORM(COCOA)
static void setCanSuspendLockedFileAttribute(const String& filePath);
#endif

// Moves a database file to a new place.
WEBCORE_EXPORT static bool moveDatabaseFile(const String& oldFilePath, const String& newFilePath);
WEBCORE_EXPORT static String computeHashForFileName(StringView filePath);
Expand Down

0 comments on commit b182e96

Please sign in to comment.