MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / build_immutable_uri

Function build_immutable_uri

src/store/store.c:785–835  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

783 * for drive-letter paths so the drive is not parsed as a URI authority.
784 * Returns false if the output buffer is too small. */
785static bool build_immutable_uri(const char *path, char *out, size_t out_sz) {
786 static const char PREFIX[] = "file://";
787 static const char SUFFIX[] = "?immutable=1";
788 static const char HEX[] = "0123456789ABCDEF";
789 size_t prefix_len = sizeof(PREFIX) - 1;
790 size_t suffix_len = sizeof(SUFFIX) - 1;
791 if (prefix_len + 1 > out_sz) {
792 return false;
793 }
794 memcpy(out, PREFIX, prefix_len);
795 size_t pos = prefix_len;
796
797 /* Ensure the path component begins with '/' (POSIX absolute paths already
798 * do; Windows "C:\..." gets a leading '/' -> "/C:/..."). */
799 if (path[0] != '/') {
800 if (pos + 1 >= out_sz) {
801 return false;
802 }
803 out[pos++] = '/';
804 }
805
806 for (const unsigned char *p = (const unsigned char *)path; *p != '\0'; p++) {
807 unsigned char c = *p;
808 if (c == '\\') {
809 c = '/'; /* normalize Windows separators */
810 }
811 bool safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
812 c == '/' || c == '.' || c == '-' || c == '_' || c == '~' || c == ':';
813 if (safe) {
814 if (pos + 1 >= out_sz) {
815 return false;
816 }
817 out[pos++] = (char)c;
818 } else {
819 if (pos + 3 >= out_sz) {
820 return false;
821 }
822 out[pos++] = '%';
823 out[pos++] = HEX[(c >> 4) & 0xF];
824 out[pos++] = HEX[c & 0xF];
825 }
826 }
827
828 if (pos + suffix_len + 1 > out_sz) {
829 return false;
830 }
831 memcpy(out + pos, SUFFIX, suffix_len);
832 pos += suffix_len;
833 out[pos] = '\0';
834 return true;
835}
836
837cbm_store_t *cbm_store_open_path_query(const char *db_path) {
838 if (!db_path) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected