Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bundled sqlite to 3.5.0 #47

Merged
merged 1 commit into from
Oct 1, 2014
Merged

update bundled sqlite to 3.5.0 #47

merged 1 commit into from
Oct 1, 2014

Conversation

mwotton
Copy link
Contributor

@mwotton mwotton commented Jun 27, 2014

i noticed there was a new sqlite, so figured the bundling could be updated. No sweat if you've already got a process for doing this :)

@IreneKnapp
Copy link
Owner

I appreciate it! There's actually a one-line change that we have to make every time we upgrade. :( It has to do with the stat64 structure on OS X... I can get to it sometime this weekend, or if you are motivated and want to learn the details, you can educate yourself by examining a pull request for a previous sqlite version upgrade. :)

@mwotton
Copy link
Contributor Author

mwotton commented Jun 28, 2014

i couldn't test it on OS X anyway, so probably better for you to do it :)

On Sat, Jun 28, 2014 at 7:05 AM, Irene Knapp notifications@github.com
wrote:

I appreciate it! There's actually a one-line change that we have to make
every time we upgrade. :( It has to do with the stat64 structure on OS X...
I can get to it sometime this weekend, or if you are motivated and want to
learn the details, you can educate yourself by examining a pull request for
a previous sqlite version upgrade. :)


Reply to this email directly or view it on GitHub
#47 (comment)
.

A UNIX signature isn't a return address, it's the ASCII equivalent of a
black velvet clown painting. It's a rectangle of carets surrounding a
quote from a literary giant of weeniedom like Heinlein or Dr. Who.
-- Chris Maeda

@IreneKnapp
Copy link
Owner

Fair enough! :)

@mwotton
Copy link
Contributor Author

mwotton commented Jul 29, 2014

i think

