⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smbget.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	} else newpath = name;	if(newpath[0] == '/')newpath++;		/* Open local file and, if necessary, resume */	if(!send_stdout) {		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);		if(localhandle < 0) {			fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));			smbc_close(remotehandle);			return 0;		}			fstat(localhandle, &localstat);		start_offset = localstat.st_size;		if(localstat.st_size && localstat.st_size == remotestat.st_size) {			if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);			else if(!quiet)fprintf(stderr, "%s\n", path);			smbc_close(remotehandle);			close(localhandle);			return 1;		}		if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {			offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;			offset_check = localstat.st_size - RESUME_CHECK_OFFSET;			if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"				   "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",				newpath, (OFF_T_FORMAT_CAST)offset_check, 				(OFF_T_FORMAT_CAST)localstat.st_size,				(OFF_T_FORMAT_CAST)remotestat.st_size);		}		if(offset_check) { 			off_t off1, off2;			/* First, check all bytes from offset_check to offset_download */			off1 = lseek(localhandle, offset_check, SEEK_SET);			if(off1 < 0) {				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",					(OFF_T_FORMAT_CAST)offset_check, newpath);				smbc_close(remotehandle); close(localhandle);				return 0;			}			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 			if(off2 < 0) {				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",					(OFF_T_FORMAT_CAST)offset_check, newpath);				smbc_close(remotehandle); close(localhandle);				return 0;			}			if(off1 != off2) {				fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",					(OFF_T_FORMAT_CAST)off1,					(OFF_T_FORMAT_CAST)off2);				return 0;			}			if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {				fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);				smbc_close(remotehandle); close(localhandle);				return 0;			}			if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {				fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);				smbc_close(remotehandle); close(localhandle);				return 0;			}			if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {				if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", (OFF_T_FORMAT_CAST)offset_download);			} else {				fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);				smbc_close(remotehandle); close(localhandle);				return 0;			}		}	} else {		localhandle = STDOUT_FILENO;		start_offset = 0;		offset_download = 0;		offset_check = 0;	}	readbuf = SMB_MALLOC(blocksize);	/* Now, download all bytes from offset_download to the end */	for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {		ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);		if(bytesread < 0) {			fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);			smbc_close(remotehandle);			if (localhandle != STDOUT_FILENO) close(localhandle);			free(readbuf);			return 0;		}		total_bytes += bytesread;		if(write(localhandle, readbuf, bytesread) < 0) {			fprintf(stderr, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT"\n", (unsigned int)bytesread, path, (OFF_T_FORMAT_CAST)curpos);			free(readbuf);			smbc_close(remotehandle);			if (localhandle != STDOUT_FILENO) close(localhandle);			return 0;		}		if(dots)fputc('.', stderr);		else if(!quiet) {			print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);		}	}	free(readbuf);	if(dots){		fputc('\n', stderr);		printf("%s downloaded\n", path);	} else if(!quiet) {		int i;		fprintf(stderr, "\r%s", path);		if(columns) {			for(i = strlen(path); i < columns; i++) {				fputc(' ', stderr);			}		}		fputc('\n', stderr);	}	if(keep_permissions && !send_stdout) {		if(fchmod(localhandle, remotestat.st_mode) < 0) {			fprintf(stderr, "Unable to change mode of local file %s to %o\n", path, remotestat.st_mode);			smbc_close(remotehandle);			close(localhandle);			return 0;		}	}	smbc_close(remotehandle);	if (localhandle != STDOUT_FILENO) close(localhandle);	return 1;}void clean_exit(void){	char bs[100];	human_readable(total_bytes, bs, sizeof(bs));	if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs, time(NULL) - total_start_time);	exit(0);}void signal_quit(int v){	clean_exit();}int readrcfile(const char *name, const struct poptOption long_options[]){	FILE *fd = fopen(name, "r");	int lineno = 0, i;	char var[101], val[101];	char found;	int *intdata; char **stringdata;	if(!fd) {		fprintf(stderr, "Can't open RC file %s\n", name);		return 1;	}	while(!feof(fd)) {		lineno++;		if(fscanf(fd, "%100s %100s\n", var, val) < 2) {			fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);			continue;		}		found = 0;		for(i = 0; long_options[i].shortName; i++) {			if(!long_options[i].longName)continue;			if(strcmp(long_options[i].longName, var)) continue;			if(!long_options[i].arg)continue;			switch(long_options[i].argInfo) {			case POPT_ARG_NONE:				intdata = (int *)long_options[i].arg;				if(!strcmp(val, "on")) *intdata = 1;				else if(!strcmp(val, "off")) *intdata = 0;				else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);				break;			case POPT_ARG_INT:				intdata = (int *)long_options[i].arg;				*intdata = atoi(val);				break;			case POPT_ARG_STRING:				stringdata = (char **)long_options[i].arg;				*stringdata = SMB_STRDUP(val);				break;			default:				fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);				break;			}			found = 1;		}		if(!found) {			fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);		}	}	fclose(fd);	return 0;}int main(int argc, const char **argv){	int c = 0;	const char *file = NULL;	char *rcfile = NULL;	struct poptOption long_options[] = {		{"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },			{"resume", 'r', POPT_ARG_NONE, &_resume, 0, "Automatically resume aborted files" },		{"recursive", 'R',  POPT_ARG_NONE, &_recursive, 0, "Recursively download files" },		{"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },		{"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },		{"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },		{"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },		{"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },		{"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },		{"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },		{"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },		{"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },		{"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },		{"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },		{"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},		{"rcfile", 'f', POPT_ARG_STRING, NULL, 0, "Use specified rc file"},		POPT_AUTOHELP		POPT_TABLEEND	};	poptContext pc;	load_case_tables();	/* only read rcfile if it exists */	asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME"));	if(access(rcfile, F_OK) == 0) 		readrcfile(rcfile, long_options);	free(rcfile);#ifdef SIGWINCH	signal(SIGWINCH, change_columns);#endif	signal(SIGINT, signal_quit);	signal(SIGTERM, signal_quit);	pc = poptGetContext(argv[0], argc, argv, long_options, 0);	while((c = poptGetNextOpt(pc)) >= 0) {		switch(c) {		case 'f':			readrcfile(poptGetOptArg(pc), long_options);			break;		case 'a':			username = ""; password = "";			break;		}	}	if((send_stdout || outputfile) && _recursive) {		fprintf(stderr, "The -o or -O and -R options can not be used together.\n");		return 1;	}	if(outputfile && send_stdout) {		fprintf(stderr, "The -o and -O options cannot be used together.\n");		return 1;	}	if(smbc_init(get_auth_data, debuglevel) < 0) {		fprintf(stderr, "Unable to initialize libsmbclient\n");		return 1;	}	columns = get_num_cols();	total_start_time = time(NULL);	while ( (file = poptGetArg(pc)) ) {		if (!_recursive) 			return smb_download_file(file, "", _recursive, _resume, outputfile);		else 			return smb_download_dir(file, "", _resume);	}	clean_exit();	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -