当前位置: 首页>>代码示例>>C++>>正文


C++ pwrite函数代码示例

本文整理汇总了C++中pwrite函数的典型用法代码示例。如果您正苦于以下问题:C++ pwrite函数的具体用法?C++ pwrite怎么用?C++ pwrite使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pwrite函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: switch

int keydiv_dtable::create(int dfd, const char * name, const params & config, dtype::ctype key_type)
{
	int r, kdd_dfd, meta;
	divider_list dividers;
	const dtable_factory * base;
	params base_config;
	
	kddtable_header header;
	header.magic = KDDTABLE_MAGIC;
	header.version = KDDTABLE_VERSION;
	switch(key_type)
	{
		case dtype::UINT32:
			header.key_type = 1;
			r = load_dividers<int, uint32_t>(config, 0, &dividers);
			break;
		case dtype::DOUBLE:
			header.key_type = 2;
			r = load_dividers<float, double>(config, 0, &dividers);
			break;
		case dtype::STRING:
			header.key_type = 3;
			r = load_dividers<istr, istr>(config, 0, &dividers);
			break;
		case dtype::BLOB:
			header.key_type = 4;
			r = load_dividers<blob, blob>(config, 0, &dividers, true);
			break;
		default:
			return -EINVAL;
	}
	header.dt_count = dividers.size() + 1;
	/* make sure we don't overflow the header field */
	if(header.dt_count != dividers.size() + 1)
		return -EINVAL;
	
	base = dtable_factory::lookup(config, "base");
	if(!base)
		return -EINVAL;
	if(!config.get("base_config", &base_config, params()))
		return -EINVAL;
	
	r = mkdirat(dfd, name, 0755);
	if(r < 0)
		return r;
	kdd_dfd = openat(dfd, name, O_RDONLY);
	if(kdd_dfd < 0)
	{
		unlinkat(dfd, name, AT_REMOVEDIR);
		return kdd_dfd;
	}
	
	for(uint32_t i = 0; i < header.dt_count; i++)
	{
		char name[32];
		sprintf(name, "kdd_data.%u", i);
		r = base->create(kdd_dfd, name, base_config, key_type);
		if(r < 0)
			goto fail;
	}
	
	meta = openat(kdd_dfd, "kdd_meta", O_WRONLY | O_CREAT, 0644);
	if(meta < 0)
	{
		r = meta;
		goto fail;
	}
	r = pwrite(meta, &header, sizeof(header), 0);
	close(meta);
	if(r != sizeof(header))
		goto fail;
	close(kdd_dfd);
	return 0;
	
fail:
	close(kdd_dfd);
	util::rm_r(dfd, name);
	return (r < 0) ? r : -1;
}
开发者ID:nathansgreen,项目名称:anvil,代码行数:79,代码来源:keydiv_dtable.cpp

示例2:

void
runtime·badsignal(void)
{
	runtime·pwrite(2, badsignal, sizeof badsignal - 1, -1LL);
	runtime·exits(badsignal);
}
开发者ID:blackbeans,项目名称:golang,代码行数:6,代码来源:os_plan9.c

示例3: write_filemarks

int
write_filemarks(uint32_t count, uint8_t *sam_stat)
{
	uint32_t blk_number;
	uint64_t data_offset;
	ssize_t nwrite;

	if (!tape_loaded(sam_stat)) {
		return -1;
	}

	/* Applications assume that writing a filemark (even writing zero
	   filemarks) will force-flush any data buffered in the drive to media
	   so that after the write-filemarks call returns there is no
	   possibility that any data previously written could be lost due
	   to a power hit.  Provide a similar guarantee here.
	*/

	if (count == 0) {
		MHVTL_DBG(2, "Flushing data - 0 filemarks written");
		fsync(datafile);
		fsync(indxfile);
		fsync(metafile);

		return 0;
	}

	if (check_for_overwrite(sam_stat)) {
		return -1;
	}

	/* Preserve existing raw_pos data we need, then clear raw_pos and
	   fill it in with new data.
	*/

	blk_number = raw_pos.hdr.blk_number;
	data_offset = raw_pos.data_offset;

	memset(&raw_pos, 0, sizeof(raw_pos));

	raw_pos.data_offset = data_offset;

	raw_pos.hdr.blk_type = B_FILEMARK;	/* Header type */
	raw_pos.hdr.blk_flags = 0;
	raw_pos.hdr.blk_number = blk_number;
	raw_pos.hdr.blk_size = 0;
	raw_pos.hdr.disk_blk_size = 0;

	/* Now write out one header per filemark. */

	for ( ; count > 0; count--, blk_number++) {
		raw_pos.hdr.blk_number = blk_number;

		MHVTL_DBG(3, "Writing filemark: block %d", blk_number);

		nwrite = pwrite(indxfile, &raw_pos, sizeof(raw_pos),
			blk_number * sizeof(raw_pos));
		if (nwrite != sizeof(raw_pos)) {
			mkSenseBuf(MEDIUM_ERROR, E_WRITE_ERROR, sam_stat);
			MHVTL_ERR("Index file write failure,"
					" pos: %" PRId64 ": %s",
				(uint64_t)blk_number * sizeof(raw_pos),
				strerror(errno));
			return -1;
		}
		add_filemark(blk_number);
	}

	/* Provide the force-flush guarantee. */

	fsync(datafile);
	fsync(indxfile);
	fsync(metafile);

	return mkEODHeader(blk_number, data_offset);
}
开发者ID:aroundrobin,项目名称:mhvtl,代码行数:76,代码来源:vtlcart.c

示例4: snprintf

int journal::init_crfd(const istr & commit_name)
{
	int r;
	off_t filesize, nextcr = 0;
	struct timeval settime[2] = {{0, 0}, {0, 0}};
	commit_record zero = {0, 0}, cr;
	
	/* Only append more empy records to the commit file if it is already open
	 * otherwise create a new commit record file. */
	if(crfd < 0)
	{
		istr cname = commit_name;
		if(!cname)
		{
			char commit_number[16];
			snprintf(commit_number, sizeof(commit_number), "%d", commits);
			cname = path + J_COMMIT_EXT + commit_number;
		}
		crfd = openat(dfd, cname, O_CREAT | O_RDWR, 0644);
		if(crfd < 0)
			return -1;
	}

	filesize = lseek(crfd, 0, SEEK_END);
	util::memset(zero.checksum, 0, sizeof(zero.checksum));
	/* find out where the last good commit record is */
	while((r = pread(crfd, &cr, sizeof(cr), nextcr)))
	{
		if(r < (int) sizeof(cr))
			break;
		if(!cr.offset && !cr.length && !memcmp(&cr.checksum, &zero.checksum, J_CHECKSUM_LEN))
			break;
		nextcr += r;
	}
	
	/* We set the mtime for the commit record file in the future to prevent
	 * the inode metadata being updated with every write - this uses a hack
	 * in Featherstitch to optimize for patchgroups. */
	
	/* atime */
	settime[0].tv_sec = time(NULL);
	/* mtime is current time plus 10 years, or the end of 31-bit time, whichever is later */
	settime[1].tv_sec = settime[0].tv_sec + 315360000;
	if(settime[1].tv_sec < 2147483647)
		settime[1].tv_sec = 2147483647;
	if((r = futimes(crfd, settime)) < 0)
		goto error;
	
	if(filesize < (nextcr + (int) sizeof(cr)))
	{
		/* zero out the rest of the file J_ADD_N_COMMITS records at a time */
		uint8_t zbuffer[1000 * sizeof(zero)];
		util::memset(zbuffer, 0, sizeof(zbuffer));
		while((filesize - nextcr) < J_ADD_N_COMMITS * (int) sizeof(zbuffer))
		{
			r = pwrite(crfd, zbuffer, sizeof(zbuffer), filesize);
			if(r <= 0)
				goto error;
			filesize += r;
		}
		/* necessary? */
		fsync(crfd);
	}
	
	return nextcr;
	
error:
	if(crfd > 0)
	{
		close(crfd);
		crfd = -1;
	}
	return r < 0 ? r : -1;
}
开发者ID:GSathish,项目名称:casamia-datastore,代码行数:74,代码来源:journal.cpp

示例5: masterconn_download_data