diff --git a/cbits/sqlite3.c b/cbits/sqlite3.c
index 9228d24..fb437d8 100644
--- a/cbits/sqlite3.c
+++ b/cbits/sqlite3.c
@@ -24251,6 +24251,17 @@ static int posixFchown(int fd, uid_t uid, gid_t gid){

 /* Forward reference */
 static int openDirectory(const char*, int*);
+
+int stat_local(const char *x, struct stat *y)
+{
+       return stat(x, y);
+}
+int fstat_local(int x, struct stat *y)
+{
+       return fstat(x, y);
+}
+
+
 static int unixGetpagesize(void);

 /*
@@ -24276,7 +24287,7 @@ static struct unix_syscall {
   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)

-  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
+  { "stat",         (sqlite3_syscall_ptr)stat_local,       0  },
 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)

 /*
@@ -24289,7 +24300,7 @@ static struct unix_syscall {
   { "fstat",        0,                 0  },
 #define osFstat(a,b,c)    0
 #else
-  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
+  { "fstat",        (sqlite3_syscall_ptr)fstat_local,      0  },
 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
 #endif

is the needed change? Anyway, it's working locally.

@mwotton
Copy link
Contributor Author

mwotton commented Jul 29, 2014

(i would update the PR but emacs stripped the whitespace from the c files so it looks like a massive commit)

@IreneKnapp
Copy link
Owner

Ah, excellent, thank you! I very much appreciate your doing this work. I tagged this issue with a new tag, "useful", so that we can find your research easily next time around, haha. I'll fix the whitespace at my end and get this merged in, sometime tonight. :)

@mwotton
Copy link
Contributor Author

mwotton commented Jul 29, 2014

Thanks Irene :) looking to remove one of my seemingly infinite list of cabal sandbox add-source invocations...

@mwotton
Copy link
Contributor Author

mwotton commented Sep 19, 2014

Hi Irene - is there something blocking releasing this to hackage?

nurpax added a commit that referenced this pull request Oct 1, 2014
update bundled sqlite to 3.5.0
@nurpax nurpax merged commit 6d0811a into IreneKnapp:master Oct 1, 2014
@nurpax
Copy link
Collaborator

nurpax commented Oct 1, 2014

I'll apply the OS X change separately.

@nurpax
Copy link
Collaborator

nurpax commented Oct 1, 2014

For the record, I think the author of this PR meant sqlite 3.8.5, not 3.5.

@nurpax
Copy link
Collaborator

nurpax commented Oct 1, 2014

@mwotton @IreneKnapp This is working for me on Mac OS X without applying the stat fix. I'll try to dig out from the issues database why it's required. If you know, please let me know.

EDIT: it's documented in commit 6890f68. I'll try if it still repros.

@IreneKnapp
Copy link
Owner

Yeah - I tagged that issue as "useful" for this reason!

@IreneKnapp
Copy link
Owner

Since it was a patch around an upstream issue, probably they finally fixed it. :)

@nurpax
Copy link
Collaborator

nurpax commented Oct 1, 2014

*Database.SQLite3> import Database.SQLite3
*Database.SQLite3 Database.SQLite3> import Data.Text
*Database.SQLite3 Database.SQLite3 Data.Text> conn <- open (pack ":memory:")
*Database.SQLite3 Database.SQLite3 Data.Text> 

is working fine. But I don't know if this is pertinent to some particular GHC and/or Mac OS X version.

I'm thinking I'll release without it and make a point release with this patch if someone complains.

EDIT: I think I'll just apply the path.

nurpax added a commit that referenced this pull request Oct 1, 2014
This reverts commit ffc6e42.

See #47 for more
information.

(cherry picked from commit 8576d6f)
@nurpax
Copy link
Collaborator

nurpax commented Oct 4, 2014

This is now in direct-sqlite-2.3.14 on Hackage.

nurpax added a commit that referenced this pull request Oct 31, 2016
See #47 for more
information.

(cherry picked from commit 2e532e2)
nurpax added a commit that referenced this pull request Dec 10, 2016
See #47 for more
information.

(cherry picked from commit 2e532e2)
mvoidex added a commit to mvoidex/direct-sqlite that referenced this pull request Sep 27, 2017
This reverts commit ffc6e42.

See IreneKnapp#47 for more
information.

(cherry picked from commit 8576d6f)
angerman added a commit to angerman/direct-sqlite that referenced this pull request Jan 8, 2018
There are two SQLite encryption options:
- [SQLite SEE](http://www.hwaci.com/sw/sqlite/see.html)
- [SQLCipher](https://www.zetetic.net/sqlcipher/)

SEE can be bought from the original authors, while
SQLCipher is an open source option.  This change adds
the SQLCipher amalgamation (`./configure && make sqlite3.c`)
from SQLCipher v3.4.2 release source code.  The `stat_local`
patch (IreneKnapp#47) has *NOT* be applied, as this works fine without
on macOS Sierra.

The SQLCipher engine can be compiled against commoncrypto (apple),
OpenSSL or LibTomCrypt. If any of the respective `-f`lags are
enable, the SQLCipher SQLite3 version is build.

With SQLCipher the `PRAGMA key` and `PRAGMA rekey` pragmas can
be used:
```
PRAGMA key = 'passphrase'; -- start with the existing database passphrase
PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase
```
and need to be issued as first command when working with
encrypted databases.

Together with the `sqlite-simple` package we can then build
something simple like the following encrypted databases access
```
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Database.SQLite.Simple as DB
import qualified Database.SQLite.Simple.FromRow as DB
import Data.Monoid ((<>))
import qualified Data.Text as T
import Data.String (fromString)
import Control.Exception

main :: IO ()
main = DB.withConnection "test.db" $ \conn -> do
  version <- getUserVersion' conn $ do
    putStrLn "Please provide the passphrase"
    getLine
  case version of
    0 -> do putStrLn "Please provide initial passphrase"
            passphrase <- getLine
            setKey conn passphrase
            initDb conn
            return ()
    _ -> putStrLn "TODO: Do something with the database..."
  return ()

setKey :: DB.Connection -> String -> IO ()
setKey conn passphrase = DB.execute_ conn . fromString $ "PRAGMA key = '" <> passphrase <> "'"

getUserVersion :: DB.Connection -> IO Int
getUserVersion conn = head <$> DB.query_ conn "PRAGMA user_version" >>= \case
  DB.Only n -> return n

-- | Tries to get the userVersion, and uses the handle to
-- request the password, until it succeeds.
getUserVersion' :: DB.Connection -> IO String -> IO Int
getUserVersion' conn getPassphrase = handleJust exFilter exHandle (getUserVersion conn)
  where exFilter :: DB.SQLError -> Maybe ()
        exFilter (DB.SQLError DB.ErrorNotADatabase _ _) = Just ()
        exFilter _ = Nothing
        exHandle :: () -> IO Int
        exHandle _ = do passphrase <- getPassphrase
                        setKey conn passphrase
                        getUserVersion' conn getPassphrase

-- | Initialize or migrate the database
initDb :: DB.Connection -> IO Int
initDb conn = getUserVersion conn >>= \case
  0 -> DB.execute_ conn "PRAGMA user_version = 1" >> return 1
  n -> return n
```
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants