summaryrefslogtreecommitdiff
path: root/src/delete.c
blob: f432934278fff14c39eae061def40ba95027b588 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <dirent.h>

#include "cli_val.h"
#include "cli_objects.h"

extern char *cli_operation_name;

static void remove_rf(boolean do_umount)
{
  char *command = NULL;
  touch();
  if (do_umount) {
    command = malloc(strlen(get_mdirp()) + 20);
    sprintf(command, "sudo umount %s", get_mdirp());
    system(command);
  }

  command = realloc(command, strlen(m_path.path) + 10);
  sprintf(command, "rm -rf %s", m_path.path);
  system(command);

  if (do_umount) {
    command = realloc(command, strlen(get_mdirp()) + strlen(get_cdirp()) + 
		      strlen(get_mdirp()) + 100);
    sprintf(command, "sudo mount -t $UNIONFS -o dirs=%s=rw:%s=ro" 
	    " $UNIONFS %s", get_cdirp(), get_adirp(), get_mdirp());
    system(command);
  }
  free(command);
}

static boolean has_default(char **def, int size)
{
  char *buf_ptr;
  FILE *fp = fopen(t_path.path, "r");

  if (fp) {
    char buf[1025];
    while (fgets(buf, 1024, fp)) {
      if (strncmp(buf, "default:", 8) == 0) {
	buf_ptr = index(buf,':');
	if (buf_ptr == NULL) {
	  break;
	}

	//iterate up to non-whitespace character
	buf_ptr++;
	while (buf_ptr < (buf + size)) {
	  if (*buf_ptr == ' ') {
	    buf_ptr++;
	  }
	  else {
	    break;
	  }
	}

	if (size < strlen(buf_ptr)-1) {
	  bye("default buffer size is too small\n");
	}
	memcpy(*def, buf_ptr, strlen(buf_ptr)-1);
	fclose(fp);
	return 0;
      }
    }
    fclose(fp);
  }

  return 1;
}

static void reset_default(const char *def_val)
{
  if (def_val == NULL)
    return;

  //strip off quotes
  char tmp_val[1025];
  char *ptr = index(def_val,'"');
  if (ptr != NULL) {
    strcpy(tmp_val,ptr+1);
    ptr = rindex(tmp_val,'"');
    if (ptr != NULL) {
      *ptr = '\0';
    }
    else {
      strcpy(tmp_val,def_val); //go with original value.
    }
  }
  else {
    strcpy(tmp_val,def_val);
  }

  char filename[strlen(m_path.path) + 10];
  touch();
  sprintf(filename, "%s/node.val", m_path.path);

  FILE *fp = fopen(filename, "w");
  if (fp == NULL)
    bye("can not open: %s", filename);
  fputs(tmp_val, fp);
  fclose(fp);

  sprintf(filename, "%s/def", m_path.path);
  touch_file(filename);
}
/***************************************************
  set_validate:
    validate value against definition
    return TRUE if OK, FALSE otherwise
****************************************************/
boolean set_validate(vtw_def *defp, char *valp)
{  
  boolean res;
  int status;
  struct stat    statbuf;

  pop_path(&t_path); /* it was tag or real value */
  push_path(&t_path, DEF_NAME);
  if (lstat(t_path.path, &statbuf) < 0 || 
      (statbuf.st_mode & S_IFMT) != S_IFREG) {
    fprintf(out_stream, "The specified configuration node is not valid\n");
    bye("Can not set value (2), no definition for %s", m_path.path);
  }
  /* defniition present */
  memset(defp, 0, sizeof(vtw_def));
  if ((status = parse_def(defp, t_path.path, FALSE))) {
    bye("parse error: %d\n", status);
  }
  res = validate_value(defp, valp);
  pop_path(&t_path);
  return res;
}

