forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.c
More file actions
186 lines (149 loc) · 5.56 KB
/
Copy pathdatabase.c
File metadata and controls
186 lines (149 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
This file is part of darktable,
copyright (c) 2011 henrik andersson.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/darktable.h"
#include "common/debug.h"
#include "common/database.h"
#include "control/control.h"
#include "control/conf.h"
#include <sqlite3.h>
#include <glib.h>
#include <gio/gio.h>
typedef struct dt_database_t
{
gboolean is_new_database;
/* database filename */
gchar *dbfilename;
/* ondisk DB */
sqlite3 *handle;
} dt_database_t;
/* migrates database from old place to new */
static void _database_migrate_to_xdg_structure();
/* delete old mipmaps files */
static void _database_delete_mipmaps_files();
gboolean dt_database_is_new(const dt_database_t *db)
{
return db->is_new_database;
}
dt_database_t *dt_database_init(char *alternative)
{
/* migrate default database location to new default */
_database_migrate_to_xdg_structure();
/* delete old mipmaps files */
_database_delete_mipmaps_files();
/* lets construct the db filename */
gchar * dbname = NULL;
gchar dbfilename[DT_MAX_PATH_LEN] = {0};
gchar datadir[DT_MAX_PATH_LEN] = {0};
dt_loc_get_user_config_dir(datadir, DT_MAX_PATH_LEN);
if ( alternative == NULL )
{
dbname = dt_conf_get_string ("database");
if(!dbname) snprintf(dbfilename, DT_MAX_PATH_LEN, "%s/library.db", datadir);
else if(dbname[0] != '/') snprintf(dbfilename, DT_MAX_PATH_LEN, "%s/%s", datadir, dbname);
else snprintf(dbfilename, DT_MAX_PATH_LEN, "%s", dbname);
}
else
{
snprintf(dbfilename, DT_MAX_PATH_LEN, "%s", alternative);
dbname = g_file_get_basename (g_file_new_for_path(alternative));
}
/* create database */
dt_database_t *db = (dt_database_t *)g_malloc(sizeof(dt_database_t));
memset(db,0,sizeof(dt_database_t));
db->dbfilename = g_strdup(dbfilename);
db->is_new_database = FALSE;
/* test if databasefile is available */
if(!g_file_test(dbfilename, G_FILE_TEST_IS_REGULAR))
db->is_new_database = TRUE;
/* opening / creating database */
if(sqlite3_open(db->dbfilename, &db->handle))
{
fprintf(stderr, "[init] could not find database ");
if(dbname) fprintf(stderr, "`%s'!\n", dbname);
else fprintf(stderr, "\n");
fprintf(stderr, "[init] maybe your %s/darktablerc is corrupt?\n",datadir);
dt_loc_get_datadir(dbfilename, 512);
fprintf(stderr, "[init] try `cp %s/darktablerc %s/darktablerc'\n", dbfilename,datadir);
g_free(dbname);
g_free(db);
return NULL;
}
/* attach a memory database to db connection for use with temporary tables
used during instance life time, which is discarded on exit.
*/
sqlite3_exec(db->handle, "attach database ':memory:' as memory",NULL,NULL,NULL);
sqlite3_exec(db->handle, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
sqlite3_exec(db->handle, "PRAGMA journal_mode = MEMORY", NULL, NULL, NULL);
sqlite3_exec(db->handle, "PRAGMA page_size = 32768", NULL, NULL, NULL);
g_free(dbname);
return db;
}
void dt_database_destroy(const dt_database_t *db)
{
sqlite3_close(db->handle);
g_free((dt_database_t *)db);
}
sqlite3 *dt_database_get(const dt_database_t *db)
{
return db->handle;
}
const gchar *dt_database_get_path(const struct dt_database_t *db)
{
return db->dbfilename;
}
static void _database_migrate_to_xdg_structure()
{
gchar dbfilename[DT_MAX_PATH_LEN]= {0};
gchar *conf_db = dt_conf_get_string("database");
gchar datadir[DT_MAX_PATH_LEN] = {0};
dt_loc_get_datadir(datadir, DT_MAX_PATH_LEN);
if (conf_db && conf_db[0] != '/')
{
char *homedir = getenv ("HOME");
snprintf (dbfilename,DT_MAX_PATH_LEN,"%s/%s", homedir, conf_db);
if (g_file_test (dbfilename, G_FILE_TEST_EXISTS))
{
fprintf(stderr, "[init] moving database into new XDG directory structure\n");
char destdbname[DT_MAX_PATH_LEN]= {0};
snprintf(destdbname,DT_MAX_PATH_LEN,"%s/%s",datadir,"library.db");
if(!g_file_test (destdbname,G_FILE_TEST_EXISTS))
{
rename(dbfilename,destdbname);
dt_conf_set_string("database","library.db");
}
}
}
g_free(conf_db);
}
/* delete old mipmaps files */
static void _database_delete_mipmaps_files()
{
/* This migration is intended to be run only from 0.9.x to new cache in 1.0 */
// Directory
char cachedir[DT_MAX_PATH_LEN], mipmapfilename[DT_MAX_PATH_LEN];
dt_loc_get_user_cache_dir(cachedir, sizeof(cachedir));
snprintf(mipmapfilename, DT_MAX_PATH_LEN, "%s/mipmaps", cachedir);
if(access(mipmapfilename, F_OK) != -1)
{
fprintf(stderr, "[mipmap_cache] dropping old version file: %s\n", mipmapfilename);
unlink(mipmapfilename);
snprintf(mipmapfilename, DT_MAX_PATH_LEN, "%s/mipmaps.fallback", cachedir);
if(access(mipmapfilename, F_OK) != -1)
unlink(mipmapfilename);
}
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;