summaryrefslogtreecommitdiffstats
path: root/xlators/meta/src/tree.c
blob: 62faad19ed8d9ef6bfa4df8c9c6c0adb1ae0f7da (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
/*
   Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS 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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#include "glusterfs.h"
#include "xlator.h"

#include "meta.h"
#include "meta-mem-types.h"

static int
is_meta_path (const char *path)
{
  while (*path == '/')
    path++;
  if (!strncmp (path, ".meta", strlen (".meta")))
    return 1;
  return 0;
}

struct stat *
new_stbuf (void)
{
  static int next_inode = 0;
  struct stat *stbuf = GF_CALLOC (1, sizeof (struct stat), gf_meta_mt_stat);
  
  ERR_ABORT (stbuf);

  stbuf->st_dev = 0;
  stbuf->st_ino = next_inode++;
  stbuf->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
  stbuf->st_nlink = 1;
  stbuf->st_uid = 0;
  stbuf->st_gid = 0;
  stbuf->st_rdev = 0;
  stbuf->st_size = 0;
  stbuf->st_blksize = 0;
  stbuf->st_blocks = 0;
  stbuf->st_atime = time (NULL);
  stbuf->st_atim.tv_nsec = 0;
  stbuf->st_mtime = stbuf->st_atime;
  stbuf->st_mtim.tv_nsec = 0;
  stbuf->st_ctime = stbuf->st_ctime;
  stbuf->st_ctim.tv_nsec = 0;

  return stbuf;
}

/* find an entry among the siblings of an entry */
static meta_dirent_t *
find_entry (meta_dirent_t *node, const char *dir)
{
  meta_dirent_t *trav = node;
  while (trav) {
    if (!strcmp (trav->name, dir))
      return trav;
    trav = trav->next;
  }
  return NULL;
}

/*
 * Return the meta_dirent_t corresponding to the pathname.
 *
 * If pathname does not exist in the meta tree, try to return
 * its highest parent that does exist. The part of the
 * pathname that is left over is returned in the value-result
 * variable {remain}.
 * For example, for "/.meta/xlators/brick1/view/foo/bar/baz",
 * return the entry for "/.meta/xlators/brick1/view"
 * and set remain to "/bar/baz"
 */

meta_dirent_t *
lookup_meta_entry (meta_dirent_t *root, const char *path,
		   char **remain)
{
  char *_path = gf_strdup (path);

  if (!is_meta_path (path))
    return NULL;

  meta_dirent_t *trav = root;
  char *dir = strtok (_path, "/");
  dir = strtok (NULL, "/");

  while (dir) {
    meta_dirent_t *ntrav;
    ntrav = find_entry (trav->children, dir);
    if (!ntrav) {
      /* we have reached bottom of the meta tree. 
         Unknown dragons lie further below */
      if (remain) {
	char *piece = dir;
	while (piece) {
	  char *tmp = *remain;
	  if (*remain)
	    gf_asprintf (remain, "/%s/%s", *remain, piece);
	  else
	    gf_asprintf (remain, "/%s", piece);
	  if (tmp) GF_FREE (tmp);
	  piece = strtok (NULL, "/");
	}
      }
      return trav;
    }
    dir = strtok (NULL, "/");
    trav = ntrav;
  }

  GF_FREE (_path);
  return trav;
}

meta_dirent_t *
insert_meta_entry (meta_dirent_t *root, const char *path,
		   int type, struct stat *stbuf, struct xlator_fops *fops)
{
  if (!is_meta_path (path))
    return NULL;
  char *slashpos = strrchr (path, '/');
  char *dir = strndup (path, slashpos - path);
  meta_dirent_t *parent = lookup_meta_entry (root, dir, NULL);
  if (!dir)
    return NULL;

  meta_dirent_t *new = GF_CALLOC (1, sizeof (meta_dirent_t),
                                  gf_meta_mt_meta_dirent_t);
  ERR_ABORT (new);
  new->name        = gf_strdup (slashpos+1);
  new->type        = type;
  new->parent      = parent;
  new->next        = parent->children;
  parent->children = new;
  if (stbuf)
    new->stbuf     = stbuf;
  else 
    new->stbuf     = new_stbuf ();

  new->stbuf->st_mode |= type;
  new->fops        = fops;
  return new;
}

int main (void)
{
  meta_dirent_t *root = GF_CALLOC (1, sizeof (meta_dirent_t),
                                   gf_meta_mt_meta_dirent_t);
  ERR_ABORT (root);
  root->name = gf_strdup (".meta");

  insert_meta_entry (root, "/.meta/version", S_IFREG, NULL, NULL);
  return 0;
}