Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
c2cc3b49
Commit
c2cc3b49
authored
Jan 18, 2009
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
database: pass database file name to db_init()
Don't include conf.h in database.c.
parent
004dfddc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
40 deletions
+45
-40
database.c
src/database.c
+36
-32
database.h
src/database.h
+5
-7
main.c
src/main.c
+4
-1
No files found.
src/database.c
View file @
c2cc3b49
...
@@ -21,7 +21,6 @@
...
@@ -21,7 +21,6 @@
#include "directory.h"
#include "directory.h"
#include "directory_save.h"
#include "directory_save.h"
#include "song.h"
#include "song.h"
#include "conf.h"
#include "path.h"
#include "path.h"
#include "stats.h"
#include "stats.h"
#include "config.h"
#include "config.h"
...
@@ -39,13 +38,17 @@
...
@@ -39,13 +38,17 @@
#undef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "database"
#define G_LOG_DOMAIN "database"
static
char
*
database_path
;
static
struct
directory
*
music_root
;
static
struct
directory
*
music_root
;
static
time_t
directory_dbModTime
;
static
time_t
directory_dbModTime
;
void
void
db_init
(
void
)
db_init
(
const
char
*
path
)
{
{
database_path
=
g_strdup
(
path
);
music_root
=
directory_new
(
""
,
NULL
);
music_root
=
directory_new
(
""
,
NULL
);
}
}
...
@@ -53,6 +56,15 @@ void
...
@@ -53,6 +56,15 @@ void
db_finish
(
void
)
db_finish
(
void
)
{
{
directory_free
(
music_root
);
directory_free
(
music_root
);
g_free
(
database_path
);
}
void
db_clear
(
void
)
{
directory_free
(
music_root
);
music_root
=
directory_new
(
""
,
NULL
);
}
}
struct
directory
*
struct
directory
*
...
@@ -120,42 +132,32 @@ db_walk(const char *name,
...
@@ -120,42 +132,32 @@ db_walk(const char *name,
return
directory_walk
(
directory
,
forEachSong
,
forEachDir
,
data
);
return
directory_walk
(
directory
,
forEachSong
,
forEachDir
,
data
);
}
}
static
char
*
db_get_file
(
void
)
{
struct
config_param
*
param
=
parseConfigFilePath
(
CONF_DB_FILE
,
1
);
assert
(
param
);
assert
(
param
->
value
);
return
param
->
value
;
}
bool
bool
db_check
(
void
)
db_check
(
void
)
{
{
struct
stat
st
;
struct
stat
st
;
char
*
dbFile
=
db_get_file
();
assert
(
database_path
!=
NULL
);
/* Check if the file exists */
/* Check if the file exists */
if
(
access
(
d
bFile
,
F_OK
))
{
if
(
access
(
d
atabase_path
,
F_OK
))
{
/* If the file doesn't exist, we can't check if we can write
/* If the file doesn't exist, we can't check if we can write
* it, so we are going to try to get the directory path, and
* it, so we are going to try to get the directory path, and
* see if we can write a file in that */
* see if we can write a file in that */
char
*
dirPath
=
g_path_get_dirname
(
d
bFile
);
char
*
dirPath
=
g_path_get_dirname
(
d
atabase_path
);
/* Check that the parent part of the path is a directory */
/* Check that the parent part of the path is a directory */
if
(
stat
(
dirPath
,
&
st
)
<
0
)
{
if
(
stat
(
dirPath
,
&
st
)
<
0
)
{
g_free
(
dirPath
);
g_free
(
dirPath
);
g_warning
(
"Couldn't stat parent directory of db file "
g_warning
(
"Couldn't stat parent directory of db file "
"
\"
%s
\"
: %s"
,
d
bFile
,
strerror
(
errno
));
"
\"
%s
\"
: %s"
,
d
atabase_path
,
strerror
(
errno
));
return
false
;
return
false
;
}
}
if
(
!
S_ISDIR
(
st
.
st_mode
))
{
if
(
!
S_ISDIR
(
st
.
st_mode
))
{
g_free
(
dirPath
);
g_free
(
dirPath
);
g_warning
(
"Couldn't create db file
\"
%s
\"
because the "
g_warning
(
"Couldn't create db file
\"
%s
\"
because the "
"parent path is not a directory"
,
d
bFile
);
"parent path is not a directory"
,
d
atabase_path
);
return
false
;
return
false
;
}
}
...
@@ -173,21 +175,21 @@ db_check(void)
...
@@ -173,21 +175,21 @@ db_check(void)
}
}
/* Path exists, now check if it's a regular file */
/* Path exists, now check if it's a regular file */
if
(
stat
(
d
bFile
,
&
st
)
<
0
)
{
if
(
stat
(
d
atabase_path
,
&
st
)
<
0
)
{
g_warning
(
"Couldn't stat db file
\"
%s
\"
: %s"
,
g_warning
(
"Couldn't stat db file
\"
%s
\"
: %s"
,
d
bFile
,
strerror
(
errno
));
d
atabase_path
,
strerror
(
errno
));
return
false
;
return
false
;
}
}
if
(
!
S_ISREG
(
st
.
st_mode
))
{
if
(
!
S_ISREG
(
st
.
st_mode
))
{
g_warning
(
"db file
\"
%s
\"
is not a regular file"
,
d
bFile
);
g_warning
(
"db file
\"
%s
\"
is not a regular file"
,
d
atabase_path
);
return
false
;
return
false
;
}
}
/* And check that we can write to it */
/* And check that we can write to it */
if
(
access
(
d
bFile
,
R_OK
|
W_OK
))
{
if
(
access
(
d
atabase_path
,
R_OK
|
W_OK
))
{
g_warning
(
"Can't open db file
\"
%s
\"
for reading/writing: %s"
,
g_warning
(
"Can't open db file
\"
%s
\"
for reading/writing: %s"
,
d
bFile
,
strerror
(
errno
));
d
atabase_path
,
strerror
(
errno
));
return
false
;
return
false
;
}
}
...
@@ -198,9 +200,11 @@ bool
...
@@ -198,9 +200,11 @@ bool
db_save
(
void
)
db_save
(
void
)
{
{
FILE
*
fp
;
FILE
*
fp
;
char
*
dbFile
=
db_get_file
();
struct
stat
st
;
struct
stat
st
;
assert
(
database_path
!=
NULL
);
assert
(
music_root
!=
NULL
);
g_debug
(
"removing empty directories from DB"
);
g_debug
(
"removing empty directories from DB"
);
directory_prune_empty
(
music_root
);
directory_prune_empty
(
music_root
);
...
@@ -210,10 +214,10 @@ db_save(void)
...
@@ -210,10 +214,10 @@ db_save(void)
g_debug
(
"writing DB"
);
g_debug
(
"writing DB"
);
fp
=
fopen
(
d
bFile
,
"w"
);
fp
=
fopen
(
d
atabase_path
,
"w"
);
if
(
!
fp
)
{
if
(
!
fp
)
{
g_warning
(
"unable to write to db file
\"
%s
\"
: %s"
,
g_warning
(
"unable to write to db file
\"
%s
\"
: %s"
,
d
bFile
,
strerror
(
errno
));
d
atabase_path
,
strerror
(
errno
));
return
false
;
return
false
;
}
}
...
@@ -232,7 +236,7 @@ db_save(void)
...
@@ -232,7 +236,7 @@ db_save(void)
while
(
fclose
(
fp
)
&&
errno
==
EINTR
);
while
(
fclose
(
fp
)
&&
errno
==
EINTR
);
if
(
stat
(
d
bFile
,
&
st
)
==
0
)
if
(
stat
(
d
atabase_path
,
&
st
)
==
0
)
directory_dbModTime
=
st
.
st_mtime
;
directory_dbModTime
=
st
.
st_mtime
;
return
true
;
return
true
;
...
@@ -242,19 +246,19 @@ bool
...
@@ -242,19 +246,19 @@ bool
db_load
(
void
)
db_load
(
void
)
{
{
FILE
*
fp
=
NULL
;
FILE
*
fp
=
NULL
;
char
*
dbFile
=
db_get_file
();
struct
stat
st
;
struct
stat
st
;
char
buffer
[
100
];
char
buffer
[
100
];
bool
foundFsCharset
=
false
,
foundVersion
=
false
;
bool
foundFsCharset
=
false
,
foundVersion
=
false
;
assert
(
database_path
!=
NULL
);
assert
(
music_root
!=
NULL
);
assert
(
music_root
!=
NULL
);
if
(
!
music_root
)
if
(
!
music_root
)
music_root
=
directory_new
(
""
,
NULL
);
music_root
=
directory_new
(
""
,
NULL
);
while
(
!
(
fp
=
fopen
(
d
bFile
,
"r"
))
&&
errno
==
EINTR
)
;
while
(
!
(
fp
=
fopen
(
d
atabase_path
,
"r"
))
&&
errno
==
EINTR
)
;
if
(
fp
==
NULL
)
{
if
(
fp
==
NULL
)
{
g_warning
(
"unable to open db file
\"
%s
\"
: %s"
,
g_warning
(
"unable to open db file
\"
%s
\"
: %s"
,
d
bFile
,
strerror
(
errno
));
d
atabase_path
,
strerror
(
errno
));
return
false
;
return
false
;
}
}
...
@@ -289,7 +293,7 @@ db_load(void)
...
@@ -289,7 +293,7 @@ db_load(void)
foundFsCharset
=
true
;
foundFsCharset
=
true
;
fsCharset
=
&
(
buffer
[
strlen
(
DIRECTORY_FS_CHARSET
)]);
fsCharset
=
&
(
buffer
[
strlen
(
DIRECTORY_FS_CHARSET
)]);
tempCharset
=
config_get_string
(
CONF_FS_CHARSET
,
NULL
);
tempCharset
=
path_get_fs_charset
(
);
if
(
tempCharset
!=
NULL
if
(
tempCharset
!=
NULL
&&
strcmp
(
fsCharset
,
tempCharset
))
{
&&
strcmp
(
fsCharset
,
tempCharset
))
{
g_message
(
"Using
\"
%s
\"
for the "
g_message
(
"Using
\"
%s
\"
for the "
...
@@ -312,7 +316,7 @@ db_load(void)
...
@@ -312,7 +316,7 @@ db_load(void)
stats_update
();
stats_update
();
if
(
stat
(
d
bFile
,
&
st
)
==
0
)
if
(
stat
(
d
atabase_path
,
&
st
)
==
0
)
directory_dbModTime
=
st
.
st_mtime
;
directory_dbModTime
=
st
.
st_mtime
;
return
true
;
return
true
;
...
...
src/database.h
View file @
c2cc3b49
...
@@ -27,9 +27,11 @@ struct directory;
...
@@ -27,9 +27,11 @@ struct directory;
/**
/**
* Initialize the database library.
* Initialize the database library.
*
* @param path the absolute path of the database file
*/
*/
void
void
db_init
(
void
);
db_init
(
const
char
*
path
);
void
void
db_finish
(
void
);
db_finish
(
void
);
...
@@ -37,12 +39,8 @@ db_finish(void);
...
@@ -37,12 +39,8 @@ db_finish(void);
/**
/**
* Clear the database.
* Clear the database.
*/
*/
static
inline
void
void
db_clear
(
void
)
db_clear
(
void
);
{
db_finish
();
db_init
();
}
struct
directory
*
struct
directory
*
db_get_root
(
void
);
db_get_root
(
void
);
...
...
src/main.c
View file @
c2cc3b49
...
@@ -126,7 +126,10 @@ static void changeToUser(void)
...
@@ -126,7 +126,10 @@ static void changeToUser(void)
static
void
openDB
(
Options
*
options
,
char
*
argv0
)
static
void
openDB
(
Options
*
options
,
char
*
argv0
)
{
{
db_init
();
struct
config_param
*
param
;
param
=
parseConfigFilePath
(
CONF_DB_FILE
,
true
);
db_init
(
param
->
value
);
if
(
options
->
createDB
>
0
||
!
db_load
())
{
if
(
options
->
createDB
>
0
||
!
db_load
())
{
unsigned
job
;
unsigned
job
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment