Ejemplo n.º 1
0
static int list_file(direntry_t *entry, MainParam_t *mp)
{
	unsigned long size;
	int i;
	int Case;
	int r;

	wchar_t ext[4];
	wchar_t name[9];
	doscp_t *cp;

	if(!all && (entry->dir.attr & 0x6))
		return 0;

	if(concise && isSpecialW(entry->name))
		return 0;

	r=enterDirectory(entry->Dir);
	if (r)
		return ERROR_ONE;
	if (wide) {
		if(filesInDir % 5)
			putchar(' ');				
		else
			putchar('\n');
	}
	
	if(IS_DIR(entry)){
		size = 0;
	} else
		size = FILE_SIZE(&entry->dir);
	
	Case = entry->dir.Case;
	if(!(Case & (BASECASE | EXTCASE)) && 
	   mtools_ignore_short_case)
		Case |= BASECASE | EXTCASE;
	
	cp = GET_DOSCONVERT(entry->Dir);
	dos_to_wchar(cp, entry->dir.ext, ext, 3);
	if(Case & EXTCASE){
		for(i=0; i<3;i++)
			ext[i] = towlower(ext[i]);
	}
	ext[3] = '\0';
	dos_to_wchar(cp, entry->dir.name, name, 8);
	if(Case & BASECASE){
		for(i=0; i<8;i++)
			name[i] = towlower(name[i]);
	}
	name[8]='\0';
	if(wide){
		if(IS_DIR(entry))
			printf("[%s]%*s", global_shortname,
			       (int) (15 - 2 - strlen(global_shortname)), "");
		else
			printf("%-15s", global_shortname);
	} else if(!concise) {				
		char tmpBasename[4*8+1];
		char tmpExt[4*8+1];
		wchar_to_native(name,tmpBasename,8);
		wchar_to_native(ext,tmpExt,3);

		if (name[0] == ' ') 
			printf("             ");
		else if(mtools_dotted_dir)
			printf("%-12s ", global_shortname);
		else
			printf("%s %s ", tmpBasename, tmpExt);
		/* is a subdirectory */
		if(IS_DIR(entry))
			printf("<DIR>    ");
		else
			printf(" %8ld", (long) size);
		printf(" ");
		print_date(&entry->dir);
		printf("  ");
		print_time(&entry->dir);

		if(debug)
			printf(" %s %d ", tmpBasename, START(&entry->dir));
		
		if(*global_longname)
			printf(" %s", global_longname);
		printf("\n");
	} else {
		char tmp[4*MAX_VNAMELEN+1];
		wchar_to_native(entry->name,tmp,MAX_VNAMELEN);

		printf("%s/%s", dirPath, tmp);
		if(IS_DIR(entry))
			putchar('/');
		putchar('\n');
	}

	filesOnDrive++;
	filesInDir++;

	bytesOnDrive += (mt_size_t) size;
	bytesInDir += (mt_size_t) size;
	return GOT_ONE;
}
Ejemplo n.º 2
0
struct doscp_t *get_dosConvert_pass_through(struct Stream_t *Stream)
{
	return GET_DOSCONVERT(Stream->Next);
}
Ejemplo n.º 3
0
static int _mwrite_one(Stream_t *Dir,
		       char *argname,
		       char *shortname,
		       write_data_callback *cb,
		       void *arg,
		       ClashHandling_t *ch)
{
	char longname[VBUFSIZE];
	const char *dstname;
	dos_name_t dosname;
	int expanded;
	struct scan_state scan;
	clash_action ret;
	doscp_t *cp = GET_DOSCONVERT(Dir);

	expanded = 0;

	if(isSpecial(argname)) {
		fprintf(stderr, "Cannot create entry named . or ..\n");
		return -1;
	}

	if(ch->name_converter == dos_name) {
		if(shortname)
			stripspaces(shortname);
		if(argname)
			stripspaces(argname);
	}

	if(shortname){
		convert_to_shortname(cp, ch, shortname, &dosname);
		if(ch->use_longname & 1){
			/* short name mangled, treat it as a long name */
			argname = shortname;
			shortname = 0;
		}
	}

	if (argname[0] && (argname[1] == ':')) {
		/* Skip drive letter */
		dstname = argname + 2;
	} else {
		dstname = argname;
	}

	/* Copy original argument dstname to working value longname */
	strncpy(longname, dstname, VBUFSIZE-1);

	if(shortname) {
		ch->use_longname =
			convert_to_shortname(cp, ch, shortname, &dosname);
		if(strcmp(shortname, longname))
			ch->use_longname |= 1;
	} else {
		ch->use_longname =
			convert_to_shortname(cp, ch, longname, &dosname);
	}

	ch->action[0] = ch->namematch_default[0];
	ch->action[1] = ch->namematch_default[1];

	while (1) {
		switch((ret=get_slots(Dir, &dosname, longname, &scan, ch))){
			case NAMEMATCH_ERROR:
				return -1;	/* Non-file-specific error,
						 * quit */
				
			case NAMEMATCH_SKIP:
				return -1;	/* Skip file (user request or
						 * error) */

			case NAMEMATCH_PRENAME:
				ch->use_longname =
					convert_to_shortname(cp, ch,
							     longname,
							     &dosname);
				continue;
			case NAMEMATCH_RENAME:
				continue;	/* Renamed file, loop again */

			case NAMEMATCH_GREW:
				/* No collision, and not enough slots.
				 * Try to grow the directory
				 */
				if (expanded) {	/* Already tried this
						 * once, no good */
					fprintf(stderr,
						"%s: No directory slots\n",
						progname);
					return -1;
				}
				expanded = 1;
				
				if (dir_grow(Dir, scan.max_entry))
					return -1;
				continue;
			case NAMEMATCH_OVERWRITE:
			case NAMEMATCH_SUCCESS:
				return write_slots(Dir, &dosname, longname,
						   &scan, cb, arg,
						   ch->use_longname);
			default:
				fprintf(stderr,
					"Internal error: clash_action=%d\n",
					ret);
				return -1;
		}

	}
}
Ejemplo n.º 4
0
static __inline__ clash_action get_slots(Stream_t *Dir,
					 dos_name_t *dosname,
					 char *longname,
					 struct scan_state *ssp,
					 ClashHandling_t *ch)
{
	int error;
	clash_action ret;
	int match_pos=0;
	direntry_t entry;
	int isprimary;
	int no_overwrite;
	int reason;
	int pessimisticShortRename;
	doscp_t *cp = GET_DOSCONVERT(Dir);

	pessimisticShortRename = (ch->action[0] == NAMEMATCH_AUTORENAME);

	entry.Dir = Dir;
	no_overwrite = 1;
	if((is_reserved(longname,1)) ||
	   longname[strspn(longname,". ")] == '\0'){
		reason = RESERVED;
		isprimary = 1;
	} else if(contains_illegals(longname,long_illegals,1024)) {
		reason = ILLEGALS;
		isprimary = 1;
	} else if(is_reserved(dosname->base,0)) {
		reason = RESERVED;
		ch->use_longname = 1;
		isprimary = 0;
	} else if(contains_illegals(dosname->base,short_illegals,11)) {
		reason = ILLEGALS;
		ch->use_longname = 1;
		isprimary = 0;
	} else {
		reason = EXISTS;
		switch (lookupForInsert(Dir,
					&entry,
					dosname, longname, ssp,
					ch->ignore_entry,
					ch->source_entry,
					pessimisticShortRename &&
					ch->use_longname,
					ch->use_longname)) {
			case -1:
				return NAMEMATCH_ERROR;
				
			case 0:
				return NAMEMATCH_SKIP;
				/* Single-file error error or skip request */
				
			case 5:
				return NAMEMATCH_GREW;
				/* Grew directory, try again */
				
			case 6:
				return NAMEMATCH_SUCCESS; /* Success */
		}	
		match_pos = -2;
		if (ssp->longmatch > -1) {
			/* Primary Long Name Match */
#ifdef debug
			fprintf(stderr,
				"Got longmatch=%d for name %s.\n",
				longmatch, longname);
#endif			
			match_pos = ssp->longmatch;
			isprimary = 1;
		} else if ((ch->use_longname & 1) && (ssp->shortmatch != -1)) {
			/* Secondary Short Name Match */
#ifdef debug
			fprintf(stderr,
				"Got secondary short name match for name %s.\n",
				longname);
#endif

			match_pos = ssp->shortmatch;
			isprimary = 0;
		} else if (ssp->shortmatch >= 0) {
			/* Primary Short Name Match */
#ifdef debug
			fprintf(stderr,
				"Got primary short name match for name %s.\n",
				longname);
#endif
			match_pos = ssp->shortmatch;
			isprimary = 1;
		} else
			return NAMEMATCH_RENAME;

		if(match_pos > -1) {
			entry.entry = match_pos;
			dir_read(&entry, &error);
			if (error)
			    return NAMEMATCH_ERROR;
			/* if we can't overwrite, don't propose it */
			no_overwrite = (match_pos == ch->source || IS_DIR(&entry));
		}
	}
	ret = process_namematch(cp, dosname, longname,
				isprimary, ch, no_overwrite, reason);
	
	if (ret == NAMEMATCH_OVERWRITE && match_pos > -1){
		if((entry.dir.attr & 0x5) &&
		   (ask_confirmation("file is read only, overwrite anyway (y/n) ? ")))
			return NAMEMATCH_RENAME;
		/* Free up the file to be overwritten */
		if(fatFreeWithDirentry(&entry))
			return NAMEMATCH_ERROR;
		
#if 0
		if(isprimary &&
		   match_pos - ssp->match_free + 1 >= ssp->size_needed){
			/* reuse old entry and old short name for overwrite */
			ssp->free_start = match_pos - ssp->size_needed + 1;
			ssp->free_size = ssp->size_needed;
			ssp->slot = match_pos;
			ssp->got_slots = 1;
			strncpy(dosname, dir.name, 3);
			strncpy(dosname + 8, dir.ext, 3);
			return ret;
		} else
#endif
			{
			wipeEntry(&entry);
			return NAMEMATCH_RENAME;
		}
	}

	return ret;
}
Ejemplo n.º 5
0
void mlabel(int argc, char **argv, int type)
{

	char *newLabel;
	int verbose, clear, interactive, show;
	direntry_t entry;
	int result=0;
	char longname[VBUFSIZE];
	char shortname[45];
	ClashHandling_t ch;
	struct MainParam_t mp;
	Stream_t *RootDir;
	int c;
	int mangled;
	enum { SER_NONE, SER_RANDOM, SER_SET }  set_serial = SER_NONE;
	long serial = 0;
	int need_write_boot = 0;
	int have_boot = 0;
	char *eptr;
	union bootsector boot;
	Stream_t *Fs=0;
	int r;
	struct label_blk_t *labelBlock;
	int isRo=0;
	int *isRop=NULL;

	init_clash_handling(&ch);
	ch.name_converter = label_name;
	ch.ignore_entry = -2;

	verbose = 0;
	clear = 0;
	show = 0;

	if(helpFlag(argc, argv))
		usage(0);
	while ((c = getopt(argc, argv, "i:vcsnN:h")) != EOF) {
		switch (c) {
			case 'i':
				set_cmd_line_image(optarg, 0);
				break;
			case 'v':
				verbose = 1;
				break;
			case 'c':
				clear = 1;
				break;
			case 's':
				show = 1;
				break;
			case 'n':
				set_serial = SER_RANDOM;
				srandom((long)time (0));
				serial=random();
				break;
			case 'N':
				set_serial = SER_SET;
				serial = strtol(optarg, &eptr, 16);
				if(*eptr) {
					fprintf(stderr,
						"%s not a valid serial number\n",
						optarg);
					exit(1);
				}
				break;
			case 'h':
				usage(0);
			default:
				usage(1);
			}
	}

	if (argc - optind != 1 || !argv[optind][0] || argv[optind][1] != ':')
		usage(1);

	init_mp(&mp);
	newLabel = argv[optind]+2;
	if(strlen(newLabel) > VBUFSIZE) {
		fprintf(stderr, "Label too long\n");
		FREE(&RootDir);
		exit(1);
	}

	interactive = !show && !clear &&!newLabel[0] &&
		(set_serial == SER_NONE);
	if(!clear && !newLabel[0]) {
		isRop = &isRo;
	}
	RootDir = open_root_dir(argv[optind][0], isRop ? 0 : O_RDWR, isRop);
	if(isRo) {
		show = 1;
		interactive = 0;
	}	
	if(!RootDir) {
		fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]);
		exit(1);
	}

	initializeDirentry(&entry, RootDir);
	r=vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY,
		      shortname, longname);
	if (r == -2) {
		FREE(&RootDir);
		exit(1);
	}

	if(show || interactive){
		if(isNotFound(&entry))
			printf(" Volume has no label\n");
		else if (*longname)
			printf(" Volume label is %s (abbr=%s)\n",
			       longname, shortname);
		else
			printf(" Volume label is %s\n",  shortname);

	}

	/* ask for new label */
	if(interactive){
		newLabel = longname;
		fprintf(stderr,"Enter the new volume label : ");
		if(fgets(newLabel, VBUFSIZE, stdin) == NULL) {
			newLabel[0] = '\0';
			fprintf(stderr, "\n");
		}
		if(newLabel[0])
			newLabel[strlen(newLabel)-1] = '\0';
	}

	if((!show || newLabel[0]) && !isNotFound(&entry)){
		/* if we have a label, wipe it out before putting new one */
		if(interactive && newLabel[0] == '\0')
			if(ask_confirmation("Delete volume label (y/n): ")){
				FREE(&RootDir);
				exit(0);
			}
		entry.dir.attr = 0; /* for old mlabel */
		wipeEntry(&entry);
	}

	if (newLabel[0] != '\0') {
		ch.ignore_entry = 1;
		result = mwrite_one(RootDir,newLabel,0,labelit,NULL,&ch) ?
		  0 : 1;
	}

	have_boot = 0;
	if( (!show || newLabel[0]) || set_serial != SER_NONE) {
		Fs = GetFs(RootDir);
		have_boot = (force_read(Fs,boot.characters,0,sizeof(boot)) ==
			     sizeof(boot));
	}

	if(WORD_S(fatlen)) {
	    labelBlock = &boot.boot.ext.old.labelBlock;
	} else {
	    labelBlock = &boot.boot.ext.fat32.labelBlock;
	}

	if(!show || newLabel[0]){
		dos_name_t dosname;
		const char *shrtLabel;
		doscp_t *cp;
		if(!newLabel[0])
			shrtLabel = "NO NAME    ";
		else
			shrtLabel = newLabel;
		cp = GET_DOSCONVERT(Fs);
		label_name(cp, shrtLabel, verbose, &mangled, &dosname);

		if(have_boot && boot.boot.descr >= 0xf0 &&
		   labelBlock->dos4 == 0x29) {
			strncpy(labelBlock->label, dosname.base, 11);
			need_write_boot = 1;

		}
	}

	if((set_serial != SER_NONE) & have_boot) {
		if(have_boot && boot.boot.descr >= 0xf0 &&
		   labelBlock->dos4 == 0x29) {
			set_dword(labelBlock->serial, serial);	
			need_write_boot = 1;
		}
	}

	if(need_write_boot) {
		force_write(Fs, (char *)&boot, 0, sizeof(boot));
	}

	FREE(&RootDir);
	exit(result);
}
Ejemplo n.º 6
0
int fatlabel_set_label(const char* device_name, const char* new_label)
{
	if (strlen(new_label) > VBUFSIZE)
		return -1;

	/*
	 * 1. Init clash handling
	 */
	struct ClashHandling_t ch;
	init_clash_handling(&ch);
	ch.name_converter = label_name;
	ch.ignore_entry = -2;

	/*
	 * 2. Open root dir
	 */
	struct Stream_t* RootDir = fs_init(device_name, O_RDWR);

	if (RootDir)
		RootDir = OpenRoot(RootDir);

	if (!RootDir)
	{
		fprintf(stderr, "Opening root dir failed.\n");
		return -2;
	}

	/*
	 * 3. Init dir entry
	 */
	struct direntry_t entry;
	initializeDirentry(&entry, RootDir);

	/*
	 * 4. Lookup vfat
	 */
	char longname[VBUFSIZE];
	char shortname[45];
	if (vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY, shortname, longname) == -2)
	{
		fprintf(stderr, "Looking up vfat failed.\n");
		free_stream(&RootDir);
		return -3;
	}

	/*
	 * 5. Wipe existing entry.
	 */
	if (!isNotFound(&entry))
	{
		/* if we have a label, wipe it out before putting new one */
		entry.dir.attr = 0; /* for old mlabel */
		wipeEntry(&entry);
	}

	/*
	 * 6. Write new entry
	 */
	ch.ignore_entry = 1;

	/* don't try to write the label if it's empty */
	int result = strlen(new_label) ? mwrite_one(RootDir, new_label, labelit, &ch) : 0;

	/*
	 * 7. Load FAT boot record
	 */
	union bootsector boot;
	struct Stream_t* Fs = GetFs(RootDir);
	int have_boot = force_read(Fs, boot.characters, 0, sizeof(boot)) == sizeof(boot);

	struct label_blk_t* labelBlock = WORD_S(fatlen) ? &boot.boot.ext.old.labelBlock : &boot.boot.ext.fat32.labelBlock;

	/*
	 * 8. Get "dosconvert" struct
	 */
	struct dos_name_t dosname;
	struct doscp_t* cp = GET_DOSCONVERT(Fs);

	/*
	 * 9. Convert label
	 */
	int mangled = 0;
	label_name(cp, new_label, &mangled, &dosname);

	/*
	 * 10. Overwrite FAT boot record
	 */
	if (have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29)
	{
		strncpy(labelBlock->label, dosname.base, 11);
		force_write(Fs, (char *)&boot, 0, sizeof(boot));
	}

	free_stream(&RootDir);
	fs_close(Fs);

	return result;
}