summaryrefslogtreecommitdiff
path: root/node.c
blob: f11fa7b077329e4f1e94b54d815238e72a77a165 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* procfs -- a translator for providing GNU/Linux compatible 
             proc pseudo-filesystem

   node.c -- This file contains function defintions to handle
             node creation and destruction.
               
   Copyright (C) 2008, FSF.
   Written as a Summer of Code Project
   
   procfs 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.

   procfs 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 <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <hurd/ihash.h>
#include <hurd/fshelp.h>
#include <hurd/iohelp.h>

#include <hurd/netfs.h>

#include "procfs.h"

/* Return a new node in NODE, with a name NAME, and return the
   new node with a single reference in NODE. */
error_t procfs_create_node (struct procfs_dir_entry *dir_entry, 
                            const char *fs_path, struct node **node)
{
  struct node *new;
  struct netnode *nn = malloc (sizeof (struct netnode));
  error_t err;

  if (! nn)
    return ENOMEM;
  if (! fs_path)
    fs_path = strdup ("");
  nn->fs = dir_entry->dir->fs;
  nn->dir_entry = dir_entry;
  nn->dir = NULL;
  nn->fs_path = strdup (fs_path);

  new = netfs_make_node (nn);
  if (! new)
    {
      free (nn);
      return ENOMEM;
    }

  fshelp_touch (&new->nn_stat, TOUCH_ATIME|TOUCH_MTIME|TOUCH_CTIME,
		procfs_maptime);

  spin_lock (&nn->fs->inode_mappings_lock);
  err = hurd_ihash_add (&nn->fs->inode_mappings, dir_entry->stat.st_ino, dir_entry);
  spin_unlock (&nn->fs->inode_mappings_lock);

  if (err)
    {
      free (nn);
      free (new);
      return err;
    }
 
  dir_entry->node = new;
  *node = new;

  return 0;
}

/* Update the directory entry for NAME to reflect ST and SYMLINK_TARGET.
   True is returned if successful, or false if there was a memory allocation
   error.  TIMESTAMP is used to record the time of this update.  */
static void
update_entry (struct procfs_dir_entry *dir_entry, const struct stat *st,
	      const char *symlink_target, time_t timestamp)
{
  ino_t ino;
  struct procfs *fs = dir_entry->dir->fs;

  if (dir_entry->stat.st_ino)
    ino = dir_entry->stat.st_ino;
  else
    ino = fs->next_inode++;

  dir_entry->name_timestamp = timestamp;

  if (st)
    /* The ST and SYMLINK_TARGET parameters are only valid if ST isn't 0.  */
    {
      dir_entry->stat = *st;
      dir_entry->stat_timestamp = timestamp;

      if (!dir_entry->symlink_target || !symlink_target
	  || strcmp (dir_entry->symlink_target, symlink_target) != 0)
	{
	  if (dir_entry->symlink_target)
	    free (dir_entry->symlink_target);
	  dir_entry->symlink_target = symlink_target ? strdup (symlink_target) : 0;
	}
    }

  /* The st_ino field is always valid.  */
  dir_entry->stat.st_ino = ino;
  dir_entry->stat.st_fsid = fs->fsid;
  dir_entry->stat.st_fstype = PROCFILESYSTEM;
}

/* Refresh stat information for NODE */
error_t procfs_refresh_node (struct node *node)
{
  struct netnode *nn = node->nn;
  struct procfs_dir_entry *dir_entry = nn->dir_entry;

  if (! dir_entry)
    /* This is a deleted node, don't attempt to do anything.  */
    return 0;
  else
    {
      error_t err = 0;
      
      struct timeval tv;
      maptime_read (procfs_maptime, &tv); 
  
      time_t timestamp = tv.tv_sec;

      struct procfs_dir *dir = dir_entry->dir;

      mutex_lock (&dir->node->lock);

      if (! dir_entry->self_p)
	/* This is a deleted entry, just awaiting disposal; do so.  */
	{
#if 0
	  nn->dir_entry = 0;
	  free_entry (dir_entry);
	  return 0;
#endif
	}
      
      else if (dir_entry->noent)
	err = ENOENT;
      else 
        {
          if (*(dir_entry->name))
            {
              err =  procfs_dir_refresh (dir_entry->dir, 
              dir_entry->dir->node == dir_entry->dir->fs->root);
	      if (!err && dir_entry->noent)
	        err = ENOENT;

              if (err == ENOENT)
	        {
	          dir_entry->noent = 1; /* A negative entry.  */
	          dir_entry->name_timestamp = timestamp;
	        }
            }
          else
	    {
	      /* Refresh the root node with the old stat
                 information.  */             
              update_entry (dir_entry, &netfs_root_node->nn_stat, NULL, timestamp);     
	    }
	}

      node->nn_stat = dir_entry->stat;
      node->nn_translated = S_ISLNK (dir_entry->stat.st_mode) ? S_IFLNK : 0;
      if (!nn->dir && S_ISDIR (dir_entry->stat.st_mode))
	procfs_dir_create (nn->fs, node, nn->fs_path, &nn->dir);

      mutex_unlock (&dir->node->lock);

      return err;
    }
}

/* Remove NODE from its entry */
error_t procfs_remove_node (struct node *node)
{

    /* STUB */
    
  return 0;    
}