void masterconn_download_data(masterconn *eptr,const uint8_t *data,uint32_t length) {
    uint64_t offset;
    uint32_t leng;
    uint32_t crc;
    ssize_t ret;
    if (eptr->metafd<0) {
        syslog(LOG_NOTICE,"MATOML_DOWNLOAD_DATA - file not opened");
        eptr->mode = KILL;
        return;
    }
    if (length<16) {
        syslog(LOG_NOTICE,"MATOML_DOWNLOAD_DATA - wrong size (%"PRIu32"/16+data)",length);
        eptr->mode = KILL;
        return;
    }
    passert(data);
    offset = get64bit(&data);
    leng = get32bit(&data);
    crc = get32bit(&data);
    if (leng+16!=length) {
        syslog(LOG_NOTICE,"MATOML_DOWNLOAD_DATA - wrong size (%"PRIu32"/16+%"PRIu32")",length,leng);
        eptr->mode = KILL;
        return;
    }
    if (offset!=eptr->dloffset) {
        syslog(LOG_NOTICE,"MATOML_DOWNLOAD_DATA - unexpected file offset (%"PRIu64"/%"PRIu64")",offset,eptr->dloffset);
        eptr->mode = KILL;
        return;
    }
    if (offset+leng>eptr->filesize) {
        syslog(LOG_NOTICE,"MATOML_DOWNLOAD_DATA - unexpected file size (%"PRIu64"/%"PRIu64")",offset+leng,eptr->filesize);
        eptr->mode = KILL;
        return;
    }
#ifdef HAVE_PWRITE
    ret = pwrite(eptr->metafd,data,leng,offset);
#else /* HAVE_PWRITE */
    lseek(eptr->metafd,offset,SEEK_SET);
    ret = write(eptr->metafd,data,leng);
#endif /* HAVE_PWRITE */
    if (ret!=(ssize_t)leng) {
        mfs_errlog_silent(LOG_NOTICE,"error writing metafile");
        if (eptr->downloadretrycnt>=5) {
            masterconn_download_end(eptr);
        } else {
            eptr->downloadretrycnt++;
            masterconn_download_next(eptr);
        }
        return;
    }
    if (crc!=mycrc32(0,data,leng)) {
        syslog(LOG_NOTICE,"metafile data crc error");
        if (eptr->downloadretrycnt>=5) {
            masterconn_download_end(eptr);
        } else {
            eptr->downloadretrycnt++;
            masterconn_download_next(eptr);
        }
        return;
    }
    /*if (fsync(eptr->metafd)<0) {
      mfs_errlog_silent(LOG_NOTICE,"error syncing metafile");
      if (eptr->downloadretrycnt>=5) {
      masterconn_download_end(eptr);
      } else {
      eptr->downloadretrycnt++;
      masterconn_download_next(eptr);
      }
      return;
      }*/
    eptr->dloffset+=leng;
    eptr->downloadretrycnt=0;
    masterconn_download_next(eptr);
}
开发者ID:windreamer,项目名称:moosefs,代码行数:74,代码来源:masterconn.c

示例6: nandwrite_mlc


//.........这里部分代码省略.........
        if (read_next) {
            erase_buffer(writebuf, readlen);
            /* Read up to one page data */
            while (tinycnt < readlen) {
                cnt = read(image, writebuf + tinycnt, readlen - tinycnt);
                if (cnt == 0) { /* EOF */
                    break;
                } else if (cnt < 0) {
                    perror ("File I/O error on input");
                    retCode = -3;
                    goto closeall;
                }
                tinycnt += cnt;
            }
            imglen -= tinycnt;
            read_next = false;
        }

        for (nblock = 0; nblock < CONFIG_BLOCK_REPLICATION; nblock++)
        {
//			offs = mtdoffset + nblock * blocksize + skip * blocksize;
            offs = mtdoffset + skip * blocksize;
            // skip bad blocks
            ret = nand_block_isbad(dev, offs);
            if (ret < 0) {
                retCode = -5;
                goto closeall;
            } else if (ret == 1) {
#if 0
                loff_t checkblock;
                char have_copy = 0;

                if (!quiet) {
                    fprintf(stderr, "Skip bad block at address 0x%x, (block %u)\n",
                            offs, offs / blocksize);
                }

                badblocks++;
                // make sure we have at least one copy
                for (checkblock = 0; checkblock < CONFIG_BLOCK_REPLICATION; checkblock++)
                {
                    offs = mtdoffset + checkblock * blocksize + skip * blocksize;
                    ret = nand_block_isbad(dev, offs);
                    if (ret < 0) goto closeall;
                    else if (ret == 0) {
                        have_copy = 1;
                        break;
                    }
                }
                if (!have_copy)
                {
                    printf("Too many bad blocks\n");
                    goto closeall;
                }
                skip += blockskip;
                continue;
#else
                if (!quiet) {
                    uint32_t block_mask = meminfo->erasesize - 1;
                    printf("Bad block 0x%x\n", (offs & (~block_mask)));
                }
                if (++badblocks >= CONFIG_BLOCK_REPLICATION) {
                    printf("Too many bad blocks\n");
                    retCode = -4;
                    goto closeall;
                }
                skip += blockskip;
                continue;
#endif
            }

            for (npage = 0; npage < CONFIG_PAGE_REPLICATION; npage++)
            {
                offs = mtdoffset + npage * pagesize + skip * blocksize;
                /* Write out the Page data */
                if (pwrite(dev, writebuf, pagesize, offs) != pagesize) {
                    fprintf(stderr, "Bad page for copy %u of block %x for address %x\n",
                            npage, nblock, offs);
                }
            }
            skip += blockskip;
            read_next = true;
        } // for nblock
        mtdoffset += pagesize * CONFIG_PAGE_REPLICATION;
    } // while (imglen > 0)

closeall:
    if (writebuf) {
        free(writebuf);
    }

    close(image);

    if (imglen > 0)
    {
        fprintf(stderr, "Data was only partially written due to error\n");
    }

    return retCode;
}
开发者ID:yjhjstz,项目名称:pandorabox,代码行数:101,代码来源:nandwrite_mlc.c

示例7: write_process_memory

