SQLite Forum

How to get the extension path?
Login

How to get the extension path?

(1) By anonymous on 2021-02-26 10:17:40 [link] [source]

Hi,

I’m building a SQLite loadable extension, and this extension will read some other txt files. I will put the extension and txt file in the same folder, but how should I get the extension’s abs path?

I search the web, different platform have different solution. I’m wondering if we have a unified way in sqlite? Or what’s the recommend way to do this?

Thank you so much!

(2) By anonymous on 2021-02-26 12:49:56 in reply to 1 [link] [source]

On a Linux box I have stored the shared libraries as follows:
/usr/lib/sqlite3/ {all shared libraries in use}

To load the shared libraries automatically modify the file (or create one) called:
.sqliterc

I use the following shared libraries and added these lines:
.load /usr/lib/sqlite3/pcre
.load /usr/lib/sqlite3/libuic

If you write a program that uses the SQLite API you have to load these libraries using the API load_extension function.

(3) By Fenjin Wang (wangfenjin) on 2021-02-27 02:32:41 in reply to 2 [link] [source]

Sorry it's a misunderstanding.

I mean let's say I make an extension named libcode.so, I can put the libcode.so anywhere and use the full path to load it into sqlite.

In the source code of libcode.so, how can I get the full path of libcode.so

(4) By Larry Brasfield (larrybr) on 2021-02-27 03:24:55 in reply to 3 [link] [source]

The SQLite library APIs do not provide a way to get that information directly.

However, to load an extension image dynamically, sqlite3_load_extension() relies upon methods provided by a VFS. (virtual file system access + exposure of certain common OS features) You could probably arrange to capture the last pathname passed to the VFS's xDlOpen() function and pass that into your extension via an entry point not used for its role as a SQLite extension. You would be augmenting the stock VFS, but this is often done by SQLite library users, particularly for embedded systems.

In this way, you would not need to modify the SQLite library proper.

Look for "#if SQLITE_OS_UNIX" in the amalgamation to find the VFS for Unix-like OSs.

(5) By Fenjin Wang (wangfenjin) on 2021-02-27 05:29:04 in reply to 4 [link] [source]

Thanks Larry

(6) By Keith Medcalf (kmedcalf) on 2021-02-27 06:36:06 in reply to 3 [link] [source]

Very important question: Why on earth would you want that information?

Second, this is an Operating System question and has nothing to do with SQLite3.

The loading and linkage of "dynamic load libraries" or "discontiguous saved sections" -- or even indeed individual executables -- is the concern of the Operating System and resort should be had to the documentation for the Operating System.

SQLite3 has no influence nor control over this process.

(7) By anonymous on 2021-02-27 10:20:56 in reply to 6 [link] [source]

Why on earth would you want that information?

With Windows, the search path for a DLL about to be loaded is the current directory/path, followed by the paths(s) specified in the PATH environment variable.

The usual trick is not copy the DLL to be loaded (and its dependencies) into the System or System32 folders. This is modifying an operating system folder - something usually to be avoided.

The alternative is to have your DLL (to be loaded with its dependencies at a location/path of your choice and then to switch to that location before attempting to load that DLL (and then revert to the original location).

Since you choose the location/path where you store the DLL & dependencies, there is NO need to figure out what that location is.

However, if the DLL is loaded from somewhere i.e. a location in the directories specified within the PATH environment variable, then being able to query the location wherefrom the DLL is loaded makes sense either for purposes of removing, upgrading, or re-locating the DLL & it dependencies. (The DLL could exist in multiple locations in the PATH).

(8.1) By Fenjin Wang (wangfenjin) on 2021-02-28 07:18:21 edited from 8.0 in reply to 6 [source]

Why on earth would you want that information?

In the title: this extension will read some other txt files. I will put the extension and txt file in the same folder. So if we can get the path of the extension, we will know where to read the txt files.

this is an Operating System question and has nothing to do with SQLite3.

Yes it's related to OS, but we can't say it has nothing to do with SQLite

Because SQLite should know the full path as it need to load the extension, for now seems SQLite just don't remember it and expose api about it. I ask this question to confirm it.

BTW, I think it will be good if SQLite have this info and maybe can pass to the sqlite_$(extension)_init() as a params

(9.1) By Fenjin Wang (wangfenjin) on 2021-02-28 07:10:05 edited from 9.0 in reply to 6 [link] [source]

For now my solution is make user pass the txt file path to the extension, it's doable but don't you think it'll be better we can infer that from the extension path?