Example #1
0
int mydc_open(char *path, int flag, int mode, afs_uint64 size)
{
    char real[256];
    int code;
    int fd;
    code = real_path(path, &real, 256);
    if (code)
        return 0;
    fd = dc_open(real, flag, mode);
    return fd;
}
Example #2
0
int open64(const char *path, int flags,...)
{
	mode_t mode = 0;
	va_list    args;

	if (flags & O_CREAT) {
		va_start(args, flags);
		mode = va_arg(args, mode_t);
		va_end(args);
	}

	dc_debug(DC_TRACE, "Running preloaded open64 for %s", path);
	return dc_open(path, flags, mode);
}
Example #3
0
int main(int argc, char *argv[])
{

	int  src, dest;
	struct stat64 sbuf, sbuf2;
	time_t copy_time;
	off64_t size=0, total_size;
	size_t buffer_size = DEFAULT_BUFFER; /* transfer buffer size */
	int rc ;
	char filename[MAXPATHLEN],*inpfile, *outfile;
	char formatted_rate[12], formatted_size[12];
	char extraOption[MAXPATHLEN];
	char allocSpaceOption[MAXPATHLEN];
	char *cp ;
	int c;
	int overwrite = 1;
	int isStdin = 0;
	mode_t mode = 0666;
	char *firstP, *lastP;
	unsigned short first_port, last_port;
	int stage = 0;
	int stagetime = 0;
	int unsafeWrite = 0;
	char *stagelocation = NULL;
	int ahead = 0;
	size_t ra_buffer_size = 1048570L;
	int doCheckSum = 1;

	/* for getopt */
	extern char *optarg;
	extern int optind;

	if (argc < 3) {
		usage();
	}

	extraOption[0] = '\0';
	allocSpaceOption[0] = '\0';

	if( getenv("DCACHE_SHOW_PROGRESS") != NULL) {
		is_feedback_enabled = 1;
	}

	/* FIXME removing the DC_LOCAL_CACHE_BUFFER environment
	 * variable vetos dcap's use of the lcb (the local cache).
	 * This is an ugly work-around needed because the current lcb
	 * code gives terrible performance when the client streams
	 * data in large chunks.  Rather than rewrite LCB, we
	 * introduce this as a "temporary" work-around.
	 *
	 * Although clients should tune their software for their
	 * access patterns, this is "impossible" (or at least
	 * unlikely); therefore LCB should be rewritten to provide
	 * better performance in this case.
	 */
	unsetenv("DC_LOCAL_CACHE_BUFFER");

	while( (c = getopt(argc, argv, "Ad:o:h:iX:Pt:l:aB:b:up:T:r:s:w:cC:H")) != EOF) {

		switch(c) {
			case 'd':
				dc_setStrDebugLevel(optarg);
				break;
			case 'o':
				dc_setOpenTimeout(atol(optarg));
				break;
			case 'h':
				dc_setReplyHostName(optarg);
				break;
			case 'i':
				overwrite = 0;
				break;
			case 'P':
				stage = 1;
				break;
			case 't' :
				stagetime = atoi(optarg);
				break;
			case 'l' :
				stagelocation = optarg;
				break;
			case 'B' :
				buffer_size = atol(optarg);
				break;
			case 'a' :
				ahead = 1;
				break;
			case 'b' :
				ra_buffer_size = atol(optarg);
				break;
			case 'X':
				dc_snaprintf(extraOption, sizeof(extraOption), " %s", optarg);
				break;
			case 'u':
				unsafeWrite = 1;
				break;
			case 'p':
				lastP = strchr(optarg, ':');
				if( lastP == NULL ) {
				    first_port = atoi(optarg);
					last_port = first_port;
				}else{
				    firstP = optarg; /*just to be simple */
					lastP[0] = '\0';

				    first_port = atoi(firstP);
					last_port = atoi(lastP +1);

				}

				dc_setCallbackPortRange(first_port, last_port);
				break;
			case 'T':
				dc_setTunnel(optarg);
				break;
			case 'r':
				dc_setTCPReceiveBuffer( atoi(optarg) );
				break;
			case 's':
				dc_setTCPSendBuffer( atoi(optarg) );
				break;
			case 'w':
				dc_setTunnelType(optarg);
				break;
			case 'c':
				doCheckSum = 0;
				break;
			case 'A':
				dc_setClientActive();
				break;
			case 'C':
				dc_setCloseTimeout(atoi(optarg));
				break;
		        case 'H':
				is_feedback_enabled=1;
				break;
			case '?':
				usage();

		}
	}

	if(((argc - optind) != 2) && (!stage)) {
		usage();
	}

#ifndef WIN32
	dcap_signal();
#endif

	inpfile = argv[optind];
	if(stage) {
		dc_setExtraOption(extraOption);
		if ( (rc = dc_stage(inpfile, stagetime, stagelocation)) < 0 ) {
			dc_perror("dc_stage fail");
			rc = -1;
		}
		return rc;
	}

	outfile = argv[optind+1];

#ifndef WIN32
	if(strcmp(inpfile, "-") == 0) {
		isStdin = 1;
		src = fileno(stdin);
		inpfile = strdup("/dev/stdin");
	}

	if(strcmp(outfile, "-") == 0) {
		outfile = strdup("/dev/stdout");
	}
#endif /* WIN32 */

	if(!isStdin) {
		dc_setExtraOption(extraOption);
		rc = dc_stat64(inpfile, &sbuf);
		if ( (rc == 0) && ( S_ISDIR(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) ) {
			fprintf(stderr,"file %s: Not a regular file\n",inpfile);
			return -1 ;
		}

		if( rc == 0 ) {
			/* if file do not exist it can be a url, and
				dc_open will handle this */
			mode = sbuf.st_mode & 0777;
			/* tell to pool how many bytes we want to write */
#ifdef WIN32
			dc_snaprintf(allocSpaceOption, sizeof(allocSpaceOption),
			             " -alloc-size=%lld", (__int64)sbuf.st_size);
#else
			dc_snaprintf(allocSpaceOption, sizeof(allocSpaceOption),
			             " -alloc-size=%lld", (long long)sbuf.st_size);
#endif
		}
		total_size = sbuf.st_size;
	} else {
		total_size = SIZE_FOR_UNKNOWN_TRANSFER_LENGTH;
	}

	dc_setExtraOption(extraOption);
	if ( dc_stat64( outfile, &sbuf2) == 0 &&  S_ISDIR(sbuf2.st_mode) ) {
		if ( (cp = strrchr(inpfile,PATH_SEPARATOR))  != NULL  ) {
			cp++;
		}else{
			cp = inpfile;
		}
		sprintf(filename, "%s%c%s", outfile, PATH_SEPARATOR, cp);
	}else{
		strcpy(filename,outfile) ;
	}

	dc_setExtraOption(extraOption);
	if((!overwrite) && (dc_access(filename, F_OK) == 0)) {
		fprintf(stderr, "%s: Skipping existing file %s.\n", argv[0], filename);
		return 0;
	}

	errno = 0 ;

	if(!isStdin) {
		dc_setExtraOption(extraOption);
		src = dc_open(inpfile,O_RDONLY | O_BINARY );
		if (src < 0) {
			dc_perror("Can't open source file");
			return -1;
		}
	}

	if(!ahead || (ra_buffer_size <= buffer_size)) {
		dc_noBuffering(src);
	}else{
		dc_setBufferSize(src,ra_buffer_size);
	}

	errno = 0 ;

#ifdef WIN32
	mode = _S_IWRITE ;
#endif /* WIN32 */

	dc_setExtraOption(extraOption);
	dc_setExtraOption(allocSpaceOption);
	dest = dc_open( filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, mode|S_IWUSR);
	if (dest < 0) {
		dc_perror("Can't open destination file");
		return -1;
	}

	if(unsafeWrite) {
		dc_unsafeWrite(dest);
	}

	if( ! doCheckSum ) {
		dc_noCheckSum(dest);
	}

	elapsed_transfer_time( ett_set);
	rc = copyfile(src, dest, buffer_size, &size, total_size);

	if (dc_close(src) < 0) {
		perror("Failed to close source file");
		rc = -1;
	}

	if (dc_close(dest) < 0) {
		perror("Failed to close destination file");
		dc_stat64( outfile, &sbuf2);
		mode = sbuf2.st_mode & S_IFMT;
		if (mode == S_IFREG) dc_unlink(outfile);
		rc = -1;
	}

	if (rc != -1 )  {
		copy_time = elapsed_transfer_time(ett_measure);
		dc_bytes_as_size(formatted_size, size);
		fprintf(stderr,"%ld bytes (%s) in %lu seconds", size, formatted_size, copy_time);
		if ( copy_time > 0) {
			dc_bytes_as_size(formatted_rate, (double)size / copy_time);
			fprintf(stderr," (%s/s)\n", formatted_rate);
		}else{
			fprintf(stderr,"\n");
		}
	}else{
		fprintf(stderr,"dccp failed.\n");

		/* remove destination if copy failed */
		dc_stat64( outfile, &sbuf2);
		mode = sbuf2.st_mode & S_IFMT;
		if (mode == S_IFREG) dc_unlink(outfile);
	}

	return rc;
}
Example #4
0
int dc_feof(FILE *fp)
{
	int     rc 	;
	struct vsp_node *node;

	node = get_vsp_node(FILE_NO(fp));
	if( node == NULL ) {
		return system_feof(fp);
	}

#if defined(__linux__) || defined(__GNU__) || defined(__FreeBSD_kernel__) || defined(__CYGWIN__)
	if ( ((FILE *)fp)->_flags & _IO_EOF_SEEN ) {
#else
	if ( ((FILE *)fp)->_flag & _IOEOF ) {
#endif
		rc = 1 ;
	}else {
		rc = 0 ;
	}

	m_unlock(&node->mux);
	return rc ;

}

FILE   *dc_fopen64(const char *file, const char *mode);

FILE   *dc_fopen(const char *file, const char *mode)
{
	return dc_fopen64(file, mode);
}

FILE   *dc_fopen64(const char *file, const char *mode)
{
	int fd, rw, oflags ;
	FILE *fp;

	if( isPnfs( file )  || isUrl(file) ) {

		rw= ( mode[1] == '+' ) ;
		switch(*mode) {
			case 'a':
				oflags= O_APPEND | O_CREAT | ( rw ? O_RDWR : O_WRONLY ) ;
				break ;
			case 'r':
				oflags= rw ? O_RDWR : O_RDONLY ;
				break ;
			case 'w':
				oflags= O_TRUNC | O_CREAT | ( rw ? O_RDWR : O_WRONLY ) ;
				break ;
			default:
				return NULL ;
		}


		fp = (FILE *)malloc( sizeof(FILE) );
		if( fp == NULL ) {
			return NULL;
		}

		/* break FILE chain */
	#if defined(__linux__) || defined(__GNU__) || defined(__FreeBSD_kernel__)
		fp->_chain = NULL;
		fp->_IO_write_ptr = NULL;
		fp->_IO_write_base = NULL;
		fp->_lock = NULL;
		fp->_flags = 0;
	#else
		fp->_flag = 0;
	#endif
		fd = dc_open(file,oflags, 0644) ;
		if ( fd < 0 ) {
			free(fp);
			return NULL ;
		}

		FILE_NO(fp) = fd;

	} else {
		dc_debug(DC_TRACE, "Running system native fopen [%s, %s]", file, mode);
		fp = system_fopen64( file, mode );
	}

	return fp;
}
Example #5
0
File: dcsuck.c Project: ic-hep/emi3
int main(int argc, char *argv[])
{

	int  src, dest, c;
	struct stat64 sbuf;
	ssize_t data_len;
	int rc;
	char extraOption[MAXPATHLEN];



	if( argc < 3 ) {
		fprintf(stderr,"usage: %s <path in dcache> <local path>\n",argv[0]);
		exit(1);
	}


	extraOption[0] = '\0';

	while( (c = getopt(argc, argv, "Ad:o:h:iX:Pt:l:aB:b:up:T:r:s:w:c")) != EOF) {

		switch(c) {
			case 'd':
				dc_setStrDebugLevel(optarg);
				break;
			case 'X':
				dc_snaprintf(extraOption, sizeof(extraOption),
				             " %s", optarg);
				break;
		}
	}


	rc = dc_stat64(argv[optind], &sbuf);

	if ( (rc == 0) && ( S_ISDIR(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) ) {
		fprintf(stderr,"file %s: Not a regular file\n",argv[optind]);
		return -1 ;
	}

	rc = 0;
	dc_setExtraOption(extraOption);
	src = dc_open(argv[optind], O_RDONLY );
	if( src < 0 ) {
		dc_perror("Can't open source file");
		return -1;
	}
	dest = dc_open( argv[optind+1], O_WRONLY|O_CREAT|O_TRUNC, (sbuf.st_mode & 0777) | S_IWUSR);
	if( dest < 0 ) {
		dc_perror("Can't open destination file");
		dc_close(src);
		return -2;
	}

	data_len = dc_readTo(src, dest, sbuf.st_size);
	if( data_len != sbuf.st_size) {
		fprintf(stderr,"recived data (%lld) .NE. file size(%lld) \n", data_len, sbuf.st_size);
		rc = -3;
	}

	dc_close(src);
	close(dest);

	return rc;
}
Example #6
0
int main(int argc, char ** argv)
{
	char*         cmd = NULL;
	dc_context_t* context = dc_context_new(receive_event, NULL, "CLI");
	int           stresstest_only = 0;

	dc_cmdline_skip_auth(context); /* disable the need to enter the command `auth <password>` for all mailboxes. */

	/* open database from the commandline (if omitted, it can be opened using the `open`-command) */
	if (argc == 2) {
		if (strcmp(argv[1], "--stress")==0) {
			stresstest_only = 1;
		}
		else if (!dc_open(context, argv[1], NULL)) {
			printf("ERROR: Cannot open %s.\n", argv[1]);
		}
	}
	else if (argc != 1) {
		printf("ERROR: Bad arguments\n");
	}

	s_do_log_info = 0;
	stress_functions(context);
	s_do_log_info = 1;


	if (stresstest_only) {
		return 0;
	}

	printf("Delta Chat Core is awaiting your commands.\n");

	/* wait for command */
	while (1)
	{
		/* read command */
		const char* cmdline = read_cmd();
		free(cmd);
		cmd = dc_strdup(cmdline);
		char* arg1 = strchr(cmd, ' ');
		if (arg1) { *arg1 = 0; arg1++; }

		if (strcmp(cmd, "connect")==0)
		{
			start_threads(context);
		}
		else if (strcmp(cmd, "disconnect")==0)
		{
			stop_threads(context);
		}
		else if (strcmp(cmd, "smtp-jobs")==0)
		{
			if (run_threads) {
				printf("smtp-jobs are already running in a thread.\n");
			}
			else {
				dc_perform_smtp_jobs(context);
			}
		}
		else if (strcmp(cmd, "imap-jobs")==0)
		{
			if (run_threads) {
				printf("imap-jobs are already running in a thread.\n");
			}
			else {
				dc_perform_imap_jobs(context);
			}
		}
		else if (strcmp(cmd, "configure")==0)
		{
			start_threads(context);
			dc_configure(context);
		}
		else if (strcmp(cmd, "oauth2")==0)
		{
			char* addr = dc_get_config(context, "addr");
			if (addr==NULL || addr[0]==0) {
				printf("oauth2: set addr first.\n");
			}
			else {
				char* oauth2_url = dc_get_oauth2_url(context, addr,
					"chat.delta:/com.b44t.messenger");
				if (oauth2_url==NULL) {
					printf("OAuth2 not available for %s.\n", addr);
				}
				else {
					printf("Open the following url, "
						"set mail_pw to the generated token "
						"and server_flags to 2:\n%s\n", oauth2_url);
				}
				free(oauth2_url);
			}
			free(addr);
		}
		else if (strcmp(cmd, "clear")==0)
		{
			printf("\n\n\n\n"); /* insert some blank lines to visualize the break in the buffer */
			printf("\e[1;1H\e[2J"); /* should work on ANSI terminals and on Windows 10. If not, well, then not. */
		}
		else if (strcmp(cmd, "getqr")==0 || strcmp(cmd, "getbadqr")==0)