From 80b8eafa7de393cbbecd220e43ecd1dbdc0a2131 Mon Sep 17 00:00:00 2001 From: Bob Zieman Date: Fri, 9 Apr 1993 07:42:40 +0000 Subject: [PATCH] initial version --- src/util/apCreateShadow.c | 686 ++++++++++++++++---------------------- 1 file changed, 285 insertions(+), 401 deletions(-) diff --git a/src/util/apCreateShadow.c b/src/util/apCreateShadow.c index 15d425c24..9a1ba1b37 100644 --- a/src/util/apCreateShadow.c +++ b/src/util/apCreateShadow.c @@ -1,6 +1,10 @@ /* * apCreateShadow.c * - todo + +changed appLoc to appList ... + + * complete rework ??? to EPICS area tools - templates etc. * * @@ -28,29 +32,25 @@ #define BUFLEN 256 #define SAME 0 -void procNoTopDirs(); -void procNoAppDirs(); -int doTopNoDirs(); -void Usage(); -void procDirEntries(); -void createLink(); -int procLink(); -int processFile(); -int dirwalk(); +static void Usage(); +static void procDirEntries(); +static void createLink(); +static void procLink(); +static void processFile(); +static void dirwalk(); +static void startFromHere(); +static void init_setup(); int errno; -int status; -int len; -struct stat stbuf; -char buffer[BUFLEN]; -char command[80]; /* system command */ -char dest_base[80]; /* dest base (root of shadow node) */ -char src_base[80]; /* current source descend root node */ -char dpath[80]; /* dest/subdir... */ -char spath[80]; /* src/subdir... */ -char xname[30]; /* program name */ -char appl_base[80]; /* root node */ -char appLocName[80];/* pathname of master appLoc file */ +static struct stat stbuf; +static char buffer[BUFLEN]; +static char dest_base[80]; /* dest base (root of shadow node) */ +static char src_base[80]; /* current source descend root node */ +static char dpath[80]; /* dest/subdir... */ +static char spath[80]; /* src/subdir... */ +static char progName[30]; /* program name */ +static char sys_top[80]; /* root node */ +static char topAppl[80];/* pathname of master appLoc file */ /**************************************************************************** @@ -65,70 +65,129 @@ main(argc, argv) int argc; char **argv; { +#if 0 char *ptr; char top_node[30]; FILE *appLoc_fp; /* user version of appLoc file */ char comLocName[80]; /* refs to all application common * nodes */ +#endif /* * intial setup */ init_setup(argc, argv); -/* - * At this point skip all top directories - * just process files and links in the top node - */ - chdir(appl_base); - doTopNoDirs(); - -/* - * descend top directories not in appList - */ - chdir(appl_base); - doTopDirsNoAppDirs(); - -/* - * Now process the application directories specified by the User's - * modified copy of the appLoc file - */ - chdir(dest_base); - if ((appLoc_fp = fopen("appLoc", "r")) == NULL) { - fprintf(stdout, "%: can't fopen %s in dest_base area\n", xname, "appLoc"); - exit(1); + if (chdir(sys_top)!=0) { + fprintf(stderr, "%s: Can't chdir\n", progName); + exit(1); } - /* descend each directory in the dest_base/appLoc file */ - while ((fgets(buffer, sizeof(buffer) + 2, appLoc_fp)) != NULL) { - len = strlen(buffer); - /* remove the N/L from input line */ - if (buffer[len - 1] == '\n') { - buffer[len - 1] = 0; - } - ptr = (char *) strrchr(buffer, '/'); - *ptr = NULL; - strcpy(top_node, "./"); - strcat(top_node, ++ptr); - strcpy(src_base, buffer); - status = chdir(src_base); - procDirEntries(top_node, src_base); - } - fclose(appLoc_fp); - sprintf(stderr, "makeApplShadow completed\n"); +/* TODO */ +/*fprintf(stderr,"%s: Creating a shadow node attached to %s",progName, xxx);*/ +/* TODO */ + startFromHere(); + + fprintf(stderr, "makeApplShadow %s\n", "completed"); return (0); } + +/**************************************************************************** +PROCLINK +****************************************************************************/ +void +procLink(name) + char *name; +{ + + char buff[MAXPATHLEN]; + int num_chars; + + /* always skip appLoc */ + if ((strcmp("./appLoc", name)) == SAME) { +printf("procLink: compare ./appLoc and %s\n",name); + exit(1); + } + if ((num_chars = readlink(name, buff, MAXPATHLEN)) < 0) { + fprintf(stderr, "procLink: FATAL ERROR - errno=%d name=%s\n", errno, name); + exit(1); + } + buff[num_chars] = NULL; + + if ((buff[0] == '/') || ((buff[0] == '.') && (buff[1] == '/'))) { + strcpy(spath, src_base); + strcat(spath, "/"); + strcat(spath, name); + } else { + strcpy(spath, buff); + } + strcpy(dpath, dest_base); + strcat(dpath, "/"); + strcat(dpath, name); + createLink(); +} +/**************************************************************************** +CREATELINK +****************************************************************************/ +void +createLink() +{ +/* + A symbolic link name2 is created to name1 (name2 is the name + of the file created, name1 is the string used in creating + the symbolic link). Either name may be an arbitrary path + name; the files need not be on the same file system. + */ + if ((symlink(spath, dpath)) != 0) { + printf("\n####################################################\n"); + printf("createLink: symlink failure: errno=%d\nspath=%s\n\tdpath=%s\n" + ,errno, spath, dpath); + printf("####################################################\n"); + } +} +/**************************************************************************** +DIRWALK applies a function to each file in a directory +****************************************************************************/ +void +dirwalk(dir, fcn) + char *dir; + void (*fcn) (); +{ + char name[MAXNAMLEN]; + struct dirent *dp; + DIR *dfd; + if ((dfd = opendir(dir)) == NULL) { + fprintf(stderr, "dirwalk: can't open %s\n", dir); + exit(1); + } + while ((dp = readdir(dfd)) != NULL) { + if (strcmp(dp->d_name, ".") == 0 + || strcmp(dp->d_name, "..") == 0) + continue; /* skip self and parent */ + if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) + fprintf(stderr, "dirwalk: name %s/%s too long\n", + dir, dp->d_name); + else { + sprintf(name, "%s/%s", dir, dp->d_name); + (*fcn) (name, dir); + } + } + closedir(dfd); +} +void +Usage() +{ + printf("\nUsage:\t%s \n", progName); + printf("\tor\n"); + printf("\t%s \n", progName); +} + /**************************************************************************** PROCESSFILE create a soft link and return ****************************************************************************/ -int -processFile(name, dir) +void +processFile(name) char *name; /* regular file */ - char *dir; /* current directory */ { - /* always skip appLoc */ - if ((strcmp("./appLoc", name)) == SAME) { - return; - } strcpy(spath, src_base); strcat(spath, "/"); strcat(spath, name); @@ -137,29 +196,182 @@ processFile(name, dir) strcat(dpath, "/"); strcat(dpath, name); createLink(); - return; +} + + +void +init_setup(argc, argv) + int argc; + char **argv; +{ + char *ptr; + int len; + FILE *origin_fp; + ptr = (char *) strrchr(argv[0], '/'); + if (ptr == 0) { + strcpy(progName, argv[0]); + } else { + strcpy(progName, ptr+1); + } +if ( argc != 2 ) { + printf("####################################################\n"); + Usage(); + printf("####################################################\n"); + exit(1); +} + + + if (*argv[1] != '/') { + printf("####################################################\n"); + printf("%s: ERROR - arg#1 must be a full pathname\n", progName); + Usage(); + printf("####################################################\n"); + exit(1); + } + +/* check to see if path points to app system area - file .topAppl exists*/ + /* application base is argv[1] */ + len = strlen(argv[1]); + strncpy(sys_top, argv[1], len); + sys_top[len] = NULL; + + strcpy(topAppl, sys_top); + strcat(topAppl, "/.topAppl"); + if ((stat(topAppl, &stbuf)) != 0) { + fprintf(stderr, "####################################################\n"); + fprintf(stderr, "%s: Can't stat file: %s\n", progName, topAppl); + fprintf(stderr, "####################################################\n"); + Usage(); + exit(1); + } + /* get shadow base */ + if ((getcwd(dest_base, 78)) == NULL) { + fprintf(stderr, "getcwd failed\n"); + exit(1); + } + strcpy(src_base, sys_top); + + /* if .applShadow doesn't exist - require touch .applShadow */ + if ((stat("./.applShadow", &stbuf)) != 0) { + fprintf(stderr, "\n"); + fprintf(stderr, "####################################################\n"); + fprintf(stderr, "%s: Can't stat file: './applShadow'\n", progName); + fprintf(stderr, "####################################################\n"); + fprintf(stderr, "If this is NOT the root of your application shadow directory,\n"); + fprintf(stderr, "then please cd to the appropriate place and try again.\n"); + fprintf(stderr, "If this IS the root your shadow directory, then create .applShadow\n"); + fprintf(stderr, "with the following command and then run %s again.\n", progName); + fprintf(stderr, " %%\n touch .applShadow\n"); + fprintf(stderr, "####################################################\n"); + exit(1); + } +/*********************** create/check an invocation marker *******************/ +/*rz*/ + if ((stat("./.applShadowOrgin", &stbuf)) != 0) { + if ((origin_fp = fopen("./.applShadowOrgin", "w")) == NULL) { + fprintf(stderr, "%s: can't fopen %s in dest_base area\n", + progName, ".applShadowOrgin"); + exit(1); + } + fprintf(origin_fp, "%s\n", sys_top); + } else { + if ((origin_fp = fopen("./.applShadowOrgin", "r")) == NULL) { + fprintf(stderr, "%s: can't fopen %s in dest_base area\n", + progName, ".applShadowOrgin"); + exit(1); + } + /* compare sys_top with buffer */ + while ((fgets(buffer, sizeof(buffer) + 2, origin_fp)) != NULL) { + len = strlen(buffer); + /* remove the N/L from input line */ + if (buffer[len - 1] == '\n') { + buffer[len - 1] = 0; + } + if ((strcmp(buffer, sys_top)) != SAME) { +fprintf(stderr, "%s: FATAL ERROR - Illegal invocation\n", progName); + fprintf(stderr, +"Your last system area was %s\n", buffer); + fprintf(stderr, +"Your new (arg1) system area is %s\n",sys_top); + exit(1); + } + } +#if 0 /* DEBUG */ + fprintf(stderr, "%s: WARNING - This is NOT the first run!\n",progName ); +#endif + } + fclose(origin_fp); + + +#if 0 /* RZ COMMENT OUT */ +/*********************** create a marker *******************/ + /* if appLoc doesn't exist - provide one */ + if ((stat("./appLoc", &stbuf)) != 0) { + /* make a copy of topAppl */ + sprintf(command, "cp %s %s\n", topAppl, "appLoc"); + status = system(command); + fprintf(stderr, "####################################################\n"); + fprintf(stderr, "I am providing you with a copy of the appLoc file\n"); + fprintf(stderr, "The appLoc file determines how many shadow applications\n"); + fprintf(stderr, "will be created in your shadow node\n"); + fprintf(stderr, "You may/should edit your copy of the appLoc file\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Once you are satisfied with the contents of your appLoc file,\n"); + fprintf(stderr, "Please invoke this program again:\n"); + fprintf(stderr, " %%\n %s/bin/%s\n", sys_top, progName); + fprintf(stderr, "####################################################\n"); + exit(1); + } +#endif /* ENDIF RZ COMMENT OUT */ +} + +/*********************************************/ +void +startFromHere() +{ + char *dir = "."; + char name[MAXNAMLEN]; + struct dirent *dp; + DIR *dfd; + if ((dfd = opendir(dir)) == NULL) { + fprintf(stderr, "dirwalk: can't open %s\n", dir); + exit(1); + } + while ((dp = readdir(dfd)) != NULL) { + if (strcmp(dp->d_name, ".") == 0 + || strcmp(dp->d_name, "..") == 0) + continue; /* skip self and parent */ + if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) + fprintf(stderr, "dirwalk: name %s/%s too long\n", + dir, dp->d_name); + else { + sprintf(name, "%s/%s", dir, dp->d_name); + procDirEntries(name); + } + } + closedir(dfd); + } /**************************************************************************** PROCDIRENTRIES process directory entries ****************************************************************************/ void -procDirEntries(name, dir) +procDirEntries(name) char *name; /* entry name */ - char *dir; /* current directory */ { char *ptr; struct stat stbuf; if (lstat(name, &stbuf) == -1) { - fprintf(stdout, "procDirEntries: can't access %s\n", name); - return; + fprintf(stderr, "procDirEntries: can't access %s\n", name); + exit(1); } if ((stbuf.st_mode & S_IFMT) == S_IFLNK) { procLink(name); return; } if ((stbuf.st_mode & S_IFMT) == S_IFREG) { - processFile(name, dir); + processFile(name); return; } if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { @@ -184,337 +396,9 @@ procDirEntries(name, dir) printf("####################################################\n"); printf("procDirEntries: Can't mkdir %s\n", dpath); printf("####################################################\n"); + exit(1); } dirwalk(name, procDirEntries); } return; } -/**************************************************************************** -PROCLINK -****************************************************************************/ -int -procLink(name) - char *name; -{ - - char buff[MAXPATHLEN]; - int num_chars; - - /* always skip appLoc */ - if ((strcmp("./appLoc", name)) == SAME) { -printf("procLink: compare ./appLoc and %s\n",name); - return; - } - if ((num_chars = readlink(name, buff, MAXPATHLEN)) < 0) { - fprintf(stdout, "procLink: FATAL ERROR - errno=%d name=%s\n", errno, name); - return; - } - buff[num_chars] = NULL; - - if ((buff[0] == '/') || ((buff[0] == '.') && (buff[1] == '/'))) { - strcpy(spath, src_base); - strcat(spath, "/"); - strcat(spath, name); - } else { - strcpy(spath, buff); - } - strcpy(dpath, dest_base); - strcat(dpath, "/"); - strcat(dpath, name); - createLink(); - return; -} -/**************************************************************************** -CREATELINK -****************************************************************************/ -void -createLink() -{ -/* - A symbolic link name2 is created to name1 (name2 is the name - of the file created, name1 is the string used in creating - the symbolic link). Either name may be an arbitrary path - name; the files need not be on the same file system. - */ - if ((symlink(spath, dpath)) != 0) { - printf("\n####################################################\n"); - printf("createLink: symlink failure: errno=%d\nspath=%s\n\tdpath=%s\n" - ,errno, spath, dpath); - printf("####################################################\n"); - } - return; -} -/**************************************************************************** -DIRWALK applies a function to each file in a directory -****************************************************************************/ -int -dirwalk(dir, fcn) - char *dir; - void (*fcn) (); -{ - char name[MAXNAMLEN]; - struct dirent *dp; - DIR *dfd; - if ((dfd = opendir(dir)) == NULL) { - fprintf(stdout, "dirwalk: can't open %s\n", dir); - return; - } - while ((dp = readdir(dfd)) != NULL) { - if (strcmp(dp->d_name, ".") == 0 - || strcmp(dp->d_name, "..") == 0) - continue; /* skip self and parent */ - if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) - fprintf(stdout, "dirwalk: name %s/%s too long\n", - dir, dp->d_name); - else { - sprintf(name, "%s/%s", dir, dp->d_name); - (*fcn) (name, dir); - } - } - closedir(dfd); -} -void -Usage() -{ - printf("\nUsage: %s \n", xname); - printf("ex: %s /\n", xname); -} - -int -doTopNoDirs() -{ - char *dir = "."; - char name[MAXNAMLEN]; - struct dirent *dp; - DIR *dfd; - if ((dfd = opendir(dir)) == NULL) { - fprintf(stdout, "dirwalk: can't open %s\n", dir); - return; - } - while ((dp = readdir(dfd)) != NULL) { - if (strcmp(dp->d_name, ".") == 0 - || strcmp(dp->d_name, "..") == 0) - continue; /* skip self and parent */ - if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) - fprintf(stdout, "dirwalk: name %s/%s too long\n", - dir, dp->d_name); - else { - sprintf(name, "%s/%s", dir, dp->d_name); - procNoTopDirs(name, dir); - } - } - closedir(dfd); - -} -int -doTopDirsNoAppDirs() -{ - char *dir = "."; - char name[MAXNAMLEN]; - struct dirent *dp; - DIR *dfd; - if ((dfd = opendir(dir)) == NULL) { - fprintf(stdout, "dirwalk: can't open %s\n", dir); - return; - } - while ((dp = readdir(dfd)) != NULL) { - if (strcmp(dp->d_name, ".") == 0 - || strcmp(dp->d_name, "..") == 0) - continue; /* skip self and parent */ - if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) - fprintf(stdout, "dirwalk: name %s/%s too long\n", - dir, dp->d_name); - else { - sprintf(name, "%s/%s", dir, dp->d_name); - procNoAppDirs(name, dir); - } - } - closedir(dfd); - -} -void -procNoTopDirs(name, dir) - char *name; /* entry name */ - char *dir; /* current directory */ -{ - struct stat stbuf; - if (lstat(name, &stbuf) == -1) { - fprintf(stdout, "procNoTopDirs: can't access %s\n", name); - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFLNK) { - procLink(name); - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFREG) { - processFile(name, dir); - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { - return; - } - return; -} - -void procNoAppDirs(name, dir) - char *name; /* entry name */ - char *dir; /* current directory */ -{ - FILE *appList; - char *ptr; - struct stat stbuf; - char appname[30]; - if (lstat(name, &stbuf) == -1) { - fprintf(stdout, "procNoAppDirs: can't access %s\n", name); - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFLNK) { - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFREG) { - return; - } - if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { - chdir(appl_base); - if ((appList = fopen("appList", "r")) == NULL) { - fprintf(stdout, "%: can't fopen %s in appl_base area\n", xname, "appList"); - exit(1); - } - /* check for match */ - while ((fgets(buffer, sizeof(buffer) + 2, appList)) != NULL) { - len = strlen(buffer); - /* remove the N/L from input line */ - if (buffer[len - 1] == '\n') { - buffer[len - 1] = 0; - } - strcpy(appname, "./"); - strcat(appname, buffer); - if ((strcmp(appname, name)) == SAME) { - fclose(appList); - return; - } - } - fclose(appList); - /* descend the directories not listed in the appl_base/appList file */ - procDirEntries(name, dir); - return; - } - return; -} - -int -init_setup(argc, argv) - int argc; - char **argv; -{ - char *ptr; - int len; - FILE *origin_fp; - ptr = (char *) strrchr(argv[0], '/'); - if (ptr == 0) { - /* no slash in program pathname */ - strcpy(xname, argv[0]); - } else { - strcpy(xname, ++ptr); - --ptr; - } - if (*argv[0] != '/') { - printf("####################################################\n"); - printf("%s: ERROR - You must invoke %s with a full pathname\n", xname, xname); - printf("####################################################\n"); - exit(1); - } - /* derive application base from argv[0] */ - *ptr = NULL; - /* remove /bin from invocation */ - ptr = (char *) strrchr(argv[0], '/'); - *ptr = NULL; - len = strlen(argv[0]); - strncpy(appl_base, argv[0], len); - appl_base[len] = NULL; - - strcpy(appLocName, appl_base); - strcat(appLocName, "/appLoc"); - if ((stat(appLocName, &stbuf)) != 0) { - fprintf(stdout, "####################################################\n"); - fprintf(stdout, "%s: Can't stat file: %s\n", xname, appLocName); - fprintf(stdout, "####################################################\n"); - Usage(); - exit(1); - } - /* get shadow base */ - if ((getcwd(dest_base, 78)) == NULL) { - fprintf(stdout, "getcwd failed\n"); - return; - } - strcpy(src_base, appl_base); - - /* if .applShadow doesn't exist - require touch .applShadow */ - if ((stat("./.applShadow", &stbuf)) != 0) { - fprintf(stdout, "\n"); - fprintf(stdout, "####################################################\n"); - fprintf(stdout, "%s: Can't stat file: './applShadow'\n", xname); - fprintf(stdout, "####################################################\n"); - fprintf(stdout, "If this is NOT the root of your application shadow directory,\n"); - fprintf(stdout, "then please cd to the appropriate place and try again.\n"); - fprintf(stdout, "If this IS the root your shadow directory, then create .applShadow\n"); - fprintf(stdout, "with the following command and then run %s again.\n", xname); - fprintf(stdout, " %%\n touch .applShadow\n"); - fprintf(stdout, "####################################################\n"); - exit(1); - } -/*********************** create/check an invocation marker *******************/ - if ((stat("./.applShadowOrgin", &stbuf)) != 0) { - if ((origin_fp = fopen("./.applShadowOrgin", "w")) == NULL) { - fprintf(stdout, "%s: can't fopen %s in dest_base area\n", - xname, ".applShadowOrgin"); - exit(1); - } - fprintf(origin_fp, "%s\n", appl_base); - } else { - if ((origin_fp = fopen("./.applShadowOrgin", "r")) == NULL) { - fprintf(stdout, "%s: can't fopen %s in dest_base area\n", - xname, ".applShadowOrgin"); - exit(1); - } - /* compare appl_base with buffer */ - while ((fgets(buffer, sizeof(buffer) + 2, origin_fp)) != NULL) { - len = strlen(buffer); - /* remove the N/L from input line */ - if (buffer[len - 1] == '\n') { - buffer[len - 1] = 0; - } - if ((strcmp(buffer, appl_base)) != SAME) { - fprintf(stdout, "%s: FATAL ERROR - Illegal invocation\n", xname); - fprintf(stdout, - "Your last invocation was from %s/bin\n", buffer); - fprintf(stdout, - "Your current invocation is from %s/bin\n",appl_base); - exit(1); - } - } -#if 0 /* DEBUG */ - fprintf(stdout, "%s: WARNING - This is NOT the first run!\n",xname ); -#endif - } - fclose(origin_fp); -/*********************** create a marker *******************/ - /* if appLoc doesn't exist - provide one */ - if ((stat("./appLoc", &stbuf)) != 0) { - /* make a copy of appLocName */ - sprintf(command, "cp %s %s\n", appLocName, "appLoc"); - status = system(command); - fprintf(stdout, "####################################################\n"); - fprintf(stdout, "I am providing you with a copy of the appLoc file\n"); - fprintf(stdout, "The appLoc file determines how many shadow applications\n"); - fprintf(stdout, "will be created in your shadow node\n"); - fprintf(stdout, "You may/should edit your copy of the appLoc file\n"); - fprintf(stdout, "\n"); - fprintf(stdout, "Once you are satisfied with the contents of your appLoc file,\n"); - fprintf(stdout, "Please invoke this program again:\n"); - fprintf(stdout, " %%\n %s/bin/%s\n", appl_base, xname); - fprintf(stdout, "####################################################\n"); - exit(1); - } - return; -}