280179 {
280180#ifdef DUCKDB_DISABLE_EXTENSION_LOAD
280181 throw PermissionException("Loading external extensions is disabled through a compile time flag");
280182#else
280183 if (!Settings::Get<EnableExternalAccessSetting>(db)) {
280184 throw PermissionException("Loading external extensions is disabled through configuration");
280185 }
280186 auto filename = fs.ConvertSeparators(extension);
280187
280188 bool direct_load;
280189
280190
280191 if (!ExtensionHelper::IsFullPath(extension)) {
280192 direct_load = false;
280194#ifdef WASM_LOADABLE_EXTENSIONS
280197
280198 char *str = (char *)EM_ASM_PTR(
280199 {
280200 var jsString = ((typeof runtime == 'object') && runtime && (typeof runtime.whereToLoad == 'function') &&
280201 runtime.whereToLoad)
280202 ? runtime.whereToLoad(UTF8ToString($0))
280203 : (UTF8ToString($1));
280204 var lengthBytes = lengthBytesUTF8(jsString) + 1;
280205
280206
280207 var stringOnWasmHeap = _malloc(lengthBytes);
280208 stringToUTF8(jsString, stringOnWasmHeap, lengthBytes);
280209 return stringOnWasmHeap;
280210 },
280211 filename.c_str(), url.c_str());
280212 string address(str);
280213 free(str);
280214
280215 filename = address;
280216#else
280217
280218
280219 auto ComputeLocalExtensionPath = [&fs](const string &base_path, const string &extension_name) -> string {
280220
280221 string local_path = fs.ConvertSeparators(base_path);
280222
280223 local_path = fs.ExpandPath(local_path);
280224 auto path_components = PathComponents();
280225 for (auto &path_ele : path_components) {
280226 local_path = fs.JoinPath(local_path, path_ele);
280227 }
280228 return fs.JoinPath(local_path, extension_name + ".duckdb_extension");
280229 };
280230
280231
280232 vector<string> search_directories;
280233 auto custom_extension_directory = Settings::Get<ExtensionDirectorySetting>(db);
280234 if (!custom_extension_directory.empty()) {
280235 search_directories.push_back(custom_extension_directory);
280236 }
280237
280238 if (!
db.config.options.extension_directories.empty()) {
280239
280240 for (
const auto &dir :
db.config.options.extension_directories) {
280241 search_directories.push_back(dir);
280242 }
280243 }
280244
280245
280246 if (search_directories.empty()) {
280247 for (const auto &path : ExtensionHelper::DefaultExtensionFolders(fs)) {
280248 search_directories.push_back(path);
280249 }
280250 }
280251
280252
280253 bool found = false;
280254 for (const auto &directory : search_directories) {
280255 filename = ComputeLocalExtensionPath(directory, extension_name);
280256 if (fs.FileExists(filename)) {
280257 found = true;
280258 break;
280259 }
280260 }
280261
280262
280263 if (!found) {
280264 filename = ComputeLocalExtensionPath(search_directories[0], extension_name);
280265 }
280266#endif
280267 } else {
280268 direct_load = true;
280269 filename = fs.ExpandPath(filename);
280270 }
280272 throw PermissionException(
280273 "DuckDB extensions are files ending with '.duckdb_extension', loading different "
280274 "files is not possible, error while loading from '%s', consider 'INSTALL <path>; LOAD <name>;'",
280275 filename);
280276 }
280277 if (!fs.FileExists(filename)) {
280278 string message;
280279 bool exact_match = ExtensionHelper::CreateSuggestions(extension, message);
280280 if (exact_match) {
280281 message += "\nInstall it first using \"INSTALL " + extension + "\".";
280282 }
280284 return false;
280285 }
280286
280288
280289
280290 auto parsed_metadata = ParseExtensionMetaData(*handle);
280291
280292 auto metadata_mismatch_error = parsed_metadata.GetInvalidMetadataError();
280293
280294 if (!metadata_mismatch_error.empty()) {
280295 metadata_mismatch_error =
StringUtil::Format(
"Failed to load '%s', %s", extension, metadata_mismatch_error);
280296 }
280297
280298 if (!Settings::Get<AllowUnsignedExtensionsSetting>(db)) {
280299 bool signature_valid;
280300 if (parsed_metadata.AppearsValid()) {
280301 bool allow_community_extensions = Settings::Get<AllowCommunityExtensionsSetting>(db);
280302 signature_valid = CheckExtensionSignature(*handle, parsed_metadata, allow_community_extensions);
280303 } else {
280304 signature_valid = false;
280305 }
280306
280307 if (!metadata_mismatch_error.empty()) {
280308 throw InvalidInputException(metadata_mismatch_error);
280309 }
280310
280311 if (!signature_valid) {
280312 throw IOException(
db.config.error_manager->FormatException(ErrorType::UNSIGNED_EXTENSION, filename));
280313 }
280314 } else if (!Settings::Get<AllowExtensionsMetadataMismatchSetting>(db)) {
280315 if (!metadata_mismatch_error.empty()) {
280316
280317
280318 throw InvalidInputException(metadata_mismatch_error);
280319 }
280320 }
280321
280322 auto filebase = fs.ExtractBaseName(filename);
280323
280324#ifdef WASM_LOADABLE_EXTENSIONS
280325 EM_ASM(
280326 {
280327
280328
280329 const xhr = new XMLHttpRequest();
280330 xhr.open("GET", UTF8ToString($0), false);
280331 xhr.responseType = "arraybuffer";
280332 xhr.send(null);
280333 var uInt8Array = xhr.response;
280334 WebAssembly.validate(uInt8Array);
280335 console.log('Loading extension ', UTF8ToString($1));
280336
280337
280338 FS.writeFile(UTF8ToString($1), new Uint8Array(uInt8Array));
280339 },
280340 filename.c_str(), filebase.c_str());
280341 auto dopen_from = filebase;
280342#else
280343 auto dopen_from = filename;
280344#endif
280345
280346 auto lib_hdl = dlopen(dopen_from.c_str(), RTLD_NOW | RTLD_LOCAL);
280347 if (!lib_hdl) {
280348 throw IOException("Extension \"%s\" could not be loaded: %s", filename, GetDLError());
280349 }
280350
280352
280353
280354 result.filebase = lowercase_extension_name;
280355 result.filename = filename;
280356 result.lib_hdl = lib_hdl;
280357 result.abi_type = parsed_metadata.abi_type;
280358
280359 if (!direct_load) {
280360 auto info_file_name = filename + ".info";
280361
280363
280364 if (result.install_info->mode == ExtensionInstallMode::UNKNOWN) {
280365
280366 result.install_info->version = parsed_metadata.extension_version;
280367 }
280368
280369 if (result.install_info->version != parsed_metadata.extension_version) {
280370 throw IOException("Metadata mismatch detected when loading extension '%s'\nPlease try reinstalling the "
280371 "extension using `FORCE INSTALL '%s'`",
280372 filename, extension);
280373 }
280374 } else {
280375 result.install_info = make_uniq<ExtensionInstallInfo>();
280377 result.install_info->full_path = filename;
280378 result.install_info->version = parsed_metadata.extension_version;
280379 }
280380
280381 return true;
280382#endif
280383}
static string ExtensionUrlTemplate(optional_ptr< const DatabaseInstance > db, const ExtensionRepository &repository, const string &version)
Get the extension url template, containing placeholders for version, platform and extension name.
Definition duckdb.cpp:279386
static string ExtensionFinalizeUrlTemplate(const string &url, const string &name)
Return the extension url template with the variables replaced.
Definition duckdb.cpp:279403
static unique_ptr< ExtensionInstallInfo > TryReadInfoFile(FileSystem &fs, const string &info_file_path, const string &extension_name)
Try to read install info. returns ExtensionInstallMode::UNKNOWN on missing file, and throws on corrup...
Definition duckdb.cpp:281312
static constexpr FileOpenFlags FILE_FLAGS_READ
Open file with read access.
Definition duckdb.hpp:7850
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
@ NOT_INSTALLED
Extension is not installed, for example the extension might be directly loaded without installing.
Definition duckdb.hpp:53913