int main(int argc, char **argv)
{
  int ai;
  struct stat    statbuf;
  vtw_def        def;
  boolean        last_tag=0;
  int            status;
  FILE          *fp;
  boolean        res;
  char          *cp, *delp, *endp;
  boolean        do_umount;

  /* this is needed before calling certain glib functions */
  g_type_init();

  cli_operation_name = "Delete";

  if (initialize_output() == -1) {
    bye("can't initialize output\n");
  }

  if (argc < 2) {
    fprintf(out_stream, "Need to specify the config node to delete\n");
    bye("nothing to delete\n");
  }

  dump_log( argc, argv);
  do_umount = FALSE;
  init_edit();
  /* extend both paths per arguments given */
  /* last argument is new value */
  for (ai = 1; ai < argc; ++ai) {
    push_path(&t_path, argv[ai]);
    push_path(&m_path, argv[ai]);
    if (lstat(t_path.path, &statbuf) >= 0) {
      if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
	bye("INTERNAL:regular file %s in templates", t_path.path);
      }
      last_tag = FALSE;
      continue;
    } /*else */
    pop_path(&t_path);
    push_path(&t_path, TAG_NAME);
    if (lstat(t_path.path, &statbuf) >= 0) {
      if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
	bye("INTERNAL:regular file %s in templates", t_path.path);
      }
      last_tag = TRUE;
      continue;
    } 
    /* no match */
    break;
  }
  /*
    cases:
    multiple tag-value - not  achild 
    mutilple tag-value - not the last child
    multiple tag-value - last child
    single value modified
    signle value unmodified
    multiple non-tag value - the last value
    multiple non-tag value - not the last value
    regular child 
  */
  if (ai == argc) {
    /* full path found */
    /* all cases except multiple non-tag value */
    /* check for single value */ 
    if (last_tag) {
      /* case of multiple tag-value 
	 was this a real child?
	 was it the last child?
      */
      struct dirent *dirp;
      DIR           *dp;

      if (lstat(m_path.path, &statbuf) < 0) {
        fprintf(out_stream, "Nothing to delete\n");
	bye("Nothing to delete at %s", m_path.path);
      }

      remove_rf(FALSE);
      pop_path(&m_path);
      if ((dp = opendir(m_path.path)) == NULL){
	INTERNAL;
      }
      while ((dirp = readdir(dp)) != NULL) {
	/*do we have real child */
	if (strcmp(dirp->d_name, ".") &&
	    strcmp(dirp->d_name, "..") &&
	    strcmp(dirp->d_name, MOD_NAME)  && /* XXX */
	    strncmp(dirp->d_name, ".wh.", 4) )
	  break; 
      }
      if (dirp == NULL) {
	/* no real children left */
	/* kill parent also */
	remove_rf(FALSE);
      }
      exit(0);
    }
    /*not tag */
    push_path(&t_path, DEF_NAME);
    if (lstat(t_path.path, &statbuf) >= 0 && 
	(statbuf.st_mode & S_IFMT) == S_IFREG) {
      /* defniition present */
      memset(&def, 0, sizeof(vtw_def));
      if ((status = parse_def(&def, t_path.path, FALSE))) {
	bye("parse error: %d\n", status);
      }
      if (!def.tag && !def.multi && def.def_type!= ERROR_TYPE) {
	/* signgle value */
	/* is it modified ==
	   it is in C, but not OPAQUE */
	switch_path(CPATH);
	if(lstat(m_path.path, &statbuf) >= 0) {
	  push_path(&m_path, OPQ_NAME);  
	  if(lstat(m_path.path, &statbuf) < 0) {	  
	    /* yes remove from C only */
	    pop_path(&m_path);
	    remove_rf(TRUE);
	    exit(0);
	  }
	  pop_path(&m_path); /*OPQ_NAME */	  
	} 
	switch_path(MPATH);
      }
    }
    /* else no defnition, remove it also */
    char *def_val;
    def_val = malloc(1025);
    if (has_default(&def_val,1024) == 0) {
      reset_default(def_val);
      free(def_val);
    }
    else {
      remove_rf(FALSE);
    }
    
	//    remove_rf(FALSE);
    exit(0);
  } 
  if(ai < argc -1 || last_tag) {
    fprintf(out_stream, "The specified configuration node is not valid\n");
    bye("There is no appropriate template for %s", 
	  m_path.path + strlen(get_mdirp()));
  }
  /*ai == argc -1, must be actual value */
  pop_path(&m_path); /*it was value, not path segment */
  push_path(&m_path, VAL_NAME);
  /* set value */
  if (lstat(m_path.path, &statbuf) < 0) {
    fprintf(out_stream, "Nothing to delete\n");
    bye("Nothing to delete at %s", m_path.path);
  }
  if ((statbuf.st_mode & S_IFMT) != S_IFREG)
    bye("Not a regular file %s", m_path.path);
  /* get definition to deal with potential multi */
  pop_path(&t_path); /* it was tag or real value */
  push_path(&t_path, DEF_NAME);
  if (lstat(t_path.path, &statbuf) < 0 || 
      (statbuf.st_mode & S_IFMT) != S_IFREG) {
    fprintf(out_stream, "The specified configuration node is not valid\n");
    bye("Can not delete value, no definition for %s", m_path.path);
  }
  /* defniition present */
  memset(&def, 0, sizeof(vtw_def));
  if ((status = parse_def(&def, t_path.path, FALSE))) {
    bye("parse error: %d\n", status);
  }
  if (def.multi) {
    /* delete from multivalue */
    valstruct new_value, old_value;
    status = char2val(&def, argv[argc - 1], &new_value);
    if (status)
      exit(0);
    cp = NULL;
    pop_path(&m_path); /* get_value will push VAL_NAME */
    status = get_value(&cp, &m_path);
    if (status != VTWERR_OK)
      bye("Cannot read old value %s\n", m_path.path);
    status = char2val(&def, cp, &old_value);
    if (status != VTWERR_OK)
      bye("Corrupted old value ---- \n%s\n-----\n", cp);
    res = val_cmp(&new_value, &old_value, IN_COND);
    if (!res) {
      fprintf(out_stream, "%s is not a configured value\n", new_value.val);
      bye("Not in multivalue");
    }
    touch();
    if (old_value.cnt) {
      push_path(&m_path, VAL_NAME);
      fp = fopen(m_path.path, "w");
      if (fp == NULL)
	bye("Can not open value file %s", m_path.path);
      if (is_in_cond_tik()) {
	for(delp=cp;delp && is_in_cond_tik(); dec_in_cond_tik()) {
	  delp = strchr(delp, '\n');
	  if (!delp)
	    INTERNAL;
	  ++delp; /* over \n */
	}
	/* write "left" of deleted */
	fwrite(cp, 1, delp-cp, fp);
      }else
	delp = cp;
      /* find end of value */
      endp = strchr(delp, '\n');
      if (endp && *++endp) {
	/* write "right" of deleted */
	fwrite(endp, 1, strlen(endp), fp);
        /* need the final '\n' */
        fwrite("\n", 1, 1, fp);
      }
      fclose(fp);
      return 0;
    }
    /* it multi with only 1 value, remove */
    remove_rf(FALSE);
    return 0;
  }

  /*
    let's do a new check here:
    -> if this is a leaf and there is a value look for a match of the value
    -> make sure to check existing configuration as well as uncommitted config
   */
  if (ai+1 == argc) {
    //does this work up until the last value
    pop_path(&m_path);
    if(lstat(m_path.path, &statbuf) == 0) {
      //now compare last value with that in the node.def file to determine whether to accept this delete
      status = get_value(&cp, &m_path);
      if (status != VTWERR_OK) {
	bye("Cannot read old value %s\n", m_path.path);
      }
      if (!strcmp(cp,argv[argc - 1])) {
	/* Also need to handle the case where the value is not specified. */
	char *def_val;
	def_val = malloc(1025);
	if (has_default(&def_val,1024) == 0) {
	  reset_default(def_val);
	  free(def_val);
	}
	else {
	  remove_rf(FALSE);
	}
	return 0;
      }
    }
  }


  fprintf(out_stream, "The specified configuration node is not valid\n");
  bye("There is no appropriate template for %s", 
      m_path.path + strlen(get_mdirp()));

  return 1;
}