示例#1
0
/**
 * @brief
 *      This is main function of pbs_upgrade_job process.
 */
int
main(int argc, char *argv[])
{
	DIR *dir;
	struct stat statbuf;
	struct dirent *dirent;
	char taskdir[MAXPATHLEN + 1] = {'\0'};
	char namebuf[MAXPATHLEN + 1] = {'\0'};
	char *jobfile = NULL;
	char *p;
	char *task_start;
	int fd = -1;
	int flags = 0;
	int err = 0;
	int check_flag = 0;
	int i;
	int ret;

	errno = 0;

	/* Print pbs_version and exit if --version specified */
	execution_mode(argc, argv);

	/* Parse the command line parameters */
	while (!err && ((i = getopt(argc, argv, "cf:")) != EOF)) {
		switch (i) {
			case 'c':
				check_flag = 1;
				break;
			case 'f':
				if (jobfile) {
					err = 1;
					break;
				}
				jobfile = optarg;
				break;
			default:
				err = 1;
				break;
		}
	}
	if(!jobfile)
		err = 1;
	if (err) {
		print_usage();
		return 1;
	}

	/* Ensure the tasks directory exists */
	strncpy(namebuf, jobfile, sizeof(namebuf));
	p = strrchr(namebuf, '.');
	if (!p) {
		fprintf(stderr, "Missing job file suffix");
		return 1;
	}
	if (strncmp(p, JOB_FILE_SUFFIX, strlen(JOB_FILE_SUFFIX)) != 0) {
		fprintf(stderr, "Invalid job file suffix");
		return 1;
	}
	strcpy(p, JOB_TASKDIR_SUFFIX);
	p += strlen(JOB_TASKDIR_SUFFIX);
	ret = stat(namebuf, &statbuf);
	if (ret < 0) {
		fprintf(stderr, "Failed to stat task directory %s [%s]\n",
			namebuf, errno ? strerror(errno) : "No error");
		return 1;
	}
	if (!S_ISDIR(statbuf.st_mode)) {
		fprintf(stderr, "Expected directory at %s", namebuf);
		return 1;
	}
	strncpy(taskdir, namebuf, sizeof(taskdir));
	strcat(p, "/");
	task_start = ++p;

	if (check_flag)
		flags = O_BINARY | O_RDONLY;
	else
		flags = O_BINARY | O_RDWR;

	/* Open the job file for reading */
	fd = open(jobfile, flags);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s [%s]\n", jobfile,
			errno ? strerror(errno) : "No error");
		return 1;
	}

	/* Determine the format of the file */
	ret = check_job_file(fd);
	if (ret < 0) {
		fprintf(stderr, "Unknown format: %s\n", jobfile);
		return 1;
	}
	if (check_flag) {
		printf("%d\n", ret);
		close(fd);
		return 0;
	}
	switch (ret) {
		case 12:
			break;
		case 13:
			return 0;
		default:
			fprintf(stderr, "Unsupported version %d\n", ret);
			return 1;
	}

	/* Upgrade the job file */
	ret = upgrade_job_file(fd);
	if (ret != 0) {
		fprintf(stderr, "Upgrade failed\n");
		return 1;
	}

	/* Close the job file */
	ret = close(fd);
	if (ret < 0) {
		fprintf(stderr, "Failed to close job file [%s]\n",
			errno ? strerror(errno) : "No error");
		return 1;
	}

	/* Upgrade the task files */
	dir = opendir(taskdir);
	if (!dir) {
		fprintf(stderr, "Failed open task directory [%s]\n",
			errno ? strerror(errno) : "No error");
		return 1;
	}
	while (errno = 0, (dirent = readdir(dir)) != NULL) {
		if (errno != 0) {
			fprintf(stderr, "Failed to read directory [%s]\n",
				errno ? strerror(errno) : "No error");
			return 1;
		}
		if (dirent->d_name[0] == '.')
			continue;
		strcpy(task_start, dirent->d_name);
		ret = upgrade_task_file(namebuf);
	}
	closedir(dir);

	return 0;
}
/* ---------------------------------------------------
 * M A I N L I N E
 * Check the parms are OK
 * Check the job database file indicated is OK
 * Run the report/function required
   --------------------------------------------------- */
int main(int argc, char **argv) {
   char directory_name[MAX_DIR_LEN+1];
   char job_database_name[MAX_CHAR_LEN+1];
   int utility_command = 0; /* 1 = report, 2 = sort */
   int  i, num_records;
   char *ptr;

   /* Must have the parms we need */
   if (argc < 2) {
      show_help_screen();
      exit (1);
   }

   /* This is also used by utils */
   msg_log_handle = stdout;

   /* Get all the parms from the command line */
   utility_command = 0;   /* ensure it is 0 (not set) */
   strcpy( job_database_name, "" );   /* ensure it is not set */
   i = 1;
   while (i < argc) {
      if (memcmp(argv[i], "-d", 2) == 0) {      /* directory flag */
         i++;
         /* Get the directory, and ensure it has a trailing / on it */
         strncpy( directory_name, argv[i], MAX_DIR_LEN );
         ptr = (char *)&directory_name;
         ptr = ptr + (strlen(directory_name) - 1);
         if (*ptr != '/') {
            strcat( directory_name, "/" );
         }
         snprintf( job_database_name, MAX_CHAR_LEN, "%sjob_details.dbs", directory_name );
         i++;
      } else if (memcmp(argv[i], "-c", 2) == 0) { /* command flag */
         i++;
         /* What was the command */
         if (memcmp(argv[i], "execreport", 10) == 0) {        /* run exectime sorted report */
             utility_command = 1;
         } else if (memcmp(argv[i], "sortdbs", 7) == 0) {  /* sort database */
             utility_command = 2;
         } else if (memcmp(argv[i], "deletelist", 10) == 0) {  /* show recs pending delete */
             utility_command = 3;
         } else if (memcmp(argv[i], "namereport", 10) == 0) {  /* run jobname sorted report */
             utility_command = 4;
         } else if (memcmp(argv[i], "resyncdbs", 9) == 0) {  /* resync times in job dbs */
             utility_command = 5;
         } else {                                        /* illegal cmd, show help */
            show_help_screen();
            exit (1);
         }
         i++;
      } else {                                  /* illegal flag, show help */
         show_help_screen();
         exit (1);
      }
   }


   /* we must have a command and a job database name */
   if ((utility_command == 0) || (strlen(job_database_name) == 0)) {
      show_help_screen();
      exit (1);
   }

   /* The job file must be legal */
   if ((num_records = check_job_file( job_database_name )) == 0) {
      exit (1);
   }

   /* Execute the required commands */
   if (utility_command == 1) { produce_execorder_report( (char *)&job_database_name, num_records ); }
   else if (utility_command == 2) { sort_the_job_datafile( (char *)&job_database_name, num_records ); }
   else if (utility_command == 3) { produce_deleted_jobs_report( (char *)&job_database_name ); }
   else if (utility_command == 4) { produce_jobname_report( (char *)&job_database_name, num_records ); }
   else if (utility_command == 5) { resync_the_job_datafile( (char *)&job_database_name, num_records ); }
   else { show_help_screen(); }
   exit( 0 );
} /* end main */