9cb15cd28581d72ad47bbf064f4f793b9858476e
[ira/wip.git] / source4 / lib / registry / reg_backend_dir / reg_backend_dir.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij                                          2004.
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/common/registry.h"
23
24 static BOOL reg_dir_add_key(REG_KEY *parent, const char *name)
25 {
26         char *path;
27         int ret;
28         asprintf(&path, "%s%s\\%s", parent->handle->location, reg_key_get_path(parent), name);
29         path = reg_path_win2unix(path);
30         ret = mkdir(path, 0700);
31         SAFE_FREE(path);
32         return (ret == 0);
33 }
34
35 static BOOL reg_dir_del_key(REG_KEY *k)
36 {
37         return (rmdir((char *)k->backend_data) == 0);
38 }
39
40 static REG_KEY *reg_dir_open_key(REG_HANDLE *h, const char *name)
41 {
42         DIR *d;
43         char *fullpath;
44         REG_KEY *ret;
45         TALLOC_CTX *mem_ctx = talloc_init("tmp");
46         if(!name) {
47                 DEBUG(0, ("NULL pointer passed as directory name!"));
48                 return NULL;
49         }
50         fullpath = talloc_asprintf(mem_ctx, "%s%s", h->location, name);
51         fullpath = reg_path_win2unix(fullpath);
52         
53         d = opendir(fullpath);
54         if(!d) {
55                 DEBUG(3,("Unable to open '%s': %s\n", fullpath, strerror(errno)));
56                 talloc_destroy(mem_ctx);
57                 return NULL;
58         }
59         closedir(d);
60         ret = reg_key_new_abs(name, h, fullpath);
61         talloc_steal(mem_ctx, ret->mem_ctx, fullpath);
62         talloc_destroy(mem_ctx);
63         return ret;
64 }
65
66 static BOOL reg_dir_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***r)
67 {
68         struct dirent *e;
69         int max = 200;
70         char *fullpath = k->backend_data;
71         REG_KEY **ar;
72         DIR *d;
73         (*count) = 0;
74         ar = talloc(k->mem_ctx, sizeof(REG_KEY *) * max);
75
76         d = opendir(fullpath);
77
78         if(!d) return False;
79         
80         while((e = readdir(d))) {
81                 if(e->d_type == DT_DIR && 
82                    strcmp(e->d_name, ".") &&
83                    strcmp(e->d_name, "..")) {
84                         char *newfullpath;
85                         ar[(*count)] = reg_key_new_rel(e->d_name, k, NULL);
86                         ar[(*count)]->backend_data = talloc_asprintf(ar[*count]->mem_ctx, "%s/%s", fullpath, e->d_name);
87                         if(ar[(*count)])(*count)++;
88
89                         if((*count) == max) {
90                                 max+=200;
91                                 ar = realloc(ar, sizeof(REG_KEY *) * max);
92                         }
93                 }
94         }
95
96         closedir(d);
97         
98         *r = ar;
99         return True;
100 }
101
102 static BOOL reg_dir_open(REG_HANDLE *h, const char *loc, BOOL try) {
103         if(!loc) return False;
104         return True;
105 }
106
107 static REG_VAL *reg_dir_add_value(REG_KEY *p, const char *name, int type, void *data, int len)
108 {
109         REG_VAL *ret = reg_val_new(p, NULL);
110         char *fullpath;
111         FILE *fd;
112         ret->name = name?talloc_strdup(ret->mem_ctx, name):NULL;
113         fullpath = reg_path_win2unix(strdup(reg_val_get_path(ret)));
114         
115         fd = fopen(fullpath, "w+");
116         
117         /* FIXME */
118         return NULL;
119 }
120
121 static BOOL reg_dir_del_value(REG_VAL *v)
122 {
123         /* FIXME*/
124         return False;
125 }
126
127 static REG_OPS reg_backend_dir = {
128         .name = "dir",
129         .open_registry = reg_dir_open,
130         .open_key = reg_dir_open_key,
131         .fetch_subkeys = reg_dir_fetch_subkeys,
132         .add_key = reg_dir_add_key,
133         .del_key = reg_dir_del_key,
134         .add_value = reg_dir_add_value,
135         .del_value = reg_dir_del_value,
136 };
137
138 NTSTATUS reg_dir_init(void)
139 {
140         return register_backend("registry", &reg_backend_dir);
141 }