예제 #1
0
파일: mktape.c 프로젝트: casualfish/mhvtl
int main(int argc, char *argv[])
{
	unsigned char sam_stat;
	char *progname = argv[0];
	char *pcl = NULL;
	char *mediaType = NULL;
	char *mediaCapacity = NULL;
	char *density = NULL;
	char *lib = NULL;
	uint64_t size;
	int libno;
	struct stat statb;
	struct passwd *pw;

	if (sizeof(struct MAM) != 1024) {
		printf("Structure of MAM incorrect size: %d\n",
						(int)sizeof(struct MAM));
		exit(2);
	}

	if (argc < 2) {
		usage(progname);
		exit(1);
	}

	while(argc > 0) {
		if (argv[0][0] == '-') {
			switch (argv[0][1]) {
			case 'd':
				if (argc > 1)
					density = argv[1];
				break;
			case 'l':
				if (argc > 1) {
					lib = argv[1];
				} else {
					puts("    More args needed for -l\n");
					exit(1);
				}
				break;
			case 'm':
				if (argc > 1) {
					pcl = argv[1];
				} else {
					puts("    More args needed for -m\n");
					exit(1);
				}
				break;
			case 's':
				if (argc > 1) {
					mediaCapacity = argv[1];
				} else {
					puts("    More args needed for -s\n");
					exit(1);
				}
				break;
			case 't':
				if (argc > 1) {
					mediaType = argv[1];
				} else {
					puts("    More args needed for -t\n");
					exit(1);
				}
				break;
			case 'V':
				printf("%s: version %s\n%s\n\n",
						progname, MHVTL_VERSION,
						(char *)largefile_support);
				break;
			case 'v':
				verbose++;
				break;
			}
		}
		argv++;
		argc--;
	}

	if (pcl == NULL) {
		printf("Please supply a barcode (-b barcode)\n\n");
		usage(progname);
		exit(1);
	}
	if (mediaCapacity == NULL) {
		printf("Please supply media capacity (-s xx)\n\n");
		usage(progname);
		exit(1);
	}
	if (mediaType == NULL) {
		printf("Please supply cart type (-t data|clean|WORM)\n\n");
		usage(progname);
		exit(1);
	}
	if (lib == NULL) {
		printf("Please supply Library number (-l xx)\n\n");
		usage(progname);
		exit(1);
	}
	if (density == NULL) {
		printf("Please supply media density (-d xx)\n\n");
		usage(progname);
		exit(1);
	}

	sscanf(mediaCapacity, "%" PRId64, &size);
	if (size == 0)
		size = 8000;

	sscanf(lib, "%d", &libno);
	if (!libno) {
		printf("Invalid library number\n");
		exit(1);
	}

	find_media_home_directory(home_directory, libno);

	if (strlen(pcl) > MAX_BARCODE_LEN) {
		printf("Max barcode length (%d) exceeded\n\n", MAX_BARCODE_LEN);
		usage(progname);
		exit(1);
	}

	pw = getpwnam(USR);	/* Find UID for user 'vtl' */

	/* Verify that the MHVTL home directory exists. */
	if (stat(home_directory, &statb) < 0 && errno == ENOENT) {
		umask(0007);
		if (mkdir(home_directory, 02770) < 0) {
			printf("Cannot create PCL %s, directory %s:"
				"Doesn't exist and cannot be created\n",
						pcl, home_directory);
			exit(1);
		}
	}

	/* Don't really care if this fails or not..
	 * But lets try anyway
	 */
	if (chown(home_directory, pw->pw_uid, pw->pw_gid))
		;

	/* Initialize the contents of the MAM to be used for the new PCL. */
	memset((uint8_t *)&mam, 0, sizeof(mam));

	mam.tape_fmt_version = TAPE_FMT_VERSION;
	mam.mam_fmt_version = MAM_VERSION;
	put_unaligned_be64(size * 1048576, &mam.max_capacity);
	put_unaligned_be64(size * 1048576, &mam.remaining_capacity);
	put_unaligned_be64(sizeof(mam.pad), &mam.MAMSpaceRemaining);

	memcpy(&mam.MediumManufacturer, "linuxVTL", 8);
	memcpy(&mam.ApplicationVendor, "vtl-1.4 ", 8);
	sprintf((char *)mam.ApplicationVersion, "%d", TAPE_FMT_VERSION);

	if (! strncmp("clean", mediaType, 5)) {
		mam.MediumType = MEDIA_TYPE_CLEAN;	/* Cleaning cart */
		mam.MediumTypeInformation = 20;		/* Max cleaning loads */
	} else if (! strncmp("WORM", mediaType, 4)) {
		mam.MediumType = MEDIA_TYPE_WORM;	/* WORM cart */
	} else {
		mam.MediumType = MEDIA_TYPE_DATA;	/* Normal data cart */
	}
	set_media_params(&mam, density);

	sprintf((char *)mam.MediumSerialNumber, "%s_%d", pcl, (int)time(NULL));
	sprintf((char *)mam.MediumManufactureDate, "%d", (int)time(NULL));
	sprintf((char *)mam.Barcode, "%-31s", pcl);

	/* Create the PCL using the initialized MAM. */

	exit(create_tape(pcl, &mam, &sam_stat));
}
예제 #2
0
파일: edit_tape.c 프로젝트: eskatos/mhvtl
int main(int argc, char *argv[])
{
	unsigned char sam_stat;
	char *progname = argv[0];
	char *pcl = NULL;
	char *mediaType = NULL;
	char *mediaCapacity = NULL;
	char *density = NULL;
	uint64_t size;
	struct MAM new_mam;
	char *lib = NULL;
	int libno = 0;
	int indx;
	int rc;
	char *config = MHVTL_CONFIG_PATH"/device.conf";
	FILE *conf;
	char *b;	/* Read from file into this buffer */
	char *s;	/* Somewhere for sscanf to store results */

	if (sizeof(struct MAM) != 1024) {
		printf("Structure of MAM incorrect size: %d\n",
						(int)sizeof(struct MAM));
		exit(2);
	}

	if (argc < 2) {
		usage(progname);
		exit(1);
	}

	debug = 0;
	my_id = 0;
	verbose = 0;
	wp = 0;

	while (argc > 0) {
		if (argv[0][0] == '-') {
			switch (argv[0][1]) {
			case 'd':
				if (argc > 1)
					density = argv[1];
				break;
			case 'l':
				if (argc > 1) {
					lib = argv[1];
				} else {
					puts("    More args needed for -l\n");
					exit(1);
				}
				break;
			case 'm':
				if (argc > 1) {
					pcl = argv[1];
				} else {
					puts("    More args needed for -m\n");
					exit(1);
				}
				break;
			case 's':
				if (argc > 1) {
					mediaCapacity = argv[1];
				} else {
					puts("    More args needed for -s\n");
					exit(1);
				}
				break;
			case 't':
				if (argc > 1) {
					mediaType = argv[1];
				} else {
					puts("    More args needed for -t\n");
					exit(1);
				}
				break;
			case 'V':
				printf("%s: version %s\n%s\n\n",
						progname, MHVTL_VERSION,
						(char *)largefile_support);
				break;
			case 'v':
				verbose++;
				break;
			case 'w':
				if (argc > 1) {
					if (!strncasecmp("yes", argv[1], 3))
						wp = WRITE_PROTECT_ON;
					else if (!strncasecmp("on", argv[1], 3))
						wp = WRITE_PROTECT_ON;
					else
						wp = WRITE_PROTECT_OFF;
				} else {
					puts("    More args needed for -m\n");
					exit(1);
				}
				break;
			}
		}
		argv++;
		argc--;
	}

	if (pcl == NULL) {
		printf("Please supply a barcode (-b barcode)\n\n");
		usage(progname);
		exit(1);
	}

	conf = fopen(config , "r");
	if (!conf) {
		printf("Can not open config file %s : %s", config,
					strerror(errno));
		perror("Can not open config file");
		exit(1);
	}
	s = zalloc(MALLOC_SZ);
	if (!s) {
		perror("Could not allocate memory");
		exit(1);
	}
	b = zalloc(MALLOC_SZ);
	if (!b) {
		perror("Could not allocate memory");
		exit(1);
	}

	rc = ENOENT;

	if (lib) {
		sscanf(lib, "%d", &libno);
		printf("Looking for PCL: %s in library %d\n", pcl, libno);
		find_media_home_directory(home_directory, libno);
		rc = load_tape(pcl, &sam_stat);
	} else { /* Walk thru all defined libraries looking for media */
		while (readline(b, MALLOC_SZ, conf) != NULL) {
			if (b[0] == '#')	/* Ignore comments */
				continue;
			/* If found a library: Attempt to load media
			 * Break out of loop if found. Otherwise try next lib.
			 */
			if (sscanf(b, "Library: %d CHANNEL:", &indx)) {
				find_media_home_directory(home_directory, indx);
				rc = load_tape(pcl, &sam_stat);
				if (!rc)
					break;
			}
		}
	}

	fclose(conf);
	free(s);
	free(b);

	if (rc) {
		fprintf(stderr, "PCL %s cannot be dumped, "
				"load_tape() returned %d\n",
					pcl, rc);
		exit(1);
	}

	/* Copy media MAM into temp location */
	memcpy(&new_mam, &mam, sizeof(mam));

	size = 0L;
	if (mediaCapacity) {
		sscanf(mediaCapacity, "%" PRId64, &size);
		printf("New capacity for %s: %ldMB\n",
					pcl, (unsigned long)size);
	}

	if (mediaType) {
		if (!strncasecmp("clean", mediaType, 5)) {
			MHVTL_DBG(1, "Setting media type to CLEAN");
			new_mam.MediumType = MEDIA_TYPE_CLEAN;
			new_mam.MediumTypeInformation = 20;
		} else if (!strncasecmp("data", mediaType, 4)) {
			MHVTL_DBG(1, "Setting media type to DATA");
			new_mam.MediumType = MEDIA_TYPE_DATA;
		} else if (!strncasecmp("null", mediaType, 4)) {
			MHVTL_DBG(1, "Setting media type to NULL");
			new_mam.MediumType = MEDIA_TYPE_NULL;
		} else if (!strncasecmp("WORM", mediaType, 4)) {
			MHVTL_DBG(1, "Setting media type to NULL");
			new_mam.MediumType = MEDIA_TYPE_WORM;
		} else {
			printf("Unknown media type: %s\n", mediaType);
			usage(progname);
			exit(1);
		}
	}
	if (density) {
		printf("Setting density to %s\n", density);
		if (set_media_params(&new_mam, density)) {
			printf("Could not determine media density: %s\n",
					density);
			unload_tape(&sam_stat);
			exit(1);
		}
	}
	if (size) {
		put_unaligned_be64(size * 1048576, &new_mam.max_capacity);
		/* This will set incorrect value to start with but next media
		 * Usage, this will be recalculated correctly
		 */
		put_unaligned_be64(size * 1048576, &new_mam.remaining_capacity);
	}
	switch (wp) {
	case WRITE_PROTECT_ON:
		new_mam.Flags |= MAM_FLAGS_MEDIA_WRITE_PROTECT;
		printf("Setting write-protect for %s\n", pcl);
		break;
	case WRITE_PROTECT_OFF:
		new_mam.Flags &= ~MAM_FLAGS_MEDIA_WRITE_PROTECT;
		printf("Turning off write-protect for %s\n", pcl);
		break;
	}

	put_unaligned_be64(sizeof(mam.pad), &new_mam.MAMSpaceRemaining);

	memcpy(&mam, &new_mam, sizeof(mam));
	rewriteMAM(&sam_stat);
	unload_tape(&sam_stat);

	exit(0);
}