From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#65274: 29.0.91; [feature request] Readonly argument for emacs built-in sqlite support. Date: Sat, 19 Aug 2023 12:21:05 +0300 Message-ID: <83v8dbz8u6.fsf@gnu.org> References: <878rbc1eem.fsf@ngraves.fr> <875y5iznne.fsf@ngraves.fr> <83350lbmre.fsf@gnu.org> <87wmxvl2ar.fsf@ngraves.fr> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20057"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 65274@debbugs.gnu.org To: Nicolas Graves Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Aug 19 11:22:20 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qXIA0-0004wz-IX for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 19 Aug 2023 11:22:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXI9k-0001eo-NW; Sat, 19 Aug 2023 05:22:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qXI9i-0001ee-R5 for bug-gnu-emacs@gnu.org; Sat, 19 Aug 2023 05:22:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXI9h-0007ra-Td for bug-gnu-emacs@gnu.org; Sat, 19 Aug 2023 05:22:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qXI9i-0006Fe-HA for bug-gnu-emacs@gnu.org; Sat, 19 Aug 2023 05:22:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 19 Aug 2023 09:22:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65274 X-GNU-PR-Package: emacs Original-Received: via spool by 65274-submit@debbugs.gnu.org id=B65274.169243686323950 (code B ref 65274); Sat, 19 Aug 2023 09:22:02 +0000 Original-Received: (at 65274) by debbugs.gnu.org; 19 Aug 2023 09:21:03 +0000 Original-Received: from localhost ([127.0.0.1]:49459 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qXI8k-0006ED-SH for submit@debbugs.gnu.org; Sat, 19 Aug 2023 05:21:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33660) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qXI8i-0006Dc-MZ for 65274@debbugs.gnu.org; Sat, 19 Aug 2023 05:21:01 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qXI8b-0007mm-1V; Sat, 19 Aug 2023 05:20:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=ANiabqcrotZPoZo3IlVWQ+WrRA+pt966+cka7McW0j4=; b=odxRBT8pwCSR UeRCnHnvn9z1KySvcFVNuoLn+jNN29dVI2N6wiBwXqUSrNBg7nou82bxnOQ1+Cihf2+wjM9sa+b4e HrKBxgIN6nC9ilL8F8eJgr2O6aWi6tH/JU88rGUuz+zz7zuCc4LQt8n0HyEO7cRbNa/cY843H2fpv q7+TkZorlHS6NyfMM3vEmmnYqxAiDNPFqe8SXh+d9ueWo5LKEi9mQcRcXHJSCMX+Gjlpd3Zc/IBA5 trKYE7YSulk8VmzWvseLlDcBCYZiixSX1W7CXqPyyHxgxz5mOXBnYRhmgFXsSuoW9XQqpLDHKuf8q /d3+gfO4v45Oa6Jh+jUppw==; In-Reply-To: <87wmxvl2ar.fsf@ngraves.fr> (message from Nicolas Graves on Wed, 16 Aug 2023 12:17:00 +0200) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:267834 Archived-At: > From: Nicolas Graves > Cc: 65274@debbugs.gnu.org > Date: Wed, 16 Aug 2023 12:17:00 +0200 > > My use-case is the package ibrowse.el, which uses sql commands to parse > in real time the database of browser (for history or bookmarks, > depending on the case) > (https://git.sr.ht/~ngraves/ibrowse.el/tree/master/item/ibrowse-sql.el). > > Web browsers use very frequent updates and most of the time, even > trying to access it with readonly mode is not enough (I don't remember > all the details, but it's something along the lines of you can't open > even in read-only mode if the database file is currently being > updated). > > Most other emacs packages which try to do the same thing circumvent the > issue by copying the whole database to /tmp and parsing it from > there. I've found another solution which doesn't require to copy the > file and it's to open it in immutable mode > (https://git.sr.ht/~ngraves/ibrowse.el/tree/master/item/ibrowse-sql.el#L58) > I remember the following stackoverflow link lead me to try this: > https://stackoverflow.com/questions/7857755/is-it-possible-to-open-a-locked-sqlite-database-in-read-only-mode > > On the CLI, it's simply done by adding the uri file:...?immutable=1. The SQL > manual warns for possible errors if the database is being updated at the > same time, but I don't keep an open connection so I don't experience any > error whatsoever. One stackoverflow comment also hints about how it > could be done, this probably applies to emacs as well: > > "To achieve the same thing with most SQLite drivers, because we can't directly set SQLITE_IOCAP_IMMUTABLE, the best way is to open the connection with the flag SQLITE_OPEN_READONLY | SQLITE_OPEN_URI to allow passing the file:...?immutable=1 URI as filename." Thanks. I'm not sure I understood all the intricacies of this use case, but could you please see if the patches below make your use case possible? diff --git a/src/sqlite.c b/src/sqlite.c index fd528f2..f68edb9 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -277,13 +277,20 @@ check_sqlite (Lisp_Object db, bool is_statement) static int db_count = 0; -DEFUN ("sqlite-open", Fsqlite_open, Ssqlite_open, 0, 1, 0, +DEFUN ("sqlite-open", Fsqlite_open, Ssqlite_open, 0, 2, 0, doc: /* Open FILE as an sqlite database. -If FILE is nil, an in-memory database will be opened instead. */) - (Lisp_Object file) +If FILE is nil, an in-memory database will be opened instead. +If READONLY is non-nil, open the database in read-only mode, +otherwise open it in read-write mode. */) + (Lisp_Object file, Lisp_Object readonly) { Lisp_Object name; - int flags = (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); + int flags; + + if (!NILP (readonly)) + flags = SQLITE_OPEN_READONLY; + else + flags = (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); #ifdef SQLITE_OPEN_FULLMUTEX flags |= SQLITE_OPEN_FULLMUTEX; #endif