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
8e3982dd
Commit
8e3982dd
authored
Jan 14, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
InotifySource: convert to a class
parent
e83f805b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
105 deletions
+101
-105
InotifySource.cxx
src/InotifySource.cxx
+40
-60
InotifySource.hxx
src/InotifySource.hxx
+48
-28
InotifyUpdate.cxx
src/InotifyUpdate.cxx
+8
-10
run_inotify.cxx
test/run_inotify.cxx
+5
-7
No files found.
src/InotifySource.cxx
View file @
8e3982dd
...
@@ -36,22 +36,6 @@ extern "C" {
...
@@ -36,22 +36,6 @@ extern "C" {
#undef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "inotify"
#define G_LOG_DOMAIN "inotify"
struct
mpd_inotify_source
{
int
fd
;
GIOChannel
*
channel
;
/**
* The channel's source id in the GLib main loop.
*/
guint
id
;
struct
fifo_buffer
*
buffer
;
mpd_inotify_callback_t
callback
;
void
*
callback_ctx
;
};
/**
/**
* A GQuark for GError instances.
* A GQuark for GError instances.
*/
*/
...
@@ -61,35 +45,32 @@ mpd_inotify_quark(void)
...
@@ -61,35 +45,32 @@ mpd_inotify_quark(void)
return
g_quark_from_static_string
(
"inotify"
);
return
g_quark_from_static_string
(
"inotify"
);
}
}
static
gboolean
inline
void
mpd_inotify_in_event
(
G_GNUC_UNUSED
GIOChannel
*
_source
,
InotifySource
::
InEvent
()
G_GNUC_UNUSED
GIOCondition
condition
,
gpointer
data
)
{
{
struct
mpd_inotify_source
*
source
=
(
struct
mpd_inotify_source
*
)
data
;
void
*
dest
;
void
*
dest
;
size_t
length
;
size_t
length
;
ssize_t
nbytes
;
ssize_t
nbytes
;
dest
=
fifo_buffer_write
(
source
->
buffer
,
&
length
);
dest
=
fifo_buffer_write
(
buffer
,
&
length
);
if
(
dest
==
NULL
)
if
(
dest
==
NULL
)
MPD_ERROR
(
"buffer full"
);
MPD_ERROR
(
"buffer full"
);
nbytes
=
read
(
source
->
fd
,
dest
,
length
);
nbytes
=
read
(
fd
,
dest
,
length
);
if
(
nbytes
<
0
)
if
(
nbytes
<
0
)
MPD_ERROR
(
"failed to read from inotify: %s"
,
MPD_ERROR
(
"failed to read from inotify: %s"
,
g_strerror
(
errno
));
g_strerror
(
errno
));
if
(
nbytes
==
0
)
if
(
nbytes
==
0
)
MPD_ERROR
(
"end of file from inotify"
);
MPD_ERROR
(
"end of file from inotify"
);
fifo_buffer_append
(
source
->
buffer
,
nbytes
);
fifo_buffer_append
(
buffer
,
nbytes
);
while
(
true
)
{
while
(
true
)
{
const
char
*
name
;
const
char
*
name
;
const
struct
inotify_event
*
event
=
const
struct
inotify_event
*
event
=
(
const
struct
inotify_event
*
)
(
const
struct
inotify_event
*
)
fifo_buffer_read
(
source
->
buffer
,
&
length
);
fifo_buffer_read
(
buffer
,
&
length
);
if
(
event
==
NULL
||
length
<
sizeof
(
*
event
)
||
if
(
event
==
NULL
||
length
<
sizeof
(
*
event
)
||
length
<
sizeof
(
*
event
)
+
event
->
len
)
length
<
sizeof
(
*
event
)
+
event
->
len
)
break
;
break
;
...
@@ -99,59 +80,58 @@ mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel *_source,
...
@@ -99,59 +80,58 @@ mpd_inotify_in_event(G_GNUC_UNUSED GIOChannel *_source,
else
else
name
=
NULL
;
name
=
NULL
;
source
->
callback
(
event
->
wd
,
event
->
mask
,
name
,
callback
(
event
->
wd
,
event
->
mask
,
name
,
callback_ctx
);
source
->
callback_ctx
);
fifo_buffer_consume
(
buffer
,
sizeof
(
*
event
)
+
event
->
len
);
fifo_buffer_consume
(
source
->
buffer
,
sizeof
(
*
event
)
+
event
->
len
);
}
}
}
gboolean
InotifySource
::
InEvent
(
G_GNUC_UNUSED
GIOChannel
*
_source
,
G_GNUC_UNUSED
GIOCondition
condition
,
gpointer
data
)
{
InotifySource
&
source
=
*
(
InotifySource
*
)
data
;
source
.
InEvent
();
return
true
;
return
true
;
}
}
struct
mpd_inotify_source
*
inline
mpd_inotify_source_new
(
mpd_inotify_callback_t
callback
,
void
*
callback_ctx
,
InotifySource
::
InotifySource
(
mpd_inotify_callback_t
_callback
,
void
*
_ctx
,
GError
**
error_r
)
int
_fd
)
:
callback
(
_callback
),
callback_ctx
(
_ctx
),
fd
(
_fd
),
channel
(
g_io_channel_unix_new
(
fd
)),
id
(
g_io_add_watch
(
channel
,
G_IO_IN
,
InEvent
,
this
)),
buffer
(
fifo_buffer_new
(
4096
))
{
{
struct
mpd_inotify_source
*
source
=
}
g_new
(
struct
mpd_inotify_source
,
1
);
source
->
fd
=
inotify_init_cloexec
();
InotifySource
*
if
(
source
->
fd
<
0
)
{
InotifySource
::
Create
(
mpd_inotify_callback_t
callback
,
void
*
callback_ctx
,
GError
**
error_r
)
{
int
fd
=
inotify_init_cloexec
();
if
(
fd
<
0
)
{
g_set_error
(
error_r
,
mpd_inotify_quark
(),
errno
,
g_set_error
(
error_r
,
mpd_inotify_quark
(),
errno
,
"inotify_init() has failed: %s"
,
"inotify_init() has failed: %s"
,
g_strerror
(
errno
));
g_strerror
(
errno
));
g_free
(
source
);
return
NULL
;
return
NULL
;
}
}
source
->
buffer
=
fifo_buffer_new
(
4096
);
return
new
InotifySource
(
callback
,
callback_ctx
,
fd
);
source
->
channel
=
g_io_channel_unix_new
(
source
->
fd
);
source
->
id
=
g_io_add_watch
(
source
->
channel
,
G_IO_IN
,
mpd_inotify_in_event
,
source
);
source
->
callback
=
callback
;
source
->
callback_ctx
=
callback_ctx
;
return
source
;
}
}
void
InotifySource
::~
InotifySource
()
mpd_inotify_source_free
(
struct
mpd_inotify_source
*
source
)
{
{
g_source_remove
(
source
->
id
);
g_source_remove
(
id
);
g_io_channel_unref
(
source
->
channel
);
g_io_channel_unref
(
channel
);
fifo_buffer_free
(
source
->
buffer
);
fifo_buffer_free
(
buffer
);
close
(
source
->
fd
);
close
(
fd
);
g_free
(
source
);
}
}
int
int
mpd_inotify_source_add
(
struct
mpd_inotify_source
*
source
,
InotifySource
::
Add
(
const
char
*
path_fs
,
unsigned
mask
,
GError
**
error_r
)
const
char
*
path_fs
,
unsigned
mask
,
GError
**
error_r
)
{
{
int
wd
=
inotify_add_watch
(
source
->
fd
,
path_fs
,
mask
);
int
wd
=
inotify_add_watch
(
fd
,
path_fs
,
mask
);
if
(
wd
<
0
)
if
(
wd
<
0
)
g_set_error
(
error_r
,
mpd_inotify_quark
(),
errno
,
g_set_error
(
error_r
,
mpd_inotify_quark
(),
errno
,
"inotify_add_watch() has failed: %s"
,
"inotify_add_watch() has failed: %s"
,
...
@@ -161,9 +141,9 @@ mpd_inotify_source_add(struct mpd_inotify_source *source,
...
@@ -161,9 +141,9 @@ mpd_inotify_source_add(struct mpd_inotify_source *source,
}
}
void
void
mpd_inotify_source_rm
(
struct
mpd_inotify_source
*
source
,
unsigned
wd
)
InotifySource
::
Remove
(
unsigned
wd
)
{
{
int
ret
=
inotify_rm_watch
(
source
->
fd
,
wd
);
int
ret
=
inotify_rm_watch
(
fd
,
wd
);
if
(
ret
<
0
&&
errno
!=
EINVAL
)
if
(
ret
<
0
&&
errno
!=
EINVAL
)
g_warning
(
"inotify_rm_watch() has failed: %s"
,
g_warning
(
"inotify_rm_watch() has failed: %s"
,
g_strerror
(
errno
));
g_strerror
(
errno
));
...
...
src/InotifySource.hxx
View file @
8e3982dd
...
@@ -22,40 +22,60 @@
...
@@ -22,40 +22,60 @@
#include "gerror.h"
#include "gerror.h"
#include <glib.h>
typedef
void
(
*
mpd_inotify_callback_t
)(
int
wd
,
unsigned
mask
,
typedef
void
(
*
mpd_inotify_callback_t
)(
int
wd
,
unsigned
mask
,
const
char
*
name
,
void
*
ctx
);
const
char
*
name
,
void
*
ctx
);
struct
mpd_inotify_source
;
class
InotifySource
{
mpd_inotify_callback_t
callback
;
void
*
callback_ctx
;
/**
int
fd
;
* Creates a new inotify source and registers it in the GLib main
* loop.
*
* @param a callback invoked for events received from the kernel
*/
struct
mpd_inotify_source
*
mpd_inotify_source_new
(
mpd_inotify_callback_t
callback
,
void
*
callback_ctx
,
GError
**
error_r
);
void
GIOChannel
*
channel
;
mpd_inotify_source_free
(
struct
mpd_inotify_source
*
source
);
/**
/**
* Adds a path to the notify list.
* The channel's source id in the GLib main loop.
*
*/
* @return a watch descriptor or -1 on error
guint
id
;
*/
int
mpd_inotify_source_add
(
struct
mpd_inotify_source
*
source
,
const
char
*
path_fs
,
unsigned
mask
,
GError
**
error_r
);
/**
struct
fifo_buffer
*
buffer
;
* Removes a path from the notify list.
*
InotifySource
(
mpd_inotify_callback_t
callback
,
void
*
ctx
,
int
fd
);
* @param wd the watch descriptor returned by mpd_inotify_source_add()
*/
public
:
void
/**
mpd_inotify_source_rm
(
struct
mpd_inotify_source
*
source
,
unsigned
wd
);
* Creates a new inotify source and registers it in the GLib main
* loop.
*
* @param a callback invoked for events received from the kernel
*/
static
InotifySource
*
Create
(
mpd_inotify_callback_t
callback
,
void
*
ctx
,
GError
**
error_r
);
~
InotifySource
();
/**
* Adds a path to the notify list.
*
* @return a watch descriptor or -1 on error
*/
int
Add
(
const
char
*
path_fs
,
unsigned
mask
,
GError
**
error_r
);
/**
* Removes a path from the notify list.
*
* @param wd the watch descriptor returned by mpd_inotify_source_add()
*/
void
Remove
(
unsigned
wd
);
private
:
void
InEvent
();
static
gboolean
InEvent
(
GIOChannel
*
source
,
GIOCondition
condition
,
gpointer
data
);
};
#endif
#endif
src/InotifyUpdate.cxx
View file @
8e3982dd
...
@@ -56,7 +56,7 @@ struct watch_directory {
...
@@ -56,7 +56,7 @@ struct watch_directory {
GList
*
children
;
GList
*
children
;
};
};
static
struct
mpd_inotify_s
ource
*
inotify_source
;
static
InotifyS
ource
*
inotify_source
;
static
unsigned
inotify_max_depth
;
static
unsigned
inotify_max_depth
;
static
struct
watch_directory
inotify_root
;
static
struct
watch_directory
inotify_root
;
...
@@ -117,7 +117,7 @@ remove_watch_directory(struct watch_directory *directory)
...
@@ -117,7 +117,7 @@ remove_watch_directory(struct watch_directory *directory)
directory
->
parent
->
children
=
directory
->
parent
->
children
=
g_list_remove
(
directory
->
parent
->
children
,
directory
);
g_list_remove
(
directory
->
parent
->
children
,
directory
);
mpd_inotify_source_rm
(
inotify_source
,
directory
->
descriptor
);
inotify_source
->
Remove
(
directory
->
descriptor
);
g_free
(
directory
->
name
);
g_free
(
directory
->
name
);
g_slice_free
(
struct
watch_directory
,
directory
);
g_slice_free
(
struct
watch_directory
,
directory
);
}
}
...
@@ -195,8 +195,7 @@ recursive_watch_subdirectories(struct watch_directory *directory,
...
@@ -195,8 +195,7 @@ recursive_watch_subdirectories(struct watch_directory *directory,
continue
;
continue
;
}
}
ret
=
mpd_inotify_source_add
(
inotify_source
,
child_path_fs
,
ret
=
inotify_source
->
Add
(
child_path_fs
,
IN_MASK
,
&
error
);
IN_MASK
,
&
error
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
g_warning
(
"Failed to register %s: %s"
,
g_warning
(
"Failed to register %s: %s"
,
child_path_fs
,
error
->
message
);
child_path_fs
,
error
->
message
);
...
@@ -317,8 +316,8 @@ mpd_inotify_init(unsigned max_depth)
...
@@ -317,8 +316,8 @@ mpd_inotify_init(unsigned max_depth)
return
;
return
;
}
}
inotify_source
=
mpd_inotify_source_new
(
mpd_inotify_callback
,
NULL
,
inotify_source
=
InotifySource
::
Create
(
mpd_inotify_callback
,
nullptr
,
&
error
);
&
error
);
if
(
inotify_source
==
NULL
)
{
if
(
inotify_source
==
NULL
)
{
g_warning
(
"%s"
,
error
->
message
);
g_warning
(
"%s"
,
error
->
message
);
g_error_free
(
error
);
g_error_free
(
error
);
...
@@ -328,12 +327,11 @@ mpd_inotify_init(unsigned max_depth)
...
@@ -328,12 +327,11 @@ mpd_inotify_init(unsigned max_depth)
inotify_max_depth
=
max_depth
;
inotify_max_depth
=
max_depth
;
inotify_root
.
name
=
g_strdup
(
path
);
inotify_root
.
name
=
g_strdup
(
path
);
inotify_root
.
descriptor
=
mpd_inotify_source_add
(
inotify_source
,
path
,
inotify_root
.
descriptor
=
inotify_source
->
Add
(
path
,
IN_MASK
,
&
error
);
IN_MASK
,
&
error
);
if
(
inotify_root
.
descriptor
<
0
)
{
if
(
inotify_root
.
descriptor
<
0
)
{
g_warning
(
"%s"
,
error
->
message
);
g_warning
(
"%s"
,
error
->
message
);
g_error_free
(
error
);
g_error_free
(
error
);
mpd_inotify_source_free
(
inotify_source
)
;
delete
inotify_source
;
inotify_source
=
NULL
;
inotify_source
=
NULL
;
return
;
return
;
}
}
...
@@ -370,7 +368,7 @@ mpd_inotify_finish(void)
...
@@ -370,7 +368,7 @@ mpd_inotify_finish(void)
return
;
return
;
mpd_inotify_queue_finish
();
mpd_inotify_queue_finish
();
mpd_inotify_source_free
(
inotify_source
)
;
delete
inotify_source
;
g_tree_foreach
(
inotify_directories
,
free_watch_directory
,
NULL
);
g_tree_foreach
(
inotify_directories
,
free_watch_directory
,
NULL
);
g_tree_destroy
(
inotify_directories
);
g_tree_destroy
(
inotify_directories
);
...
...
test/run_inotify.cxx
View file @
8e3982dd
...
@@ -61,19 +61,17 @@ int main(int argc, char **argv)
...
@@ -61,19 +61,17 @@ int main(int argc, char **argv)
path
=
argv
[
1
];
path
=
argv
[
1
];
struct
mpd_inotify_source
*
source
=
InotifySource
*
source
=
InotifySource
::
Create
(
my_inotify_callback
,
mpd_inotify_source_new
(
my_inotify_callback
,
NULL
,
nullptr
,
&
error
);
&
error
);
if
(
source
==
NULL
)
{
if
(
source
==
NULL
)
{
g_warning
(
"%s"
,
error
->
message
);
g_warning
(
"%s"
,
error
->
message
);
g_error_free
(
error
);
g_error_free
(
error
);
return
2
;
return
2
;
}
}
int
descriptor
=
mpd_inotify_source_add
(
source
,
path
,
int
descriptor
=
source
->
Add
(
path
,
IN_MASK
,
&
error
);
IN_MASK
,
&
error
);
if
(
descriptor
<
0
)
{
if
(
descriptor
<
0
)
{
mpd_inotify_source_free
(
source
)
;
delete
source
;
g_warning
(
"%s"
,
error
->
message
);
g_warning
(
"%s"
,
error
->
message
);
g_error_free
(
error
);
g_error_free
(
error
);
return
2
;
return
2
;
...
@@ -90,6 +88,6 @@ int main(int argc, char **argv)
...
@@ -90,6 +88,6 @@ int main(int argc, char **argv)
event_loop
->
Run
();
event_loop
->
Run
();
mpd_inotify_source_free
(
source
)
;
delete
source
;
delete
event_loop
;
delete
event_loop
;
}
}
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