summaryrefslogtreecommitdiff
path: root/libcons/dir-changed.c
blob: 8498649ca6601eadc929b18adef21ee576eca955 (plain)
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
/* dir-changed.c - Handling dir changed notifications.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd 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 2, or (at
   your option) any later version.

   The GNU Hurd 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <errno.h>
#include <dirent.h>
#include <assert.h>
#include <mach.h>
#include <pthread.h>

#include "cons.h"
#include "fs_notify_S.h"


static error_t
add_one (cons_t cons, char *name)
{
  unsigned long int nr;
  char *tail;

  errno = 0;
  nr = strtoul (name, &tail, 10);
  if (!errno && *tail == '\0' && nr > 0)
    {
      vcons_list_t vcons_entry;
      return cons_lookup (cons, nr, 1, &vcons_entry);
    }
  return 0;
}

static error_t
lookup_one (cons_t cons, char *name, vcons_list_t *vcons_entry)
{
  unsigned long int nr;
  char *tail;

  errno = 0;
  nr = strtoul (name, &tail, 10);
  if (!errno && *tail == '\0' && nr > 0)
    return cons_lookup (cons, nr, 0, vcons_entry);
  return 0;
}


kern_return_t
cons_S_dir_changed (cons_notify_t notify, natural_t tickno,
		    dir_changed_type_t change, string_t name)
{
  error_t err;
  cons_t cons;

  if (!notify || !notify->cons)
    return EOPNOTSUPP;
  cons = notify->cons;

  pthread_mutex_lock (&cons->lock);

  switch (change)
    {
    case DIR_CHANGED_NULL:
      {
	DIR *dir = cons->dir;
	struct dirent *dent;
	do
	  {
	    errno = 0;
	    dent = readdir (dir);
	    if (!dent && errno)
	      err = errno;
	    else if (dent)
	      err = add_one (cons, dent->d_name);
	  }
	while (dent && !err);
	if (err)
	  assert ("Unexpected error");	/* XXX */
      }
      break;
    case DIR_CHANGED_NEW:
      {
	err = add_one (cons, name);
	if (err)
	  assert ("Unexpected error");	/* XXX */
      }
      break;
    case DIR_CHANGED_UNLINK:
      {
	vcons_list_t vcons_entry;
	err = lookup_one (cons, name, &vcons_entry);
	if (!err)
	  {
	    cons_vcons_remove (cons, vcons_entry);
	    if (vcons_entry->prev)
	      vcons_entry->prev->next = vcons_entry->next;
	    else
	      cons->vcons_list = vcons_entry->next;
	    if (vcons_entry->next)
	      vcons_entry->next->prev = vcons_entry->prev;
	    else
	      cons->vcons_last = vcons_entry->prev;

	    free (vcons_entry);
	  }
      }
      break;
    case DIR_CHANGED_RENUMBER:
    default:
      assert ("Unexpected dir-changed type.");
      pthread_mutex_unlock (&cons->lock);
      return EINVAL;
    }
  pthread_mutex_unlock (&cons->lock);
  return 0;
}