/* write data to a process memory space */
int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t size, const char *src )
{
    struct thread *thread = get_ptrace_thread( process );
    int ret = 0;
    long data = 0;
    data_size_t len;
    long *addr;
    unsigned long first_mask, first_offset, last_mask, last_offset;

    if (!thread) return 0;

    if ((unsigned long)ptr != ptr)
    {
        set_error( STATUS_ACCESS_DENIED );
        return 0;
    }

    /* compute the mask for the first long */
    first_mask = ~0;
    first_offset = ptr % sizeof(long);
    memset( &first_mask, 0, first_offset );

    /* compute the mask for the last long */
    last_offset = (size + first_offset) % sizeof(long);
    if (!last_offset) last_offset = sizeof(long);
    last_mask = 0;
    memset( &last_mask, 0xff, last_offset );

    addr = (long *)(unsigned long)(ptr - first_offset);
    len = (size + first_offset + sizeof(long) - 1) / sizeof(long);

    if (suspend_for_ptrace( thread ))
    {
        if (!check_process_write_access( thread, addr, len ))
        {
            set_error( STATUS_ACCESS_DENIED );
            goto done;
        }

        if (len > 3)
        {
            char procmem[24];
            int fd;

            sprintf( procmem, "/proc/%u/mem", process->unix_pid );
            if ((fd = open( procmem, O_WRONLY )) != -1)
            {
                ssize_t r = pwrite( fd, src, size, ptr );
                close( fd );
                if (r == size)
                {
                    ret = 1;
                    goto done;
                }
            }
        }

        /* first word is special */
        if (len > 1)
        {
            memcpy( (char *)&data + first_offset, src, sizeof(long) - first_offset );
            src += sizeof(long) - first_offset;
            if (write_thread_long( thread, addr++, data, first_mask ) == -1) goto done;
            first_offset = 0;
            len--;
        }
        else last_mask &= first_mask;

        while (len > 1)
        {
            memcpy( &data, src, sizeof(long) );
            src += sizeof(long);
            if (write_thread_long( thread, addr++, data, ~0ul ) == -1) goto done;
            len--;
        }

        /* last word is special too */
        memcpy( (char *)&data + first_offset, src, last_offset - first_offset );
        if (write_thread_long( thread, addr, data, last_mask ) == -1) goto done;
        ret = 1;

    done:
        resume_after_ptrace( thread );
    }
    return ret;
}
开发者ID:qqzhang,项目名称:uk-1.0,代码行数:87,代码来源:ptrace.c

示例8: xmp_write

static int xmp_write(const char *path, const char *buf, size_t size,
		     off_t offset, struct fuse_file_info *fi)
{
	(void) fi;
	(void) offset;

	int res=0;
	int action = COPY;

	ssize_t vsize = 0;
	char *tval = NULL;

	char fpath[PATH_MAX];
	xmp_getfullpath(fpath, path);

	vsize = getxattr(fpath, XATRR_ENCRYPTED_FLAG, NULL, 0);
	tval = malloc(sizeof(*tval)*(vsize));
	vsize = getxattr(fpath, XATRR_ENCRYPTED_FLAG, tval, vsize);

	if (vsize < 0 || memcmp(tval, "false", 5) == 0){
		if(errno == ENODATA){
			fprintf(stderr, "Encryption flag not set, file cannot be read.\n");
		}
		fprintf(stderr, "File unencrypted, reading...\n");
	}
	/* If the attribute exists and is true get size of decrypted file */
	else if (memcmp(tval, "true", 4) == 0){
		fprintf(stderr, "File encrypted, decrypting...\n");
		action = DECRYPT;
	}


	/* If the file to be written to is encrypted */
	if (action == DECRYPT){
		
		FILE *fd = fopen(fpath, "rb+");
		const char *tpath = ftemp(fpath, ".write");
		FILE *dfd = fopen(tpath, "wb+");

		fseek(fd, 0, SEEK_END);
		fseek(fd, 0, SEEK_SET);

		if(!do_crypt(fd, dfd, DECRYPT, ENCFS_DATA->passkey)){
			fprintf(stderr, "Decryption failed, error code: %d\n", res);
    	}

    	fseek(fd, 0, SEEK_SET);

    	res = fwrite(buf, 1, size, dfd);
    	if (res == -1)
		res = -errno;

		fseek(dfd, 0, SEEK_SET);

		if(!do_crypt(dfd, fd, ENCRYPT, ENCFS_DATA->passkey)){
			fprintf(stderr, "Encryption failed, error code: %d\n", res);
		}

		fclose(fd);
		fclose(dfd);
		remove(tpath);
	}
	/* If the file to be written to is unencrypted */
	else if (action == COPY){
		int fd1;
		fprintf(stderr, "File unencrypted, reading...\n");

		fd1 = open(fpath, O_WRONLY);
		if (fd1 == -1)
			return -errno;

		res = pwrite(fd1, buf, size, offset);
		if (res == -1)
			res = -errno;

		close(fd1);
   	}
   	
	free(tval);
	return res;
}
开发者ID:kevinjpickard,项目名称:CSCI-3753,代码行数:81,代码来源:pa5-encfs.c

