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

Proposed by James Hunt
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 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

* 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
=== modified file 'nih/tests/test_watch.c'
--- nih/tests/test_watch.c 2009-11-28 19:56:46 +0000
+++ nih/tests/test_watch.c 2011-06-20 16:24:34 +0000
@@ -946,13 +946,82 @@
946 nih_error_init ();946 nih_error_init ();
947947
948 TEST_FILENAME (dirname);948 TEST_FILENAME (dirname);
949 mkdir (dirname, 0755);949 TEST_EQ (mkdir (dirname, 0755), 0);
950
951 TEST_FEATURE ("with watched file");
952 strcpy (filename, dirname);
953 strcat (filename, "/foo");
954
955 /* Create file first since we don't set a create handler on the
956 * watch.
957 */
958 fd = fopen (filename, "w");
959 fprintf (fd, "bar\n");
960 fclose (fd);
961
962 create_called = 0;
963 modify_called = 0;
964 delete_called = 0;
965 logger_called = 0;
966 last_path = NULL;
967 last_watch = NULL;
968 last_data = NULL;
969
970 watch = nih_watch_new (NULL, filename, FALSE, FALSE, NULL,
971 NULL, my_modify_handler,
972 my_delete_handler, &watch);
973 TEST_NE_P (watch, NULL);
974
975 /* Now, modify the existing file to trigger the modify handler. */
976 fd = fopen (filename, "a+");
977 fprintf (fd, "baz\n");
978 fclose (fd);
979
980 nfds = 0;
981 FD_ZERO (&readfds);
982 FD_ZERO (&writefds);
983 FD_ZERO (&exceptfds);
984
985 nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
986 select (nfds, &readfds, &writefds, &exceptfds, NULL);
987 nih_io_handle_fds (&readfds, &writefds, &exceptfds);
988
989 TEST_EQ_STR (watch->path, filename);
990
991 /* Ensure no regression to old behaviour (LP:#777097) */
992 TEST_NE (last_path[ strlen(last_path) - 1 ], '/');
993
994 TEST_EQ_STR (last_path, filename);
995 TEST_EQ (modify_called, 1);
996
997 unlink (filename);
998
999 nfds = 0;
1000 FD_ZERO (&readfds);
1001 FD_ZERO (&writefds);
1002 FD_ZERO (&exceptfds);
1003
1004 nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
1005 select (nfds, &readfds, &writefds, &exceptfds, NULL);
1006 nih_io_handle_fds (&readfds, &writefds, &exceptfds);
1007
1008 TEST_EQ (delete_called, 1);
1009
1010 rmdir (filename);
1011 nih_free (last_path);
1012
1013 create_called = 0;
1014 modify_called = 0;
1015 delete_called = 0;
1016 logger_called = 0;
1017 last_path = NULL;
1018 last_watch = NULL;
1019 last_data = NULL;
1020
9501021
951 watch = nih_watch_new (NULL, dirname, TRUE, TRUE, my_filter,1022 watch = nih_watch_new (NULL, dirname, TRUE, TRUE, my_filter,
952 my_create_handler, my_modify_handler,1023 my_create_handler, my_modify_handler,
953 my_delete_handler, &watch);1024 my_delete_handler, &watch);
954
955
956 /* Check that creating a file within the directory being watched1025 /* Check that creating a file within the directory being watched
957 * results in the create handler being called, and passed the full1026 * results in the create handler being called, and passed the full
958 * path of the created file to it.1027 * path of the created file to it.
9591028
=== modified file 'nih/watch.c'
--- nih/watch.c 2011-01-06 09:28:39 +0000
+++ nih/watch.c 2011-06-20 16:24:34 +0000
@@ -494,12 +494,21 @@
494 return;494 return;
495 }495 }
496496
497497 /* Every other event must come with a name */
498 /* Every other event must come with a name. */498 if (name && *name) {
499 if ((! name) || strchr (name, '/'))499
500 return;500 /* If name refers to a directory, there should be no associated
501501 * path - just the name of the path element.
502 path = NIH_MUST (nih_sprintf (NULL, "%s/%s", handle->path, name));502 */
503 if (strchr (name, '/'))
504 return;
505
506 /* Event occured for file within a watched directory */
507 path = NIH_MUST (nih_sprintf (NULL, "%s/%s", handle->path, name));
508 } else {
509 /* File event occured */
510 path = NIH_MUST (nih_strdup (NULL, handle->path));
511 }
503512
504 /* Check the filter */513 /* Check the filter */
505 if (watch->filter && watch->filter (watch->data, path,514 if (watch->filter && watch->filter (watch->data, path,

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: