示例#1
0
/**
 * returns 0 on success, or an errno value on failure.
 * errno values include ENOENT if the parent folder doesn't exist,
 * plus the errno values set by tr_mkdirp() and open().
 */
static int
TrOpenFile( int          i,
            const char * folder,
            const char * torrentFile,
            int          doWrite,
            int          doPreallocate,
            uint64_t     desiredFileSize )
{
    struct tr_openfile * file = &gFd->open[i];
    int                  flags;
    char               * filename;
    struct stat          sb;
    int                  alreadyExisted;

    /* confirm the parent folder exists */
    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
        return ENOENT;

    /* create subfolders, if any */
    filename = tr_buildPath( folder, torrentFile, NULL );
    if( doWrite )
    {
        char * tmp = tr_dirname( filename );
        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
        tr_free( tmp );
        if( err ) {
            tr_free( filename );
            return err;
        }
    }

    alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );

    if( doWrite && !alreadyExisted && doPreallocate )
        if( preallocateFile( filename, desiredFileSize ) )
            tr_inf( _( "Preallocated file \"%s\"" ), filename );
    
    /* open the file */
    flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
#ifdef O_LARGEFILE
    flags |= O_LARGEFILE;
#endif
#ifdef WIN32
    flags |= O_BINARY;
#endif
    file->fd = open( filename, flags, 0666 );
    if( file->fd == -1 )
    {
        const int err = errno;
        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename,
               tr_strerror( err ) );
        tr_free( filename );
        return err;
    }

    tr_free( filename );
    return 0;
}
示例#2
0
文件: pagepool.c 项目: bigbes/btree
int pool_init_new(struct PagePool *pp, char *name, uint16_t page_size,
	          pageno_t pool_size, size_t cache_size) {
	pooli_init(pp, name, page_size, pool_size, cache_size);

	pp->fd = open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
	check(pp->fd != -1, "Can't open file '%s' for read/write", name);
	check(preallocateFile(pp->fd, pool_size) != -1, "Can't preallocate file '%s'", name);

	bitmask_init(pp);
	return 0;
error:
	exit(-1);
}
示例#3
0
void SharedFile::preallocateShmMemory()
{
    assert(_file);
    assert(_region);
    if (!_isPreallocate ||
        _file->get_mode() == boost::interprocess::read_only) {
        return;
    }

    uint64_t regionSize = getSize();
    assert(regionSize>0);
    const std::string& name = getName();
    assert(!name.empty());
    assert(name == _file->get_name());

    preallocateFile(name, regionSize);
}
示例#4
0
void SharedMemory::preallocateShmMemory()
{
    assert(_shm);
    assert(_region);
    if (!_isPreallocate ||
        _shm->get_mode() == boost::interprocess::read_only) {
        return;
    }

    uint64_t regionSize = getSize();
    assert(regionSize>0);

    // Shm objects are files in /dev/shm (at least on Linux)
    // We preallocate memory by preallocating space for the shm file.
    std::string path("/dev/shm/");
    const std::string& name = getName();
    assert(!name.empty());
    assert(name == _shm->get_name());
    path += name;

    preallocateFile(path, regionSize);
}
示例#5
0
extern int 
main(int argc, char *argv[])
{
	int s;
	void *res;


	/************** PROCESS ARGS START ***************/
	int argCount = 0;
	if (DEBUG) 
	{
    	for (argCount = 0; argCount < argc; argCount++)
		{
	  		printf("argv[%d] = %s\n", argCount, argv[argCount]);
		}
	}

    // Check the args
    while ((opt = getopt(argc, argv, "b:k:m:g:t:s:c:nvh")) != EOF) 
    {
		switch (opt) {
	    	case 'h' :
				print_help();
				exit(EXIT_FAILURE);
				break;
	    	case 't' : // terabytes
				if (optarg) {
		    		bytesToAllocate = atoll(optarg)*1024*1024*1024*1024;
            	} else {
					fprintf(stdout, "no arg");
				}
				break;
	    	case 'g' : // gigabytes
				bytesToAllocate = atoll(optarg)*1024*1024*1024;
				break;
	    	case 'm' : // megabytes
				bytesToAllocate = atoll(optarg)*1024*1024;
				break;
	    	case 'k' : // kilobytes
				bytesToAllocate = atoll(optarg)*1024;
				break;
            case 's': // number of zerofill 'streams'
                numStreams = atoll(optarg);
				break;
            case 'c' : // block size of streams
                chunkSize = atoll(optarg);
                break;
	    	case 'b':  // bytes 
				// fall through to default
	    	default:
				bytesToAllocate = atoll(optarg); // bytes
			break;
		}
    }

    // Check a filename was passed
    if (optind >= argc)
    {
		fprintf(stderr, "Expected filename after options\n");
		exit(EXIT_FAILURE);
    } 
    else
    {
		//memset(fileName, 0, sizeof(fileName));
		fileName = argv[optind];	
    }

    /*************** PROCESS ARGS END **************/
    /* Open the file handle */
	int preFh = 0;
    preFh = open(fileName, O_RDWR|O_CREAT, 0644);
    if (preFh < 0)
    {
        perror(fileName);;
        exit(EXIT_FAILURE);
    }

	/************ PREALLOCATE FILE START ***********/
	if (! preallocateFile(preFh, 0, bytesToAllocate)) {

		fprintf(stderr, "Could not preallocate file %s. Exiting.\n", fileName);
		exit (EXIT_FAILURE);
	}

	/* Close the file handle, each fill thread has its own */
	close(preFh);

	/********** PREALLOCATE FILE END *********/


	
	// DEFINE A NUMBER OF THREADS
    int rc = 0;
	int tnum = 10;
	int wCount = 0;
	int num_threads = 10;

	thread_info *tinfo;
	pthread_t attr;

    /* Allocate memory for the pthreads */
	tinfo = (thread_info *)malloc(sizeof(struct thread_info)); // bug here. Should be using calloc().  Results in segv dump
	if (tinfo == NULL) 
	{
		printf("tinfo err\n");
		exit(EXIT_FAILURE);
	}

	fprintf(stdout, "total bytes: %llu\n", bytesToAllocate);
	unsigned long long next_byte = 1;

	/* Create one thread for each block of the file */
	for (tnum=1; tnum <= num_threads; tnum++) 
	{
		/* Fill in the thread_info struct to pass to the thread */
		tinfo[tnum].thread_num = tnum;
		tinfo[tnum].start_byte = next_byte;
		tinfo[tnum].num_bytes = (bytesToAllocate / num_threads) - 1;

		next_byte = tinfo[tnum].start_byte + tinfo[tnum].num_bytes + 1;

		/* Create the thread */
		rc = pthread_create(&tinfo[tnum].thread_id, NULL, &process_request, &tinfo[tnum]);
		if (rc) {
			exit(EXIT_FAILURE);
		}
	}

	/* Now join with each thread, and display its returned value */

/*	for (tnum = 0; tnum < num_threads; tnum++)
	{
		s = pthread_join(tinfo[tnum].thread_id, &res);
        if (s != 0)
		{
			printf("pthread_join err\n");
		}
        
		printf("Joined with thread %d; returned value was %s\n", tinfo[tnum].thread_num, (char *) res); */
        //free(res);      /* Free memory allocated by thread */
//    }

    /* Exit nicely */
	exit(EXIT_SUCCESS);
}