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

env “TMPDIR" can't change the tmp store diretory #1223

Open
runlilong opened this issue Feb 22, 2024 · 7 comments
Open

env “TMPDIR" can't change the tmp store diretory #1223

runlilong opened this issue Feb 22, 2024 · 7 comments

Comments

@runlilong
Copy link

my codes

package main

import (
	"database/sql"
	"log"
	"os"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	os.Setenv("TMPDIR", "/mnt/test")
	db, err := sql.Open("sqlite3", "db.bak")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	_, err = db.Exec("VACUUM")
	if err != nil {
		log.Fatal("Failed to vacuum database:", err)
	} else {
		log.Println("Database vacuumed successfully")
	}
}

❯ du -d1 -h db.bak
84M     db.bak

❯ df -h
/dev/loop0              1003K  931K  1.0K 100% /mnt/test

the result of it


azTempDirs[0] = NULL
azTempDirs[1] = /mnt/test
2024/02/21 20:40:06 Database vacuumed successfully

while sqlite3 report the error

❯ export TMPDIR=/mnt/test
❯ sqlite3 db.bak -cmd "VACUUM" .quit

Error: database or disk is full

which says sqlite3 cli used the specified TMPDIR but the code didn't.

@mattn
Copy link
Owner

mattn commented Feb 22, 2024

How about to use pragma?

PRAGMA temp_store_directory = '/path/to/the/tmpdir';

@runlilong
Copy link
Author

runlilong commented Feb 22, 2024

still not work

❯ sqlite3 db.bak -cmd "PRAGMA temp_store_directory = '/mnt/test'; VACUUM" .quit

Error: database or disk is full

...
_, err = db.Exec("PRAGMA temp_store_directory = '/mnt/test'; VACUUM")
...

❯ go run main.go
azTempDirs[0] = NULL
azTempDirs[1] = /mnt/test
2024/02/21 21:17:54 Database vacuumed successfully

@mattn
Copy link
Owner

mattn commented Feb 22, 2024

What is azTempDirs ?

@runlilong
Copy link
Author

from https://raw.githubusercontent.com/mattn/go-sqlite3/master/sqlite3-binding.c. I add a print statement to confirm that it can access the env variable successfully.

/*
** Directories to consider for temp files.
*/
static const char *azTempDirs[] = {
  0,
  0,
  "/var/tmp",
  "/usr/tmp",
  "/tmp",
  "."
};

/*
** Initialize first two members of azTempDirs[] array.
*/
static void unixTempFileInit(void){
  azTempDirs[0] = getenv("SQLITE_TMPDIR");
  azTempDirs[1] = getenv("TMPDIR");
  printf("azTempDirs[0] = %s\n", azTempDirs[0] ? azTempDirs[0] : "NULL");
  printf("azTempDirs[1] = %s\n", azTempDirs[1] ? azTempDirs[1] : "NULL");
}

@mattn
Copy link
Owner

mattn commented Feb 22, 2024

export SQLITE_TMPDIR=/mnt/test

How this work?

@runlilong
Copy link
Author

SQLITE_TMPDIR doesn't work for both of them

@mattn
Copy link
Owner

mattn commented Feb 22, 2024

$ ls -lah /data/nostr-relay.sqlite
-rw------- 1 mattn mattn 1.7G  2月 22 23:20 /data/nostr-relay.sqlite
$ sudo mkdir test
$ sudo chmod 600 test
$ TMPDIR=./test SQLITE_TMPDIR=./test sqlite3 /data/nostr-relay.sqlite -cmd vacuum .quit

This is succeeded. sqlite3 cli really look TMPDIR for vacuum?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants