]> andersk Git - moira.git/blob - clients/moira/attach.c
b70c39cdbf0829d8ae3ecd3a6e786b5901be7ef2
[moira.git] / clients / moira / attach.c
1 /* $Id$
2  *
3  *      This is the file attach.c for the Moira Client, which allows users
4  *      to quickly and easily maintain most parts of the Moira database.
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  *
9  *      Created:        5/4/88
10  *      By:             Chris D. Peterson
11  *
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>.
15  */
16
17 #include <mit-copyright.h>
18 #include <moira.h>
19 #include <moira_site.h>
20
21 #include "defs.h"
22 #include "f_defs.h"
23 #include "globals.h"
24
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
33 RCSID("$Header$");
34
35 char *canonicalize_cell(char *c);
36 int GetAliasValue(int argc, char **argv, void *retval);
37 void RealDeleteFS(char **info, Bool one_item);
38 char *SortAfter(struct mqelem *elem, int count);
39 void RealDeleteFSAlias(char **info, Bool one_item);
40
41 #define FS_ALIAS_TYPE "FILESYS"
42
43 #define LABEL        0
44 #define MACHINE      1
45 #define GROUP        2
46 #define ALIAS        3
47
48 #define NO_MACHINE       ("[NONE]")
49
50 #define DEFAULT_TYPE     ("AFS")
51 #define DEFAULT_MACHINE  DEFAULT_NONE
52 #define DEFAULT_PACK     DEFAULT_NONE
53 #define DEFAULT_ACCESS   ("w")
54 #define DEFAULT_COMMENTS DEFAULT_COMMENT
55 #define DEFAULT_OWNER    (user)
56 #define DEFAULT_OWNERS   (user)
57 #define DEFAULT_CREATE   DEFAULT_YES
58 #define DEFAULT_L_TYPE   ("PROJECT")
59 #define DEFAULT_CELL     ("athena.mit.edu")
60
61 /*      Function Name: SetDefaults
62  *      Description: sets the default values for filesystem additions.
63  *      Arguments: info - an array of char pointers to recieve defaults.
64  *      Returns: char ** (this array, now filled).
65  */
66
67 static char **SetDefaults(char **info, char *name)
68 {
69   char buf[BUFSIZ];
70
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);
75   sprintf(buf, "/mit/%s", name);
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);
83   info[FS_MODTIME] = info[FS_MODBY] = info[FS_MODWITH] = info[FS_END] = NULL;
84   return info;
85 }
86
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
94 static struct mqelem *GetFSInfo(int type, char *name)
95 {
96   int stat;
97   struct mqelem *elem = NULL;
98   char *args[5];
99
100   switch (type)
101     {
102     case LABEL:
103       if ((stat = do_mr_query("get_filesys_by_label", 1, &name,
104                               StoreInfo, &elem)))
105         {
106           com_err(program_name, stat, " in GetFSInfo");
107           return NULL;
108         }
109       break;
110     case MACHINE:
111       if ((stat = do_mr_query("get_filesys_by_machine", 1, &name,
112                               StoreInfo, &elem)))
113         {
114           com_err(program_name, stat, " in GetFSInfo");
115           return NULL;
116         }
117       break;
118     case GROUP:
119       if ((stat = do_mr_query("get_filesys_by_group", 1, &name,
120                               StoreInfo, &elem)))
121         {
122           com_err(program_name, stat, " in GetFSInfo");
123           return NULL;
124         }
125       break;
126     case ALIAS:
127       args[ALIAS_NAME] = name;
128       args[ALIAS_TYPE] = FS_ALIAS_TYPE;
129       args[ALIAS_TRANS] = "*";
130       if ((stat = do_mr_query("get_alias", 3, args, StoreInfo, &elem)))
131         {
132           com_err(program_name, stat, " in get_alias.");
133           return NULL;
134         }
135     }
136
137   return QueueTop(elem);
138 }
139
140 /*      Function Name: PrintFSAlias
141  *      Description: Prints a filesystem alias
142  *      Arguments: info - an array contains the strings of info.
143  *      Returns: the name of the filesys - used be QueryLoop().
144  */
145
146 static char *PrintFSAlias(char **info)
147 {
148   char buf[BUFSIZ];
149
150   sprintf(buf, "Alias: %-25s Filesystem: %s", info[ALIAS_NAME],
151           info[ALIAS_TRANS]);
152   Put_message(buf);
153   return info[ALIAS_NAME];
154 }
155
156 static int fsgCount = 1;
157
158 static void PrintFSGMembers(char **info)
159 {
160   char print_buf[BUFSIZ];
161
162   sprintf(print_buf, "  %d. Filesystem: %-32s (sort key: %s)",
163           fsgCount++, info[0], info[1]);
164   Put_message(print_buf);
165 }
166
167
168 /*      Function Name: PrintFSInfo
169  *      Description: Prints the filesystem information.
170  *      Arguments: info - a pointer to the filesystem information.
171  *      Returns: none.
172  */
173
174 static char *PrintFSInfo(char **info)
175 {
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 mqelem *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],
198                               StoreInfo, &elem)))
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);
210         }
211     }
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];
235 }
236
237
238 char *canonicalize_cell(char *c)
239 {
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);
252   return strdup(path);
253 }
254
255
256 int GetAliasValue(int argc, char **argv, void *retval)
257 {
258   *(char **)retval = strdup(argv[2]);
259   return MR_CONT;
260 }
261
262 /*      Function Name: AskFSInfo.
263  *      Description: This function askes the user for information about a
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
271 static char **AskFSInfo(char **info, Bool name)
272 {
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     {
283       newname = strdup(info[FS_NAME]);
284       if (GetValueFromUser("The new name for this filesystem",
285                            &newname) == SUB_ERROR)
286         return NULL;
287     }
288
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]);
301       info[FS_MACHINE] = strdup(NO_MACHINE);
302     }
303   else
304     {
305       if (!strcasecmp(info[FS_TYPE], "AFS"))
306         {
307           if (!name || newdefaults)
308             {
309               free(info[FS_MACHINE]);
310               info[FS_MACHINE] = strdup(DEFAULT_CELL);
311             }
312           if (GetValueFromUser("Filesystem's Cell", &info[FS_MACHINE]) ==
313                 SUB_ERROR)
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]);
323         }
324     }
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;
370                             }
371                         }
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;
383                             }
384                         }
385                       else
386                         strcat(temp_buf, "/other");
387                     }
388                   else
389                     {
390                         /* no default */
391                     }
392                   strcat(temp_buf, "/");
393                   strcat(temp_buf, info[FS_NAME]);
394                   free(path);
395                 }
396               else
397                 {
398                   sprintf(temp_buf, "/afs/%s/%s/%s", info[FS_MACHINE],
399                           lowercase(info[FS_L_TYPE]), info[FS_NAME]);
400                 }
401               info[FS_PACK] = strdup(temp_buf);
402             }
403         }
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;
414     }
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;
433     }
434
435   FreeAndClear(&info[FS_MODTIME], TRUE);
436   FreeAndClear(&info[FS_MODBY], TRUE);
437   FreeAndClear(&info[FS_MODWITH], TRUE);
438
439   if (name)                     /* slide the newname into the #2 slot. */
440     SlipInNewName(info, newname);
441
442   return info;
443 }
444
445
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
454 int GetFS(int argc, char **argv)
455 {
456   struct mqelem *top;
457
458   top = GetFSInfo(LABEL, argv[1]); /* get info. */
459   Loop(top, (void (*)(char **))PrintFSInfo);
460   FreeQueue(top);               /* clean the queue. */
461   return DM_NORMAL;
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
470 int GetFSM(int argc, char **argv)
471 {
472   struct mqelem *top;
473
474   argv[1] = canonicalize_hostname(strdup(argv[1]));
475   top = GetFSInfo(MACHINE, argv[1]); /* get info. */
476   Loop(top, (void (*)(char **))PrintFSInfo);
477   FreeQueue(top);               /* clean the queue. */
478   return DM_NORMAL;
479 }
480
481 /*      Function Name: RealDeleteFS
482  *      Description: Does the real deletion work.
483  *      Arguments: info - array of char *'s containing all useful info.
484  *                 one_item - a Boolean that is true if only one item
485  *                              in queue that dumped us here.
486  *      Returns: none.
487  */
488
489 void RealDeleteFS(char **info, Bool one_item)
490 {
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,
503                               &info[FS_NAME], NULL, NULL)))
504         com_err(program_name, stat, " filesystem not deleted.");
505       else
506         Put_message("Filesystem deleted.");
507     }
508   else
509     Put_message("Filesystem not deleted.");
510 }
511
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
518 int DeleteFS(int argc, char **argv)
519 {
520   struct mqelem *elem = GetFSInfo(LABEL, argv[1]);
521   QueryLoop(elem, PrintFSInfo, RealDeleteFS, "Delete the Filesystem");
522
523   FreeQueue(elem);
524   return DM_NORMAL;
525 }
526
527 /*      Function Name: RealChangeFS
528  *      Description: performs the actual change to the filesys.
529  *      Arguments: info - the information
530  *                 junk - an unused boolean.
531  *      Returns: none.
532  */
533
534 static void RealChangeFS(char **info, Bool junk)
535 {
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;
545     }
546   stat = do_mr_query("update_filesys", CountArgs(args), args, NULL, NULL);
547   switch (stat)
548     {
549     case MR_NFS:
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,
557                                       NULL, NULL)))
558                 com_err(program_name, stat, " filesystem not updated");
559               else
560                 Put_message("filesystem sucessfully updated.");
561             }
562         }
563       break;
564     case MR_SUCCESS:
565       break;
566     default:
567       com_err(program_name, stat, " in UpdateFS");
568     }
569 }
570
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
577 int ChangeFS(int argc, char **argv)
578 {
579   struct mqelem *elem = GetFSInfo(LABEL, argv[1]);
580   QueryLoop(elem, NullPrint, RealChangeFS, "Update the Filesystem");
581
582   FreeQueue(elem);
583   return DM_NORMAL;
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
592 int AddFS(int argc, char **argv)
593 {
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,
602                               NULL, NULL)))
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;
617     }
618
619   stat = do_mr_query("add_filesys", CountArgs(args), args, NULL, NULL);
620   switch (stat)
621     {
622     case MR_NFS:
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,
630                                     NULL, NULL)))
631               com_err(program_name, stat, " in AddFS");
632             else
633               Put_message("Created.");
634           }
635         }
636       break;
637     case MR_SUCCESS:
638       break;
639     default:
640       com_err(program_name, stat, " in AddFS");
641     }
642
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 mqelem *top = NULL;
649
650       if (!def_quota)
651         {
652           stat = do_mr_query("get_value", CountArgs(val), val,
653                              StoreInfo, &top);
654           if (stat != MR_SUCCESS)
655             com_err(program_name, stat, " getting default quota");
656           else
657             {
658               top = QueueTop(top);
659               def_quota = strdup(((char **)top->q_data)[0]);
660               FreeQueue(top);
661             }
662         }
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;
675               if ((stat = do_mr_query("add_quota", Q_QUOTA + 1, argv, NULL,
676                                       NULL)))
677                 com_err(program_name, stat, " while adding quota");
678             }
679         }
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];
687           AddQuota(parsed_argc, parsed_argv);
688         }
689     }
690
691   FreeInfo(info);
692   return DM_NORMAL;
693 }
694
695 /*      Function Name: SortAfter
696  *      Description: choose a sortkey to cause an item to be added after
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
702 char *SortAfter(struct mqelem *elem, int count)
703 {
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;
715     }
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;
740     }
741   *key = 0;
742   return strdup(keybuf);
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
751 int AddFSToGroup(int argc, char **argv)
752 {
753   int stat, count;
754   struct mqelem *elem = NULL;
755   char buf[BUFSIZ], *args[5], *bufp;
756
757   if ((stat = do_mr_query("get_fsgroup_members", 1, argv + 1, StoreInfo,
758                           &elem)))
759     {
760       if (stat != MR_NO_MATCH)
761         com_err(program_name, stat, " in AddFSToGroup");
762     }
763   if (elem == NULL)
764     {
765       args[0] = argv[1];
766       args[1] = argv[2];
767       args[2] = "M";
768       stat = do_mr_query("add_filesys_to_fsgroup", 3, args, NULL, NULL);
769       if (stat)
770         com_err(program_name, stat, " in AddFSToGroup");
771       return DM_NORMAL;
772     }
773   elem = QueueTop(elem);
774   fsgCount = 1;
775   Loop(elem, PrintFSGMembers);
776   sprintf(buf, "%d", QueueCount(elem));
777   bufp = strdup(buf);
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];
788   stat = do_mr_query("add_filesys_to_fsgroup", 3, args, NULL, NULL);
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;
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
807 int RemoveFSFromGroup(int argc, char **argv)
808 {
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,
816                           NULL, NULL)))
817     com_err(program_name, stat, ", not removed.");
818   return DM_NORMAL;
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
827 int ChangeFSGroupOrder(int argc, char **argv)
828 {
829   int stat, src, dst, i;
830   struct mqelem *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,
834                           &elem)))
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;
846     }
847   top = QueueTop(elem);
848   fsgCount = 1;
849   Loop(top, PrintFSGMembers);
850   while (1)
851     {
852       bufp = strdup("1");
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;
862         }
863       else if (src == 0)
864         {
865           Put_message("Aborted.");
866           return DM_NORMAL;
867         }
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;
876     }
877   while (1)
878     {
879       bufp = strdup("0");
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;
889         }
890       if (dst < 0)
891         {
892           Put_message("You must enter a non-negative number.");
893           continue;
894         }
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;
902         }
903       break;
904     }
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,
909                           NULL, NULL)))
910     {
911       com_err(program_name, stat, " in ChangeFSGroupOrder");
912       return DM_NORMAL;
913     }
914   if ((stat = do_mr_query("add_filesys_to_fsgroup", 3, args,
915                              NULL, NULL)))
916     com_err(program_name, stat, " in ChangeFSGroupOrder");
917   return DM_NORMAL;
918 }
919
920
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
931 int GetFSAlias(int argc, char **argv)
932 {
933   struct mqelem *top;
934
935   top = GetFSInfo(ALIAS, argv[1]);
936   Put_message(" ");             /* blank line. */
937   Loop(top, (void (*)(char **))PrintFSAlias);
938   FreeQueue(top);
939   return DM_NORMAL;
940 }
941
942 /*      Function Name: CreateFSAlias
943  *      Description: Create an alias name for a filesystem
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}
947  *              from the user.  I provide type, which is well-known.
948  */
949
950 int CreateFSAlias(int argc, char **argv)
951 {
952   int stat;
953   struct mqelem *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
961   args[ALIAS_NAME] = strdup(argv[1]);
962   args[ALIAS_TYPE] = strdup(FS_ALIAS_TYPE);
963   args[ALIAS_TRANS] = strdup("*");
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
970   if (!(stat = do_mr_query("get_alias", 3, args, StoreInfo, &elem)))
971     {
972       top = elem = QueueTop(elem);
973       while (elem)
974         {
975           info = elem->q_data;
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;
980         }
981       FreeQueue(top);
982       return DM_NORMAL;
983     }
984   else if (stat != MR_NO_MATCH)
985     {
986       com_err(program_name, stat, " in CreateFSAlias.");
987       return DM_NORMAL;
988     }
989
990   args[ALIAS_TRANS] = strdup("");
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;
995
996   if ((stat = do_mr_query("add_alias", 3, args, NULL, NULL)))
997     com_err(program_name, stat, " in CreateFSAlias.");
998
999   FreeInfo(args);
1000   return DM_NORMAL;
1001 }
1002
1003 /*      Function Name: RealDeleteFSAlias
1004  *      Description: Does the real deletion work.
1005  *      Arguments: info - array of char *'s containing all useful info.
1006  *                 one_item - a Boolean that is true if only one item
1007  *                              in queue that dumped us here.
1008  *      Returns: none.
1009  */
1010
1011 void RealDeleteFSAlias(char **info, Bool one_item)
1012 {
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),
1026                               info, NULL, NULL)))
1027         com_err(program_name, stat, " filesystem alias not deleted.");
1028       else
1029         Put_message("Filesystem alias deleted.");
1030     }
1031   else
1032     Put_message("Filesystem alias not deleted.");
1033 }
1034
1035 /*      Function Name: DeleteFSAlias
1036  *      Description: Delete an alias name for a filesystem
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}
1040  *              from the user.  I provide type, which is well-known.
1041  */
1042
1043 int DeleteFSAlias(int argc, char **argv)
1044 {
1045   struct mqelem *elem = GetFSInfo(ALIAS, argv[1]);
1046   QueryLoop(elem, PrintFSAlias, RealDeleteFSAlias,
1047             "Delete the Filesystem Alias");
1048   FreeQueue(elem);
1049   return DM_NORMAL;
1050 }
1051
1052 /*      Function Name: AttachHelp
1053  *      Description: Print help info on attachmaint.
1054  *      Arguments: none
1055  *      Returns: DM_NORMAL.
1056  */
1057
1058 int AttachHelp(void)
1059 {
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);
1075 }
1076
1077 /*      Function Name: FSGroupHelp
1078  *      Description: Print help info on fsgroups.
1079  *      Arguments: none
1080  *      Returns: DM_NORMAL.
1081  */
1082
1083 int FSGroupHelp(int argc, char **argv)
1084 {
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);
1099 }
This page took 0.150224 seconds and 3 git commands to generate.