]> andersk Git - moira.git/blame - clients/moira/attach.c
second code style cleanup: void/void * usage, proper #includes. try to
[moira.git] / clients / moira / attach.c
CommitLineData
7ac48069 1/* $Id $
2 *
3 * This is the file attach.c for the Moira Client, which allows users
59ec8dae 4 * to quickly and easily maintain most parts of the Moira database.
5eaef520 5 * It Contains: Functions for maintaining data used by Hesiod
6 * to map courses/projects/users to their file systems,
7 * and maintain filesys info.
8 *
08345b74 9 * Created: 5/4/88
10 * By: Chris D. Peterson
08345b74 11 *
7ac48069 12 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
13 * For copying and distribution information, please see the file
14 * <mit-copyright.h>.
08345b74 15 */
16
7ac48069 17#include <mit-copyright.h>
8defc06b 18#include <moira.h>
19#include <moira_site.h>
08345b74 20
0a2c64cb 21#include "defs.h"
22#include "f_defs.h"
461c03b6 23#include "globals.h"
461c03b6 24
7ac48069 25#include <sys/stat.h>
26
27#include <ctype.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
33RCSID("$Header$");
34
35char *canonicalize_cell(char *c);
36int GetAliasValue(int argc, char **argv, void *retval);
37void RealDeleteFS(char **info, Bool one_item);
38char *SortAfter(struct qelem *elem, int count);
39void RealDeleteFSAlias(char **info, Bool one_item);
40
08345b74 41#define FS_ALIAS_TYPE "FILESYS"
42
461c03b6 43#define LABEL 0
44#define MACHINE 1
45#define GROUP 2
46#define ALIAS 3
08345b74 47
6966e6fe 48#define NO_MACHINE ("[NONE]")
9e2516d6 49
1104552d 50#define DEFAULT_TYPE ("AFS")
85ca828a 51#define DEFAULT_MACHINE DEFAULT_NONE
52#define DEFAULT_PACK DEFAULT_NONE
7f6743f7 53#define DEFAULT_ACCESS ("w")
85ca828a 54#define DEFAULT_COMMENTS DEFAULT_COMMENT
55#define DEFAULT_OWNER (user)
56#define DEFAULT_OWNERS (user)
57#define DEFAULT_CREATE DEFAULT_YES
7f6743f7 58#define DEFAULT_L_TYPE ("PROJECT")
7cb7d352 59#define DEFAULT_CELL ("athena.mit.edu")
85ca828a 60
61/* Function Name: SetDefaults
62 * Description: sets the default values for filesystem additions.
5eaef520 63 * Arguments: info - an array of char pointers to recieve defaults.
85ca828a 64 * Returns: char ** (this array, now filled).
65 */
66
5eaef520 67static char **SetDefaults(char **info, char *name)
85ca828a 68{
5eaef520 69 char buf[BUFSIZ];
70
7ac48069 71 info[FS_NAME] = strdup(name);
72 info[FS_TYPE] = strdup(DEFAULT_TYPE);
73 info[FS_MACHINE] = strdup(DEFAULT_MACHINE);
74 info[FS_PACK] = strdup(DEFAULT_PACK);
5eaef520 75 sprintf(buf, "/mit/%s", name);
7ac48069 76 info[FS_M_POINT] = strdup(buf);
77 info[FS_ACCESS] = strdup(DEFAULT_ACCESS);
78 info[FS_COMMENTS] = strdup(DEFAULT_COMMENTS);
79 info[FS_OWNER] = strdup(DEFAULT_OWNER);
80 info[FS_OWNERS] = strdup(DEFAULT_OWNERS);
81 info[FS_CREATE] = strdup(DEFAULT_CREATE);
82 info[FS_L_TYPE] = strdup(DEFAULT_L_TYPE);
5eaef520 83 info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
84 return info;
85ca828a 85}
86
08345b74 87/* Function Name: GetFSInfo
88 * Description: Stores the info in a queue.
89 * Arguments: type - type of information to get.
90 * name - name of the item to get information on.
91 * Returns: a pointer to the first element in the queue.
92 */
93
5eaef520 94static struct qelem *GetFSInfo(int type, char *name)
08345b74 95{
5eaef520 96 int stat;
97 struct qelem *elem = NULL;
98 char *args[5];
08345b74 99
5eaef520 100 switch (type)
101 {
461c03b6 102 case LABEL:
5eaef520 103 if ((stat = do_mr_query("get_filesys_by_label", 1, &name,
7ac48069 104 StoreInfo, &elem)))
5eaef520 105 {
106 com_err(program_name, stat, " in GetFSInfo");
107 return NULL;
08345b74 108 }
5eaef520 109 break;
461c03b6 110 case MACHINE:
5eaef520 111 if ((stat = do_mr_query("get_filesys_by_machine", 1, &name,
7ac48069 112 StoreInfo, &elem)))
5eaef520 113 {
114 com_err(program_name, stat, " in GetFSInfo");
115 return NULL;
08345b74 116 }
5eaef520 117 break;
461c03b6 118 case GROUP:
5eaef520 119 if ((stat = do_mr_query("get_filesys_by_group", 1, &name,
7ac48069 120 StoreInfo, &elem)))
5eaef520 121 {
122 com_err(program_name, stat, " in GetFSInfo");
123 return NULL;
08345b74 124 }
5eaef520 125 break;
461c03b6 126 case ALIAS:
5eaef520 127 args[ALIAS_NAME] = name;
128 args[ALIAS_TYPE] = FS_ALIAS_TYPE;
129 args[ALIAS_TRANS] = "*";
7ac48069 130 if ((stat = do_mr_query("get_alias", 3, args, StoreInfo, &elem)))
5eaef520 131 {
132 com_err(program_name, stat, " in get_alias.");
133 return NULL;
08345b74 134 }
135 }
136
5eaef520 137 return QueueTop(elem);
08345b74 138}
139
85ca828a 140/* Function Name: PrintFSAlias
141 * Description: Prints a filesystem alias
142 * Arguments: info - an array contains the strings of info.
402461ad 143 * Returns: the name of the filesys - used be QueryLoop().
85ca828a 144 */
145
5eaef520 146static char *PrintFSAlias(char **info)
85ca828a 147{
5eaef520 148 char buf[BUFSIZ];
85ca828a 149
5eaef520 150 sprintf(buf, "Alias: %-25s Filesystem: %s", info[ALIAS_NAME],
151 info[ALIAS_TRANS]);
152 Put_message(buf);
153 return info[ALIAS_NAME];
85ca828a 154}
155
9e2516d6 156static int fsgCount = 1;
157
7ac48069 158static void PrintFSGMembers(char **info)
9e2516d6 159{
5eaef520 160 char print_buf[BUFSIZ];
9e2516d6 161
5eaef520 162 sprintf(print_buf, " %d. Filesystem: %-32s (sort key: %s)",
163 fsgCount++, info[0], info[1]);
164 Put_message(print_buf);
9e2516d6 165}
166
167
08345b74 168/* Function Name: PrintFSInfo
169 * Description: Prints the filesystem information.
170 * Arguments: info - a pointer to the filesystem information.
171 * Returns: none.
172 */
173
5eaef520 174static char *PrintFSInfo(char **info)
08345b74 175{
5eaef520 176 char print_buf[BUFSIZ];
177
178 FORMFEED;
179
180 if (!strcmp(info[FS_TYPE], "FSGROUP") || !strcmp(info[FS_TYPE], "MUL"))
181 {
182 int stat;
183 struct qelem *elem = NULL;
184
185 if (!strcmp(info[FS_TYPE], "MUL"))
186 sprintf(print_buf, "%20s Multiple Filesystem: %s", " ", info[FS_NAME]);
187 else
188 sprintf(print_buf, "%20s Filesystem Group: %s", " ", info[FS_NAME]);
189 Put_message(print_buf);
190
191 sprintf(print_buf, "Comments: %s", info[FS_COMMENTS]);
192 Put_message(print_buf);
193 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
194 info[FS_MODWITH]);
195 Put_message(print_buf);
196 Put_message("Containing the filesystems (in order):");
197 if ((stat = do_mr_query("get_fsgroup_members", 1, &info[FS_NAME],
7ac48069 198 StoreInfo, &elem)))
5eaef520 199 {
200 if (stat == MR_NO_MATCH)
201 Put_message(" [no members]");
202 else
203 com_err(program_name, stat, " in PrintFSInfo");
204 }
205 else
206 {
207 fsgCount = 1;
208 Loop(QueueTop(elem), PrintFSGMembers);
209 FreeQueue(elem);
9e2516d6 210 }
9e2516d6 211 }
5eaef520 212 else
213 {
214 sprintf(print_buf, "%20s Filesystem: %s", " ", info[FS_NAME]);
215 Put_message(print_buf);
216 sprintf(print_buf, "Type: %-40s Machine: %-15s",
217 info[FS_TYPE], info[FS_MACHINE]);
218 Put_message(print_buf);
219 sprintf(print_buf, "Default Access: %-2s Packname: %-17s Mountpoint %s ",
220 info[FS_ACCESS], info[FS_PACK], info[FS_M_POINT]);
221 Put_message(print_buf);
222 sprintf(print_buf, "Comments: %s", info[FS_COMMENTS]);
223 Put_message(print_buf);
224 sprintf(print_buf, "User Ownership: %-30s Group Ownership: %s",
225 info[FS_OWNER], info[FS_OWNERS]);
226 Put_message(print_buf);
227 sprintf(print_buf, "Update Fileserver: %-27s Locker Type: %s",
228 atoi(info[FS_CREATE]) ? "ON" : "OFF", info[FS_L_TYPE]);
229 Put_message(print_buf);
230 sprintf(print_buf, MOD_FORMAT, info[FS_MODBY], info[FS_MODTIME],
231 info[FS_MODWITH]);
232 Put_message(print_buf);
233 }
234 return info[FS_NAME];
08345b74 235}
236
7cb7d352 237
5eaef520 238char *canonicalize_cell(char *c)
7cb7d352 239{
5eaef520 240 struct stat stbuf;
241 char path[512];
242 int count;
243
244 sprintf(path, "/afs/%s", c);
245 if (lstat(path, &stbuf) || !stbuf.st_mode & S_IFLNK)
246 return c;
247 count = readlink(path, path, sizeof(path));
248 if (count < 1)
249 return c;
250 path[count] = 0;
251 free(c);
7ac48069 252 return strdup(path);
7cb7d352 253}
254
255
7ac48069 256int GetAliasValue(int argc, char **argv, void *retval)
7cb7d352 257{
7ac48069 258 *(char **)retval = strdup(argv[2]);
5eaef520 259 return MR_CONT;
7cb7d352 260}
261
08345b74 262/* Function Name: AskFSInfo.
5eaef520 263 * Description: This function askes the user for information about a
08345b74 264 * machine and saves it into a structure.
265 * Arguments: info - a pointer the the structure to put the
266 * info into.
267 * name - add a newname field? (T/F)
268 * Returns: none.
269 */
270
5eaef520 271static char **AskFSInfo(char **info, Bool name)
08345b74 272{
5eaef520 273 char temp_buf[BUFSIZ], *newname, access_type[32];
274 int fsgroup = 0, newdefaults = 0;
275
276 Put_message("");
277 sprintf(temp_buf, "Changing Attributes of filesystem %s.", info[FS_NAME]);
278 Put_message(temp_buf);
279 Put_message("");
280
281 if (name)
282 {
7ac48069 283 newname = strdup(info[FS_NAME]);
5eaef520 284 if (GetValueFromUser("The new name for this filesystem",
285 &newname) == SUB_ERROR)
286 return NULL;
08345b74 287 }
288
5eaef520 289 strcpy(temp_buf, info[FS_TYPE]);
290 if (GetTypeFromUser("Filesystem's Type", "filesys", &info[FS_TYPE]) ==
291 SUB_ERROR)
292 return NULL;
293 if (!strcasecmp(info[FS_TYPE], "FSGROUP") ||
294 !strcasecmp(info[FS_TYPE], "MUL"))
295 fsgroup++;
296 if (strcasecmp(info[FS_TYPE], temp_buf))
297 newdefaults++;
298 if (fsgroup)
299 {
300 free(info[FS_MACHINE]);
7ac48069 301 info[FS_MACHINE] = strdup(NO_MACHINE);
5eaef520 302 }
303 else
304 {
305 if (!strcasecmp(info[FS_TYPE], "AFS"))
306 {
307 if (!name || newdefaults)
308 {
309 free(info[FS_MACHINE]);
7ac48069 310 info[FS_MACHINE] = strdup(DEFAULT_CELL);
7cb7d352 311 }
5eaef520 312 if (GetValueFromUser("Filesystem's Cell", &info[FS_MACHINE]) ==
7cb7d352 313 SUB_ERROR)
5eaef520 314 return NULL;
315 info[FS_MACHINE] = canonicalize_cell(info[FS_MACHINE]);
316 }
317 else
318 {
319 if (GetValueFromUser("Filesystem's Machine", &info[FS_MACHINE]) ==
320 SUB_ERROR)
321 return NULL;
322 info[FS_MACHINE] = canonicalize_hostname(info[FS_MACHINE]);
7cb7d352 323 }
9e2516d6 324 }
5eaef520 325 if (!fsgroup)
326 {
327 if (!strcasecmp(info[FS_TYPE], "AFS"))
328 {
329 char *path, *args[3], *p;
330 int status, depth;
331 if (GetTypeFromUser("Filesystem's lockertype", "lockertype",
332 &info[FS_L_TYPE]) == SUB_ERROR)
333 return NULL;
334 if (!name || newdefaults)
335 {
336 free(info[FS_PACK]);
337 lowercase(info[FS_MACHINE]);
338 sprintf(temp_buf, "%s:%s", info[FS_MACHINE], info[FS_L_TYPE]);
339 args[0] = temp_buf;
340 args[1] = "AFSPATH";
341 args[2] = "*";
342 path = "???";
343 status = do_mr_query("get_alias", 3, args, GetAliasValue, &path);
344 if (status == MR_SUCCESS)
345 {
346 p = strchr(path, ':');
347 if (p)
348 {
349 *p = '\0';
350 depth = atoi(++p);
351 }
352 else
353 depth = 0;
354 sprintf(temp_buf, "/afs/%s/%s", info[FS_MACHINE], path);
355 if (depth >= 0)
356 {
357 for (p = info[FS_NAME];
358 *p && (p - info[FS_NAME]) < depth; p++)
359 {
360 if (islower(*p))
361 {
362 strcat(temp_buf, "/x");
363 temp_buf[strlen(temp_buf) - 1] = *p;
364 }
365 else
366 {
367 sprintf(temp_buf, "/afs/%s/%s/other",
368 info[FS_MACHINE], path);
369 break;
d678d80c 370 }
b7e66e09 371 }
5eaef520 372 }
373 else if (depth == -1)
374 {
375 if (isdigit(info[FS_NAME][0]))
376 {
377 strcat(temp_buf, "/");
378 depth = strlen(temp_buf);
379 for (p = info[FS_NAME]; *p && isdigit(*p); p++)
380 {
381 temp_buf[depth++] = *p;
382 temp_buf[depth] = 0;
b7e66e09 383 }
5eaef520 384 }
385 else
386 strcat(temp_buf, "/other");
387 }
388 else
389 {
b7e66e09 390 /* no default */
7cb7d352 391 }
5eaef520 392 strcat(temp_buf, "/");
393 strcat(temp_buf, info[FS_NAME]);
394 free(path);
7cb7d352 395 }
5eaef520 396 else
397 {
398 sprintf(temp_buf, "/afs/%s/%s/%s", info[FS_MACHINE],
399 lowercase(info[FS_L_TYPE]), info[FS_NAME]);
400 }
7ac48069 401 info[FS_PACK] = strdup(temp_buf);
7cb7d352 402 }
403 }
5eaef520 404 if (GetValueFromUser("Filesystem's Pack Name", &info[FS_PACK]) ==
405 SUB_ERROR)
406 return NULL;
407 if (GetValueFromUser("Filesystem's Mount Point", &info[FS_M_POINT]) ==
408 SUB_ERROR)
409 return NULL;
410 sprintf(access_type, "fs_access_%s", info[FS_TYPE]);
411 if (GetTypeFromUser("Filesystem's Default Access", access_type,
412 &info[FS_ACCESS]) == SUB_ERROR)
413 return NULL;
9e2516d6 414 }
5eaef520 415 if (GetValueFromUser("Comments about this Filesystem", &info[FS_COMMENTS])
416 == SUB_ERROR)
417 return NULL;
418 if (GetValueFromUser("Filesystem's owner (user)", &info[FS_OWNER]) ==
419 SUB_ERROR)
420 return NULL;
421 if (GetValueFromUser("Filesystem's owners (group)", &info[FS_OWNERS]) ==
422 SUB_ERROR)
423 return NULL;
424 if (!fsgroup)
425 if (GetYesNoValueFromUser("Propagate changes to fileserver",
426 &info[FS_CREATE]) == SUB_ERROR)
427 return NULL;
428 if (strcasecmp(info[FS_TYPE], "AFS"))
429 {
430 if (GetTypeFromUser("Filesystem's lockertype", "lockertype",
431 &info[FS_L_TYPE]) == SUB_ERROR)
432 return NULL;
7cb7d352 433 }
08345b74 434
5eaef520 435 FreeAndClear(&info[FS_MODTIME], TRUE);
436 FreeAndClear(&info[FS_MODBY], TRUE);
437 FreeAndClear(&info[FS_MODWITH], TRUE);
08345b74 438
5eaef520 439 if (name) /* slide the newname into the #2 slot. */
440 SlipInNewName(info, newname);
08345b74 441
5eaef520 442 return info;
08345b74 443}
444
7cb7d352 445
08345b74 446/* --------------- Filesystem Menu ------------- */
447
448/* Function Name: GetFS
449 * Description: Get Filesystem information by name.
450 * Arguments: argc, argv - name of filsys in argv[1].
451 * Returns: DM_NORMAL.
452 */
453
5eaef520 454int GetFS(int argc, char **argv)
08345b74 455{
5eaef520 456 struct qelem *top;
08345b74 457
5eaef520 458 top = GetFSInfo(LABEL, argv[1]); /* get info. */
7ac48069 459 Loop(top, (void (*)(char **))PrintFSInfo);
5eaef520 460 FreeQueue(top); /* clean the queue. */
461 return DM_NORMAL;
ef1e47d8 462}
463
464/* Function Name: GetFSM
465 * Description: Get Filesystem information by machine.
466 * Arguments: argc, argv - name of server in argv[1].
467 * Returns: DM_NORMAL.
468 */
469
5eaef520 470int GetFSM(int argc, char **argv)
ef1e47d8 471{
5eaef520 472 struct qelem *top;
ef1e47d8 473
7ac48069 474 argv[1] = canonicalize_hostname(strdup(argv[1]));
5eaef520 475 top = GetFSInfo(MACHINE, argv[1]); /* get info. */
7ac48069 476 Loop(top, (void (*)(char **))PrintFSInfo);
5eaef520 477 FreeQueue(top); /* clean the queue. */
478 return DM_NORMAL;
08345b74 479}
480
402461ad 481/* Function Name: RealDeleteFS
482 * Description: Does the real deletion work.
483 * Arguments: info - array of char *'s containing all useful info.
5eaef520 484 * one_item - a Boolean that is true if only one item
402461ad 485 * in queue that dumped us here.
486 * Returns: none.
487 */
488
5eaef520 489void RealDeleteFS(char **info, Bool one_item)
402461ad 490{
5eaef520 491 int stat;
492 char temp_buf[BUFSIZ];
493
494 /*
495 * Deletions are performed if the user hits 'y' on a list of multiple
496 * filesystem, or if the user confirms on a unique alias.
497 */
498 sprintf(temp_buf, "Are you sure that you want to delete filesystem %s",
499 info[FS_NAME]);
500 if (!one_item || Confirm(temp_buf))
501 {
502 if ((stat = do_mr_query("delete_filesys", 1,
7ac48069 503 &info[FS_NAME], NULL, NULL)))
5eaef520 504 com_err(program_name, stat, " filesystem not deleted.");
505 else
506 Put_message("Filesystem deleted.");
402461ad 507 }
5eaef520 508 else
509 Put_message("Filesystem not deleted.");
402461ad 510}
511
08345b74 512/* Function Name: DeleteFS
513 * Description: Delete a filesystem give its name.
514 * Arguments: argc, argv - argv[1] is the name of the filesystem.
515 * Returns: none.
516 */
517
5eaef520 518int DeleteFS(int argc, char **argv)
08345b74 519{
5eaef520 520 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
521 QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
08345b74 522
5eaef520 523 FreeQueue(elem);
524 return DM_NORMAL;
08345b74 525}
526
402461ad 527/* Function Name: RealChangeFS
528 * Description: performs the actual change to the filesys.
5eaef520 529 * Arguments: info - the information
402461ad 530 * junk - an unused boolean.
531 * Returns: none.
532 */
533
5eaef520 534static void RealChangeFS(char **info, Bool junk)
402461ad 535{
5eaef520 536 int stat;
537 char **args;
538 extern Menu nfsphys_menu;
539
540 args = AskFSInfo(info, TRUE);
541 if (!args)
542 {
543 Put_message("Aborted.");
544 return;
9777ade9 545 }
7ac48069 546 stat = do_mr_query("update_filesys", CountArgs(args), args, NULL, NULL);
5eaef520 547 switch (stat)
548 {
8defc06b 549 case MR_NFS:
5eaef520 550 Put_message("That NFS filesystem is not exported.");
551 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE)
552 {
553 Do_menu(&nfsphys_menu, 0, NULL);
554 if (YesNoQuestion("Retry filesystem update now (Y/N)", TRUE) == TRUE)
555 {
556 if ((stat = do_mr_query("update_filesys", CountArgs(args), args,
7ac48069 557 NULL, NULL)))
5eaef520 558 com_err(program_name, stat, " filesystem not updated");
559 else
560 Put_message("filesystem sucessfully updated.");
bff71786 561 }
562 }
5eaef520 563 break;
8defc06b 564 case MR_SUCCESS:
5eaef520 565 break;
bff71786 566 default:
5eaef520 567 com_err(program_name, stat, " in UpdateFS");
bff71786 568 }
402461ad 569}
570
08345b74 571/* Function Name: ChangeFS
572 * Description: change the information in a filesys record.
573 * Arguments: arc, argv - value of filsys in argv[1].
574 * Returns: DM_NORMAL.
575 */
576
5eaef520 577int ChangeFS(int argc, char **argv)
08345b74 578{
5eaef520 579 struct qelem *elem = GetFSInfo(LABEL, argv[1]);
580 QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
08345b74 581
5eaef520 582 FreeQueue(elem);
583 return DM_NORMAL;
08345b74 584}
585
586/* Function Name: AddFS
587 * Description: change the information in a filesys record.
588 * Arguments: arc, argv - name of filsys in argv[1].
589 * Returns: DM_NORMAL.
590 */
591
5eaef520 592int AddFS(int argc, char **argv)
08345b74 593{
5eaef520 594 char *info[MAX_ARGS_SIZE], **args, buf[BUFSIZ];
595 int stat;
596 extern Menu nfsphys_menu;
597
598 if (!ValidName(argv[1]))
599 return DM_NORMAL;
600
601 if (!(stat = do_mr_query("get_filesys_by_label", 1, argv + 1,
7ac48069 602 NULL, NULL)))
5eaef520 603 {
604 Put_message ("A Filesystem by that name already exists.");
605 return DM_NORMAL;
606 }
607 else if (stat != MR_NO_MATCH)
608 {
609 com_err(program_name, stat, " in AddFS");
610 return DM_NORMAL;
611 }
612
613 if (!(args = AskFSInfo(SetDefaults(info, argv[1]), FALSE)))
614 {
615 Put_message("Aborted.");
616 return DM_NORMAL;
44b10c87 617 }
461c03b6 618
7ac48069 619 stat = do_mr_query("add_filesys", CountArgs(args), args, NULL, NULL);
5eaef520 620 switch (stat)
621 {
8defc06b 622 case MR_NFS:
5eaef520 623 Put_message("That NFS filesystem is not exported.");
624 if (YesNoQuestion("Fix this now (Y/N)", TRUE) == TRUE)
625 {
626 Do_menu(&nfsphys_menu, 0, NULL);
627 if (YesNoQuestion("Retry filesystem creation now (Y/N)", TRUE)
628 == TRUE) {
629 if ((stat = do_mr_query("add_filesys", CountArgs(args), args,
7ac48069 630 NULL, NULL)))
5eaef520 631 com_err(program_name, stat, " in AddFS");
632 else
633 Put_message("Created.");
634 }
bff71786 635 }
5eaef520 636 break;
8defc06b 637 case MR_SUCCESS:
5eaef520 638 break;
bff71786 639 default:
5eaef520 640 com_err(program_name, stat, " in AddFS");
bff71786 641 }
08345b74 642
5eaef520 643 if (stat == MR_SUCCESS && !strcasecmp(info[FS_L_TYPE], "HOMEDIR"))
644 {
645 static char *val[] = {"def_quota", NULL};
646 static char *def_quota = NULL;
647 char *argv[Q_QUOTA + 1];
648 struct qelem *top = NULL;
649
650 if (!def_quota)
651 {
652 stat = do_mr_query("get_value", CountArgs(val), val,
7ac48069 653 StoreInfo, &top);
5eaef520 654 if (stat != MR_SUCCESS)
655 com_err(program_name, stat, " getting default quota");
656 else
657 {
658 top = QueueTop(top);
7ac48069 659 def_quota = strdup(((char **)top->q_data)[0]);
5eaef520 660 FreeQueue(top);
7cce48da 661 }
662 }
5eaef520 663 if (def_quota)
664 {
665 sprintf(buf, "Give user %s a quota of %s on filesys %s (Y/N)",
666 info[FS_NAME], def_quota, info[FS_NAME]);
667 if (YesNoQuestion(buf, 1) == TRUE)
668 {
669 argv[Q_NAME] = argv[Q_FILESYS] = info[FS_NAME];
670 if (!strcmp(info[FS_TYPE], "NFS"))
671 argv[Q_TYPE] = "USER";
672 else
673 argv[Q_TYPE] = "ANY";
674 argv[Q_QUOTA] = def_quota;
7ac48069 675 if ((stat = do_mr_query("add_quota", Q_QUOTA + 1, argv, NULL,
5eaef520 676 NULL)))
677 com_err(program_name, stat, " while adding quota");
7cce48da 678 }
679 }
5eaef520 680 }
681 else if (stat == MR_SUCCESS)
682 {
683 if (YesNoQuestion("Assign a quota on this filesystem (Y/N)", 1) == TRUE)
684 {
685 parsed_argc = 1;
686 parsed_argv[0] = info[FS_NAME];
7ac48069 687 AddQuota(parsed_argc, parsed_argv);
d25fad50 688 }
7cce48da 689 }
690
5eaef520 691 FreeInfo(info);
692 return DM_NORMAL;
08345b74 693}
694
9e2516d6 695/* Function Name: SortAfter
5eaef520 696 * Description: choose a sortkey to cause an item to be added after
9e2516d6 697 * the count element in the queue
698 * Arguments: queue of filesys names & sortkeys, queue count pointer
699 * Returns: sort key to use.
700 */
701
5eaef520 702char *SortAfter(struct qelem *elem, int count)
9e2516d6 703{
5eaef520 704 char *prev, *next, prevnext, *key, keybuf[9];
705
706 /* first find the two keys we need to insert between */
707 prev = "A";
708 for (; count > 0; count--)
709 {
710 prev = ((char **)elem->q_data)[1];
711 if (elem->q_forw)
712 elem = elem->q_forw;
713 else
714 break;
9e2516d6 715 }
5eaef520 716 if (count > 0)
717 next = "Z";
718 else
719 next = ((char **)elem->q_data)[1];
720
721 /* now copy the matching characters */
722 for (key = keybuf; *prev && *prev == *next; next++)
723 *key++ = *prev++;
724
725 /* and set the last character */
726 if (*prev == 0)
727 *prev = prevnext = 'A';
728 else
729 prevnext = prev[1];
730 if (prevnext == 0)
731 prevnext = 'A';
732 if (*next == 0)
733 *next = 'Z';
734 if (*next - *prev > 1)
735 *key++ = (*next + *prev) / 2;
736 else
737 {
738 *key++ = *prev;
739 *key++ = (prevnext + 'Z') / 2;
9e2516d6 740 }
5eaef520 741 *key = 0;
7ac48069 742 return strdup(keybuf);
9e2516d6 743}
744
745/* Function Name: AddFSToGroup
746 * Description: add a filesystem to an FS group
747 * Arguments: arc, argv - name of group in argv[1], filesys in argv[2].
748 * Returns: DM_NORMAL.
749 */
750
5eaef520 751int AddFSToGroup(int argc, char **argv)
9e2516d6 752{
5eaef520 753 int stat, count;
754 struct qelem *elem = NULL;
755 char buf[BUFSIZ], *args[5], *bufp;
756
757 if ((stat = do_mr_query("get_fsgroup_members", 1, argv + 1, StoreInfo,
7ac48069 758 &elem)))
5eaef520 759 {
760 if (stat != MR_NO_MATCH)
761 com_err(program_name, stat, " in AddFSToGroup");
9e2516d6 762 }
5eaef520 763 if (elem == NULL)
764 {
765 args[0] = argv[1];
766 args[1] = argv[2];
767 args[2] = "M";
7ac48069 768 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, NULL, NULL);
5eaef520 769 if (stat)
770 com_err(program_name, stat, " in AddFSToGroup");
771 return DM_NORMAL;
9e2516d6 772 }
5eaef520 773 elem = QueueTop(elem);
774 fsgCount = 1;
775 Loop(elem, PrintFSGMembers);
776 sprintf(buf, "%d", QueueCount(elem));
7ac48069 777 bufp = strdup(buf);
5eaef520 778 if (GetValueFromUser("Enter number of filesystem it should follow "
779 "(0 to make it first):", &bufp) == SUB_ERROR)
780 return DM_NORMAL;
781 count = atoi(bufp);
782 free(bufp);
783 args[2] = SortAfter(elem, count);
784
785 FreeQueue(QueueTop(elem));
786 args[0] = argv[1];
787 args[1] = argv[2];
7ac48069 788 stat = do_mr_query("add_filesys_to_fsgroup", 3, args, NULL, NULL);
5eaef520 789 if (stat == MR_EXISTS)
790 {
791 Put_message("That filesystem is already a member of the group.");
792 Put_message("Use the order command if you want to change the "
793 "sorting order.");
794 }
795 else if (stat)
796 com_err(program_name, stat, " in AddFSToGroup");
797 return DM_NORMAL;
9e2516d6 798}
799
800
801/* Function Name: RemoveFSFromGroup
802 * Description: delete a filesystem from an FS group
803 * Arguments: arc, argv - name of group in argv[1].
804 * Returns: DM_NORMAL.
805 */
806
5eaef520 807int RemoveFSFromGroup(int argc, char **argv)
9e2516d6 808{
5eaef520 809 int stat;
810 char buf[BUFSIZ];
811
812 sprintf(buf, "Delete filesystem %s from FS group %s", argv[2], argv[1]);
813 if (!Confirm(buf))
814 return DM_NORMAL;
815 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, argv + 1,
7ac48069 816 NULL, NULL)))
5eaef520 817 com_err(program_name, stat, ", not removed.");
818 return DM_NORMAL;
9e2516d6 819}
820
821/* Function Name: ChangeFSGroupOrder
822 * Description: change the sortkey on a filesys in an FSgroup
823 * Arguments: arc, argv - name of group in argv[1].
824 * Returns: DM_NORMAL.
825 */
826
5eaef520 827int ChangeFSGroupOrder(int argc, char **argv)
9e2516d6 828{
5eaef520 829 int stat, src, dst, i;
830 struct qelem *elem = NULL, *top, *tmpelem;
831 char buf[BUFSIZ], *bufp, *args[3];
832
833 if ((stat = do_mr_query("get_fsgroup_members", 1, argv + 1, StoreInfo,
7ac48069 834 &elem)))
5eaef520 835 {
836 if (stat == MR_NO_MATCH)
837 {
838 sprintf(buf,
839 "Ether %s is not a filesystem group or it has no members",
840 argv[1]);
841 Put_message(buf);
842 }
843 else
844 com_err(program_name, stat, " in ChangeFSGroupOrder");
845 return DM_NORMAL;
9e2516d6 846 }
5eaef520 847 top = QueueTop(elem);
848 fsgCount = 1;
849 Loop(top, PrintFSGMembers);
850 while (1)
851 {
7ac48069 852 bufp = strdup("1");
5eaef520 853 if (GetValueFromUser("Enter number of the filesystem to move:",
854 &bufp) == SUB_ERROR)
855 return DM_NORMAL;
856 src = atoi(bufp);
857 free(bufp);
858 if (src < 0)
859 {
860 Put_message("You must enter a positive number (or 0 to abort).");
861 continue;
9e2516d6 862 }
5eaef520 863 else if (src == 0)
864 {
865 Put_message("Aborted.");
866 return DM_NORMAL;
9e2516d6 867 }
5eaef520 868 for (elem = top, i = src; i-- > 1 && elem->q_forw; elem = elem->q_forw)
869 ;
870 if (i > 0)
871 {
872 Put_message("You entered a number that is too high");
873 continue;
874 }
875 break;
9e2516d6 876 }
5eaef520 877 while (1)
878 {
7ac48069 879 bufp = strdup("0");
5eaef520 880 if (GetValueFromUser("Enter number of filesystem it should follow "
881 "(0 to make it first):", &bufp) == SUB_ERROR)
882 return DM_NORMAL;
883 dst = atoi(bufp);
884 free(bufp);
885 if (src == dst || src == dst + 1)
886 {
887 Put_message("That has no effect on the sorting order!");
888 return DM_NORMAL;
860a8dbe 889 }
5eaef520 890 if (dst < 0)
891 {
892 Put_message("You must enter a non-negative number.");
893 continue;
860a8dbe 894 }
5eaef520 895 for (tmpelem = top, i = dst; i-- > 1 && tmpelem->q_forw;
896 tmpelem = tmpelem->q_forw)
897 ;
898 if (i > 0)
899 {
900 Put_message("You entered a number that is too high");
901 continue;
860a8dbe 902 }
5eaef520 903 break;
9e2516d6 904 }
5eaef520 905 args[2] = SortAfter(top, dst);
906 args[0] = argv[1];
907 args[1] = ((char **)elem->q_data)[0];
908 if ((stat = do_mr_query("remove_filesys_from_fsgroup", 2, args,
7ac48069 909 NULL, NULL)))
5eaef520 910 {
911 com_err(program_name, stat, " in ChangeFSGroupOrder");
912 return DM_NORMAL;
9e2516d6 913 }
5eaef520 914 if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
7ac48069 915 NULL, NULL)))
5eaef520 916 com_err(program_name, stat, " in ChangeFSGroupOrder");
917 return DM_NORMAL;
9e2516d6 918}
919
920
08345b74 921/* -------------- Top Level Menu ---------------- */
922
923/* Function Name: GetFSAlias
924 * Description: Gets the value for a Filesystem Alias.
925 * Arguments: argc, argv - name of alias in argv[1].
926 * Returns: DM_NORMAL.
927 * NOTES: There should only be one filesystem per alias, thus
928 * this will work correctly.
929 */
930
5eaef520 931int GetFSAlias(int argc, char **argv)
08345b74 932{
5eaef520 933 struct qelem *top;
08345b74 934
5eaef520 935 top = GetFSInfo(ALIAS, argv[1]);
936 Put_message(" "); /* blank line. */
937 Loop(top, (void *) PrintFSAlias);
938 FreeQueue(top);
939 return DM_NORMAL;
08345b74 940}
941
942/* Function Name: CreateFSAlias
6c7a2fcf 943 * Description: Create an alias name for a filesystem
08345b74 944 * Arguments: argc, argv - name of alias in argv[1].
945 * Returns: DM_NORMAL.
946 * NOTES: This requires (name, type, transl) I get {name, translation}
5eaef520 947 * from the user. I provide type, which is well-known.
08345b74 948 */
949
5eaef520 950int CreateFSAlias(int argc, char **argv)
08345b74 951{
44d12d58 952 int stat;
5eaef520 953 struct qelem *elem, *top;
954 char *args[MAX_ARGS_SIZE], buf[BUFSIZ], **info;
955
956 elem = NULL;
957
958 if (!ValidName(argv[1]))
959 return DM_NORMAL;
960
7ac48069 961 args[ALIAS_NAME] = strdup(argv[1]);
962 args[ALIAS_TYPE] = strdup(FS_ALIAS_TYPE);
963 args[ALIAS_TRANS] = strdup("*");
5eaef520 964
965 /*
966 * Check to see if this alias already exists in the database, if so then
967 * print out values, free memory used and then exit.
968 */
969
7ac48069 970 if (!(stat = do_mr_query("get_alias", 3, args, StoreInfo, &elem)))
5eaef520 971 {
972 top = elem = QueueTop(elem);
973 while (elem)
974 {
7ac48069 975 info = elem->q_data;
5eaef520 976 sprintf(buf, "The alias: %s currently describes the filesystem %s",
977 info[ALIAS_NAME], info[ALIAS_TRANS]);
978 Put_message(buf);
979 elem = elem->q_forw;
08345b74 980 }
5eaef520 981 FreeQueue(top);
982 return DM_NORMAL;
08345b74 983 }
5eaef520 984 else if (stat != MR_NO_MATCH)
985 {
986 com_err(program_name, stat, " in CreateFSAlias.");
987 return DM_NORMAL;
08345b74 988 }
989
7ac48069 990 args[ALIAS_TRANS] = strdup("");
5eaef520 991 args[ALIAS_END] = NULL;
992 if (GetValueFromUser("Which filesystem will this alias point to?",
993 &args[ALIAS_TRANS]) == SUB_ERROR)
994 return DM_NORMAL;
08345b74 995
7ac48069 996 if ((stat = do_mr_query("add_alias", 3, args, NULL, NULL)))
5eaef520 997 com_err(program_name, stat, " in CreateFSAlias.");
08345b74 998
5eaef520 999 FreeInfo(args);
1000 return DM_NORMAL;
08345b74 1001}
5eaef520 1002
402461ad 1003/* Function Name: RealDeleteFSAlias
1004 * Description: Does the real deletion work.
1005 * Arguments: info - array of char *'s containing all useful info.
5eaef520 1006 * one_item - a Boolean that is true if only one item
402461ad 1007 * in queue that dumped us here.
1008 * Returns: none.
1009 */
1010
5eaef520 1011void RealDeleteFSAlias(char **info, Bool one_item)
402461ad 1012{
5eaef520 1013 int stat;
1014 char temp_buf[BUFSIZ];
1015
1016 /*
1017 * Deletions are performed if the user hits 'y' on a list of multiple
1018 * filesystem, or if the user confirms on a unique alias.
1019 */
1020 sprintf(temp_buf,
1021 "Are you sure that you want to delete the filesystem alias %s",
1022 info[ALIAS_NAME]);
1023 if (!one_item || Confirm(temp_buf))
1024 {
1025 if ((stat = do_mr_query("delete_alias", CountArgs(info),
7ac48069 1026 info, NULL, NULL)))
5eaef520 1027 com_err(program_name, stat, " filesystem alias not deleted.");
1028 else
1029 Put_message("Filesystem alias deleted.");
402461ad 1030 }
5eaef520 1031 else
1032 Put_message("Filesystem alias not deleted.");
402461ad 1033}
08345b74 1034
1035/* Function Name: DeleteFSAlias
6c7a2fcf 1036 * Description: Delete an alias name for a filesystem
08345b74 1037 * Arguments: argc, argv - name of alias in argv[1].
1038 * Returns: DM_NORMAL.
1039 * NOTES: This requires (name, type, transl) I get {name, translation}
5eaef520 1040 * from the user. I provide type, which is well-known.
08345b74 1041 */
1042
5eaef520 1043int DeleteFSAlias(int argc, char **argv)
08345b74 1044{
5eaef520 1045 struct qelem *elem = GetFSInfo(ALIAS, argv[1]);
1046 QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
1047 "Delete the Filesystem Alias");
1048 FreeQueue(elem);
1049 return DM_NORMAL;
08345b74 1050}
1051
1052/* Function Name: AttachHelp
1053 * Description: Print help info on attachmaint.
1054 * Arguments: none
1055 * Returns: DM_NORMAL.
1056 */
1057
5eaef520 1058int AttachHelp(void)
08345b74 1059{
5eaef520 1060 static char *message[] = {
1061 "These are the options:",
1062 "",
1063 "get - get information about a filesystem.",
1064 "add - add a new filesystem to the data base.",
1065 "update - update the information in the database on a filesystem.",
1066 "delete - delete a filesystem from the database.",
1067 "check - check information about association of a name and a filesys.",
1068 "alias - associate a name with a filesystem.",
1069 "unalias - disassociate a name with a filesystem.",
1070 "verbose - toggle the request for delete confirmation.",
1071 NULL,
1072 };
1073
1074 return PrintHelp(message);
08345b74 1075}
9e2516d6 1076
1077/* Function Name: FSGroupHelp
1078 * Description: Print help info on fsgroups.
1079 * Arguments: none
1080 * Returns: DM_NORMAL.
1081 */
1082
7ac48069 1083int FSGroupHelp(int argc, char **argv)
9e2516d6 1084{
5eaef520 1085 static char *message[] = {
1086 "A filesystem group is a named sorted list of filesystems.",
1087 "",
1088 "To create, modify, or delete a group itself, use the menu above",
1089 " this one, and manipulate a filesystem of type FSGROUP.",
1090 "Options here are:",
1091 " get - get info about a group and show its members",
1092 " add - add a new member to a group.",
1093 " remove - remove a member from a group.",
1094 " order - change the sorting order of a group.",
1095 NULL
1096 };
1097
1098 return PrintHelp(message);
9e2516d6 1099}
This page took 0.310075 seconds and 5 git commands to generate.