Build a SQLite "file:" URI with immutable=1 from a filesystem path. * immutable=1 bypasses WAL and locking and reads the main DB file directly — * used only as a fallback for read-only filesystems where the wal-index * (-shm) cannot be created. URI-special characters are percent-encoded. * Windows backslashes are normalized to '/', and a leading '/' is inserted * for drive-letter paths so the
| 1056 | * for drive-letter paths so the drive is not parsed as a URI authority. |
| 1057 | * Returns false if the output buffer is too small. */ |
| 1058 | static bool build_immutable_uri(const char *path, char *out, size_t out_sz) { |
| 1059 | static const char PREFIX[] = "file://"; |
| 1060 | static const char SUFFIX[] = "?immutable=1"; |
| 1061 | static const char HEX[] = "0123456789ABCDEF"; |
| 1062 | size_t prefix_len = sizeof(PREFIX) - 1; |
| 1063 | size_t suffix_len = sizeof(SUFFIX) - 1; |
| 1064 | if (prefix_len + 1 > out_sz) { |
| 1065 | return false; |
| 1066 | } |
| 1067 | memcpy(out, PREFIX, prefix_len); |
| 1068 | size_t pos = prefix_len; |
| 1069 | |
| 1070 | /* Ensure the path component begins with '/' (POSIX absolute paths already |
| 1071 | * do; Windows "C:\..." gets a leading '/' -> "/C:/..."). */ |
| 1072 | if (path[0] != '/') { |
| 1073 | if (pos + 1 >= out_sz) { |
| 1074 | return false; |
| 1075 | } |
| 1076 | out[pos++] = '/'; |
| 1077 | } |
| 1078 | |
| 1079 | for (const unsigned char *p = (const unsigned char *)path; *p != '\0'; p++) { |
| 1080 | unsigned char c = *p; |
| 1081 | if (c == '\\') { |
| 1082 | c = '/'; /* normalize Windows separators */ |
| 1083 | } |
| 1084 | bool safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || |
| 1085 | c == '/' || c == '.' || c == '-' || c == '_' || c == '~' || c == ':'; |
| 1086 | if (safe) { |
| 1087 | if (pos + 1 >= out_sz) { |
| 1088 | return false; |
| 1089 | } |
| 1090 | out[pos++] = (char)c; |
| 1091 | } else { |
| 1092 | if (pos + 3 >= out_sz) { |
| 1093 | return false; |
| 1094 | } |
| 1095 | out[pos++] = '%'; |
| 1096 | out[pos++] = HEX[(c >> 4) & 0xF]; |
| 1097 | out[pos++] = HEX[c & 0xF]; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | if (pos + suffix_len + 1 > out_sz) { |
| 1102 | return false; |
| 1103 | } |
| 1104 | memcpy(out + pos, SUFFIX, suffix_len); |
| 1105 | pos += suffix_len; |
| 1106 | out[pos] = '\0'; |
| 1107 | return true; |
| 1108 | } |
| 1109 | |
| 1110 | /* Query tools open the project DB READ-ONLY: a read query must never |
| 1111 | * mutate the DB (the previous READWRITE open + WAL write-pragmas did), |
no outgoing calls
no test coverage detected