示例9: ADIOI_XFS_WriteContig

void ADIOI_XFS_WriteContig(ADIO_File fd, void *buf, int count, 
                     MPI_Datatype datatype, int file_ptr_type,
		     ADIO_Offset offset, ADIO_Status *status, int *error_code)
{
    int err=-1, datatype_size, len, diff, size, nbytes;
    void *newbuf;
    static char myname[] = "ADIOI_XFS_WRITECONTIG";

    MPI_Type_size(datatype, &datatype_size);
    len = datatype_size * count;

    fd->fp_sys_posn = -1; /* set it to null, since we are using pwrite */

    if (file_ptr_type == ADIO_INDIVIDUAL) offset = fd->fp_ind;

    if (!(fd->direct_write))     /* direct I/O not enabled */
	err = pwrite(fd->fd_sys, buf, len, offset);
    else {       /* direct I/O enabled */

	/* (1) if mem_aligned && file_aligned 
                    use direct I/O to write up to correct io_size
                    use buffered I/O for remaining  */

	if (!(((long) buf) % fd->d_mem) && !(offset % fd->d_miniosz)) 
	    ADIOI_XFS_Aligned_Mem_File_Write(fd, buf, len, offset, &err);

        /* (2) if !file_aligned
                    use buffered I/O to write up to file_aligned
                    At that point, if still mem_aligned, use (1)
   		        else copy into aligned buf and then use (1) */
	else if (offset % fd->d_miniosz) {
	    diff = fd->d_miniosz - (offset % fd->d_miniosz);
	    diff = ADIOI_MIN(diff, len);
	    nbytes = pwrite(fd->fd_sys, buf, diff, offset);

	    buf = ((char *) buf) + diff;
	    offset += diff;
	    size = len - diff;
	    if (!(((long) buf) % fd->d_mem)) {
		ADIOI_XFS_Aligned_Mem_File_Write(fd, buf, size, offset, &err);
		nbytes += err;
	    }
	    else {
		newbuf = (void *) memalign(XFS_MEMALIGN, size);
		if (newbuf) {
		    memcpy(newbuf, buf, size);
		    ADIOI_XFS_Aligned_Mem_File_Write(fd, newbuf, size, offset, &err);
		    nbytes += err;
		    free(newbuf);
		}
		else nbytes += pwrite(fd->fd_sys, buf, size, offset);
	    }
	    err = nbytes;
	}

        /* (3) if !mem_aligned && file_aligned
    	            copy into aligned buf, then use (1)  */
	else {
	    newbuf = (void *) memalign(XFS_MEMALIGN, len);
	    if (newbuf) {
		memcpy(newbuf, buf, len);
		ADIOI_XFS_Aligned_Mem_File_Write(fd, newbuf, len, offset, &err);
		free(newbuf);
	    }
	    else err = pwrite(fd->fd_sys, buf, len, offset);
	}
    }

    if (file_ptr_type == ADIO_INDIVIDUAL) fd->fp_ind += err;

#ifdef HAVE_STATUS_SET_BYTES
    if (err != -1) MPIR_Status_set_bytes(status, datatype, err);
#endif

    if (err == -1) {
	*error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
					   myname, __LINE__, MPI_ERR_IO, "**io",
					   "**io %s", strerror(errno));
    }
    else *error_code = MPI_SUCCESS;
}
开发者ID:aosm,项目名称:openmpi,代码行数:81,代码来源:ad_xfs_write.c

示例10: xmp_read


//.........这里部分代码省略.........
			return -EBUSY;
		}
	}

	int i;

	struct elm_read_req prefetch_req = req;

	for (i=0; i<prefetch_aggressiveness; i++) {

		prefetch_req.offset += prefetch_req.size;
		prefetch_req.sem = NULL;
		prefetch_req.refcnt = 0;
		prefetch_req.p_response = NULL;

		if (prefetch_req.offset + prefetch_req.size
		    > info->st_buf.st_size)
		{
			/* XXX */
			break;
		}


		/* Prefetch requests are advisory only. 
		 * If the queue is full, it may not be an error.
		 */

		if (circ_enq(&req_queue, &prefetch_req)) {
			break;
		}
	}

	int q_cnt = circ_cnt(&req_queue);

	fprintf(fp_sync, "req_queue_cnt = %d\n", q_cnt);

	fflush(fp_sync);

	pthread_cond_signal(&req_condition);

	pthread_mutex_unlock(&req_queue_lock);

	if (get_rc == 0) {
		/* If it was already in cache no need to wait.
		 */
		return res;
	}

	/* Wait for the response */

	sem_wait(&sem);

	fprintf(fp_sync, "got response\n");
	fflush(fp_sync);

	pthread_mutex_lock(&cache_lock);

	/* Check the response */

	result = &response;

	if (result->rc >= 0) {
		memcpy(buf,
		       result->data,
		       result->rc);
	}

	res = result->rc;

	if (*result->refcnt > 0) {
		*result->refcnt -= 1;
	}

	cache_report(fp_sync, cache_hit_bytes, cache_miss_bytes);

	pthread_mutex_unlock(&cache_lock);

	sem_destroy(&sem);

	return res;
}

static int xmp_write(const char *path, const char *buf, size_t size,
		     off_t offset, struct fuse_file_info *fi)
{
	int fd;
	int res;

	(void) fi;
	fd = open(path, O_WRONLY);
	if (fd == -1)
		return -errno;

	res = pwrite(fd, buf, size, offset);
	if (res == -1)
		res = -errno;

	close(fd);
	return res;
}
开发者ID:zhangwei1984,项目名称:prefetch,代码行数:101,代码来源:loop.refactored.c

示例11: cfs_write

static int cfs_write(const char *path, const char *buf, size_t length, off_t offset, struct fuse_file_info *info)
{
    update_dir_cache(path, offset + length, 0, 0);
    return pwrite(((openfile *)(uintptr_t)info->fh)->fd, buf, length, offset);
}
开发者ID:gokuale,项目名称:hubicfuse,代码行数:5,代码来源:cloudfuse.c

示例12: resize_gpt_partition

int resize_gpt_partition(const char *devname, __u64 new_size)
{
	unsigned char buf[SECTOR_SIZE*GPT_DATA_SIZE]; // LBA1 header, LBA2-34 partition entry
	int fd;
	int part, ret;
	struct GptHeader *pt;
	struct GptEntry *pe;
	__u32 pt_crc32, pe_crc32, orig_crc;
	off_t size;
	__u64 tmp;

	ret = has_partition(devname, &part);
	if (ret)
		return ret;

	if (!part)
		return 0;

	ret = ploop_get_size(devname, &size);
	if (ret)
		return ret;

	// Resize up to max available space
	if (new_size == 0)
		new_size = size;

	if (new_size > size) {
		ploop_err(0, "Unable to resize GPT partition:"
				" incorrect parameter new_size=%llu size=%lu",
				new_size, (long)size);
		return SYSEXIT_PARAM;
	}

	ploop_log(1, "Resizing GPT partition to %ld", (long)new_size);
	fd = open(devname, O_RDWR);
	if (fd == -1) {
		ploop_err(errno, "open %s", devname);
		return SYSEXIT_OPEN;
	}
	// skip LBA0 Protective MBR
	ret = pread(fd, buf, sizeof(buf), SECTOR_SIZE);
	if (ret == -1) {
		ploop_err(errno, "pread %s", devname);
		goto err;
	}
	pt = (struct GptHeader *)buf;
	pe = (struct GptEntry *)(&buf[SECTOR_SIZE * GPT_HEADER_SIZE]);

	// Validate crc
	orig_crc = pt->header_crc32;
	pt->header_crc32 = 0;
	pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size);
	if (pt_crc32 != orig_crc) {
		ploop_err(0, "GPT validation failed orig crc %x != %x",
				orig_crc, pt_crc32);
		ret = -1;
		goto err;
	}
	// change GPT header
	pt->alternate_lba = new_size - 1;
	pt->last_usable_lba = new_size - GPT_DATA_SIZE - 1;
	pe->ending_lba = (pt->last_usable_lba >> 3 << 3) - 1;

	// Recalculate crc32
	pe_crc32 = ploop_crc32((unsigned char *)pe, SECTOR_SIZE * GPT_PT_ENTRY_SIZE);
	pt->partition_entry_array_crc32 = pe_crc32;

	pt->header_crc32 = 0;
	pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size);
	pt->header_crc32 = pt_crc32;

	ploop_log(0, "Storing GPT");
	ret = pwrite(fd, pt, SECTOR_SIZE * GPT_DATA_SIZE, SECTOR_SIZE);
	if (ret == -1) {
		ploop_err(errno, "Failed to store primary GPT %s", devname);
		goto err;
	}
	ret = fsync(fd);
	if (ret) {
		ploop_err(errno, "Can't fsync %s", devname);
		ret = SYSEXIT_FSYNC;
		goto err;
	}

	// Store secondary GPT entries
	tmp = pt->my_lba;
	pt->my_lba = pt->alternate_lba;
	pt->alternate_lba = tmp;
	pt->partition_entry_lba = pt->last_usable_lba + 1;

	// Recalculate crc32
	pt->header_crc32 = 0;
	pt_crc32 = ploop_crc32((unsigned char *)pt, pt->header_size);
	pt->header_crc32 = pt_crc32;

	ret = pwrite(fd, pe, SECTOR_SIZE * GPT_PT_ENTRY_SIZE,
			(new_size - GPT_DATA_SIZE)*SECTOR_SIZE);
	if (ret == -1) {
		ploop_err(errno, "Failed to store secondary GPT %s", devname);
		goto err;
//.........这里部分代码省略.........
开发者ID:varung,项目名称:ploop,代码行数:101,代码来源:gpt.c

示例13: fifolog_write_output

static int
fifolog_write_output(struct fifolog_writer *f, int fl, time_t now)
{
	long h, l = f->ff->zs->next_out - f->obuf;
	ssize_t i, w;
	int retval = 0;

	h = 4;					/* seq */
	be32enc(f->obuf, f->seq);
	f->obuf[h] = f->flag;
	h += 1;					/* flag */
	if (f->flag & FIFOLOG_FLG_SYNC) {
		be32enc(f->obuf + h, now);
		h += 4;				/* timestamp */
	}

	assert(l <= (long)f->ff->recsize);	/* NB: l includes h */
	assert(l >= h);

	/* We will never write an entirely empty buffer */
	if (l == h)
		return (0);

	if (l < (long)f->ff->recsize && fl == Z_NO_FLUSH)
		return (0);

	w = f->ff->recsize - l;
	if (w >  255) {
		be32enc(f->obuf + f->ff->recsize - 4, w);
		f->obuf[4] |= FIFOLOG_FLG_4BYTE;
	} else if (w > 0) {
		f->obuf[f->ff->recsize - 1] = (uint8_t)w;
		f->obuf[4] |= FIFOLOG_FLG_1BYTE;
	}

	f->cnt[FIFOLOG_PT_BYTES_POST] += l - h;

	i = pwrite(f->ff->fd, f->obuf, f->ff->recsize,
	    (f->recno + 1) * f->ff->recsize);
	if (i != f->ff->recsize)
		retval = -1;
	else
		retval = 1;

	f->cnt[FIFOLOG_PT_WRITES]++;
	f->cnt[FIFOLOG_PT_RUNTIME] = now - f->starttime;

	f->lastwrite = now;
	/*
	 * We increment these even on error, so as to properly skip bad,
	 * sectors or other light trouble.
	 */
	f->seq++;
	f->recno++;
	f->flag = 0;

	memset(f->obuf, 0, f->obufsize);
	f->ff->zs->next_out = f->obuf + 5;
	f->ff->zs->avail_out = f->obufsize - 5;
	return (retval);
}
开发者ID:JabirTech,项目名称:Source,代码行数:61,代码来源:fifolog_write_poll.c

示例14: tagsistant_write

/**
 * write() equivalent
 *
 * @param path the path of the file to be written
 * @param buf buffer holding write() data
 * @param size how many bytes should be written (size of *buf)
 * @param offset starting of the write
 * @param fi struct fuse_file_info used for open() flags
 * @return(0 on success, -errno otherwise)
 */
int tagsistant_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
    int res = 0, tagsistant_errno = 0, fh = 0;

	TAGSISTANT_START("WRITE on %s [size: %lu offset: %lu]", path, (unsigned long) size, (long unsigned int) offset);

	tagsistant_querytree *qtree = tagsistant_querytree_new(path, 0, 0, 1, 1);

	// -- malformed --
	if (QTREE_IS_MALFORMED(qtree))
		TAGSISTANT_ABORT_OPERATION(ENOENT);

	// -- alias --
	if (QTREE_IS_ALIAS(qtree) && qtree->alias) {
		res = size;

		gchar *_buf = g_strndup(buf, size);

		// end the string at the first carriage return or line feed character
		gchar *path_ptr = rindex(_buf, '\n');
		if (path_ptr) *path_ptr = '/';

		path_ptr = rindex(_buf, '\r');
		if (path_ptr) *path_ptr = '/';

		// remove double slashes
		GRegex *rx = g_regex_new("//", 0, 0, NULL);
		gchar *_buf2 = g_regex_replace(rx, _buf, -1, 0, "/", 0, NULL);
		g_regex_unref(rx);
		g_free(_buf);
		_buf = _buf2;

		// get the size of the buffer
		size_t max_size = MIN(size, TAGSISTANT_ALIAS_MAX_LENGTH - 1);
		size_t real_size = MIN(max_size, strlen(_buf));

		// copy the buffer to a temporary variable
		gchar *value = g_strndup(_buf, real_size);

		// save the buffer on disk
		tagsistant_sql_alias_set(qtree->dbi, qtree->alias, value);

		g_free(value);
		g_free(_buf);

	} else

	// -- object on disk --
	if (QTREE_POINTS_TO_OBJECT(qtree)) {
		if (!qtree->full_archive_path) {
			dbg('F', LOG_ERR, "Null qtree->full_archive_path");
			TAGSISTANT_ABORT_OPERATION(EFAULT);
		}

#if TAGSISTANT_ENABLE_FILE_HANDLE_CACHING
		if (fi->fh) {
			tagsistant_get_file_handle(fi, fh);
			res = pwrite(fh, buf, size, offset);
			tagsistant_errno = errno;
		}

		if ((-1 == res) || (0 == fh)) {
			if (fh) close(fh);
			fh = open(qtree->full_archive_path, fi->flags|O_WRONLY);
			if (fh)	res = pwrite(fh, buf, size, offset);
			else res = -1;
			tagsistant_errno = errno;
		}

		tagsistant_set_file_handle(fi, fh);
#else
		fh = open(qtree->full_archive_path, fi->flags|O_WRONLY);
		if (fh) {
			res = pwrite(fh, buf, size, offset);
			tagsistant_errno = errno;
			close(fh);
		} else {
			TAGSISTANT_ABORT_OPERATION(errno);
		}
#endif
	}

	// -- tags --
	// -- stats --
	// -- relations --
	else TAGSISTANT_ABORT_OPERATION(EROFS);

	// dbg('F', LOG_ERR, "Yeah!");

TAGSISTANT_EXIT_OPERATION:
//.........这里部分代码省略.........
开发者ID:chebee7i,项目名称:Tagsistant,代码行数:101,代码来源:write.c

示例15: rtems_aio_handle

static void *
rtems_aio_handle (void *arg)
{

  rtems_aio_request_chain *r_chain = arg;
  rtems_aio_request *req;
  rtems_chain_control *chain;
  rtems_chain_node *node;
  int result, policy;
  struct sched_param param;

  AIO_printf ("Thread started\n");
 
  while (1) {
    
    /* acquire the mutex of the current fd chain.
       we don't need to lock the queue mutex since we can
       add requests to idle fd chains or even active ones
       if the working request has been extracted from the
       chain */
    result = pthread_mutex_lock (&r_chain->mutex);
    if (result != 0)
      return NULL;
    
    chain = &r_chain->perfd;    

    /* If the locked chain is not empty, take the first
       request extract it, unlock the chain and process 
       the request, in this way the user can supply more
       requests to this fd chain */
    if (!rtems_chain_is_empty (chain)) {

      AIO_printf ("Get new request from not empty chain\n");	
      node = rtems_chain_first (chain);
      req = (rtems_aio_request *) node;
      
      /* See _POSIX_PRIORITIZE_IO and _POSIX_PRIORITY_SCHEDULING
	 discussion in rtems_aio_enqueue () */
      pthread_getschedparam (pthread_self(), &policy, &param);
      param.sched_priority = req->priority;
      pthread_setschedparam (pthread_self(), req->policy, &param);

      rtems_chain_extract (node);

      pthread_mutex_unlock (&r_chain->mutex);

      switch (req->aiocbp->aio_lio_opcode) {
      case LIO_READ:
	AIO_printf ("read\n");
        result = pread (req->aiocbp->aio_fildes,
                        (void *) req->aiocbp->aio_buf,
                        req->aiocbp->aio_nbytes, req->aiocbp->aio_offset);
        break;

      case LIO_WRITE:
	AIO_printf ("write\n");
        result = pwrite (req->aiocbp->aio_fildes,
                         (void *) req->aiocbp->aio_buf,
                         req->aiocbp->aio_nbytes, req->aiocbp->aio_offset);
        break;
        
      case LIO_SYNC:
	AIO_printf ("sync\n");
      	result = fsync (req->aiocbp->aio_fildes);
      	break;

      default:
        result = -1;
      }
      if (result == -1) {
        req->aiocbp->return_value = -1;
	req->aiocbp->error_code = errno;
      } else {
        req->aiocbp->return_value = result;
        req->aiocbp->error_code = 0;
      }

      // notification needed for lio

    } else {
      /* If the fd chain is empty we unlock the fd chain
	 and we lock the queue chain, this will ensure that
	 we have at most one request comming to our fd chain
	 when we check. 
	 
	 If there was no request added sleep for 3 seconds and
	 wait for a signal on chain, this will unlock the queue.
	 The fd chain is already unlocked */

      struct timespec timeout;
      
      AIO_printf ("Chain is empty [WQ], wait for work\n");
     
      pthread_mutex_unlock (&r_chain->mutex);
      pthread_mutex_lock (&aio_request_queue.mutex);
      
      if (rtems_chain_is_empty (chain))
	{
	  clock_gettime (CLOCK_REALTIME, &timeout);
	  timeout.tv_sec += 3;
//.........这里部分代码省略.........
开发者ID:AlexShiLucky,项目名称:rtems,代码行数:101,代码来源:aio_misc.c


注:本文中的pwrite函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。