Merge lp:~jamesodhunt/libnih/fix-for-777097 into lp:libnih/1.0

Proposed by James Hunt on 2011-06-20
Status: Needs review
Proposed branch: lp:~jamesodhunt/libnih/fix-for-777097
Merge into: lp:libnih/1.0
Diff against target: 121 lines (+87/-9)
2 files modified
nih/tests/test_watch.c (+72/-3)
nih/watch.c (+15/-6)
To merge this branch: bzr merge lp:~jamesodhunt/libnih/fix-for-777097
Reviewer Review Type Date Requested Status
Scott James Remnant 2011-06-20 Pending
Review via email: mp+65228@code.launchpad.net

Description of the change

  * nih/watch.c:nih_watch_handle(): Handle non-directory watches;
    previously a file watch resulted in an invalid file path ending in
    a single slash (LP:#777097).
  * nih/tests/test_watch.c: Added explicit test for watch on a file.

To post a comment you must log in.

Unmerged revisions

1049. By James Hunt on 2011-06-20

* nih/watch.c:nih_watch_handle(): Handle non-directory watches;
  previously a file watch resulted in an invalid file path ending in
  a single slash (LP:#777097).
* nih/tests/test_watch.c: Added explicit test for watch on a file.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nih/tests/test_watch.c'
2--- nih/tests/test_watch.c 2009-11-28 19:56:46 +0000
3+++ nih/tests/test_watch.c 2011-06-20 16:24:34 +0000
4@@ -946,13 +946,82 @@
5 nih_error_init ();
6
7 TEST_FILENAME (dirname);
8- mkdir (dirname, 0755);
9+ TEST_EQ (mkdir (dirname, 0755), 0);
10+
11+ TEST_FEATURE ("with watched file");
12+ strcpy (filename, dirname);
13+ strcat (filename, "/foo");
14+
15+ /* Create file first since we don't set a create handler on the
16+ * watch.
17+ */
18+ fd = fopen (filename, "w");
19+ fprintf (fd, "bar\n");
20+ fclose (fd);
21+
22+ create_called = 0;
23+ modify_called = 0;
24+ delete_called = 0;
25+ logger_called = 0;
26+ last_path = NULL;
27+ last_watch = NULL;
28+ last_data = NULL;
29+
30+ watch = nih_watch_new (NULL, filename, FALSE, FALSE, NULL,
31+ NULL, my_modify_handler,
32+ my_delete_handler, &watch);
33+ TEST_NE_P (watch, NULL);
34+
35+ /* Now, modify the existing file to trigger the modify handler. */
36+ fd = fopen (filename, "a+");
37+ fprintf (fd, "baz\n");
38+ fclose (fd);
39+
40+ nfds = 0;
41+ FD_ZERO (&readfds);
42+ FD_ZERO (&writefds);
43+ FD_ZERO (&exceptfds);
44+
45+ nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
46+ select (nfds, &readfds, &writefds, &exceptfds, NULL);
47+ nih_io_handle_fds (&readfds, &writefds, &exceptfds);
48+
49+ TEST_EQ_STR (watch->path, filename);
50+
51+ /* Ensure no regression to old behaviour (LP:#777097) */
52+ TEST_NE (last_path[ strlen(last_path) - 1 ], '/');
53+
54+ TEST_EQ_STR (last_path, filename);
55+ TEST_EQ (modify_called, 1);
56+
57+ unlink (filename);
58+
59+ nfds = 0;
60+ FD_ZERO (&readfds);
61+ FD_ZERO (&writefds);
62+ FD_ZERO (&exceptfds);
63+
64+ nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
65+ select (nfds, &readfds, &writefds, &exceptfds, NULL);
66+ nih_io_handle_fds (&readfds, &writefds, &exceptfds);
67+
68+ TEST_EQ (delete_called, 1);
69+
70+ rmdir (filename);
71+ nih_free (last_path);
72+
73+ create_called = 0;
74+ modify_called = 0;
75+ delete_called = 0;
76+ logger_called = 0;
77+ last_path = NULL;
78+ last_watch = NULL;
79+ last_data = NULL;
80+
81
82 watch = nih_watch_new (NULL, dirname, TRUE, TRUE, my_filter,
83 my_create_handler, my_modify_handler,
84 my_delete_handler, &watch);
85-
86-
87 /* Check that creating a file within the directory being watched
88 * results in the create handler being called, and passed the full
89 * path of the created file to it.
90
91=== modified file 'nih/watch.c'
92--- nih/watch.c 2011-01-06 09:28:39 +0000
93+++ nih/watch.c 2011-06-20 16:24:34 +0000
94@@ -494,12 +494,21 @@
95 return;
96 }
97
98-
99- /* Every other event must come with a name. */
100- if ((! name) || strchr (name, '/'))
101- return;
102-
103- path = NIH_MUST (nih_sprintf (NULL, "%s/%s", handle->path, name));
104+ /* Every other event must come with a name */
105+ if (name && *name) {
106+
107+ /* If name refers to a directory, there should be no associated
108+ * path - just the name of the path element.
109+ */
110+ if (strchr (name, '/'))
111+ return;
112+
113+ /* Event occured for file within a watched directory */
114+ path = NIH_MUST (nih_sprintf (NULL, "%s/%s", handle->path, name));
115+ } else {
116+ /* File event occured */
117+ path = NIH_MUST (nih_strdup (NULL, handle->path));
118+ }
119
120 /* Check the filter */
121 if (watch->filter && watch->filter (watch->data, path,

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: