Esempio n. 1
0
void
fs_test(void)
{
	struct File *f;
	int r;
	char *blk;
	uint32_t *bits;

	// back up bitmap
	if ((r = sys_page_alloc(0, (void*) PGSIZE, PTE_P|PTE_U|PTE_W)) < 0)
		panic("sys_page_alloc: %e", r);
	bits = (uint32_t*) PGSIZE;
	memmove(bits, bitmap, PGSIZE);
	// allocate block
	if ((r = alloc_block()) < 0)
		panic("alloc_block: %e", r);
	// check that block was free
	assert(bits[r/32] & (1 << (r%32)));
	// and is not free any more
	assert(!(bitmap[r/32] & (1 << (r%32))));
	cprintf("alloc_block is good\n");

	if ((r = file_open("/not-found", &f)) < 0 && r != -E_NOT_FOUND)
		panic("file_open /not-found: %e", r);
	else if (r == 0)
		panic("file_open /not-found succeeded!");
	if ((r = file_open("/newmotd", &f)) < 0)
		panic("file_open /newmotd: %e", r);
	cprintf("file_open is good\n");

	if ((r = file_get_block(f, 0, &blk)) < 0)
		panic("file_get_block: %e", r);
	if (strcmp(blk, msg) != 0)
		panic("file_get_block returned wrong data");
	cprintf("file_get_block is good\n");

	*(volatile char*)blk = *(volatile char*)blk;
	assert((vpt[PPN(blk)] & PTE_D));
	file_flush(f);
	assert(!(vpt[PPN(blk)] & PTE_D));
	cprintf("file_flush is good\n");

	if ((r = file_set_size(f, 0)) < 0)
		panic("file_set_size: %e", r);
	assert(f->f_direct[0] == 0);
	assert(!(vpt[PPN(f)] & PTE_D));
	cprintf("file_truncate is good\n");

	if ((r = file_set_size(f, strlen(msg))) < 0)
		panic("file_set_size 2: %e", r);
	assert(!(vpt[PPN(f)] & PTE_D));
	if ((r = file_get_block(f, 0, &blk)) < 0)
		panic("file_get_block 2: %e", r);
	strcpy(blk, msg);
	assert((vpt[PPN(blk)] & PTE_D));
	file_flush(f);
	assert(!(vpt[PPN(blk)] & PTE_D));
	assert(!(vpt[PPN(f)] & PTE_D));
	cprintf("file rewrite is good\n");
}
Esempio n. 2
0
File: fs.c Progetto: ren85/jos2006
// Close a file.
void
file_close(struct File *f)
{
	file_flush(f);
	if (f->f_dir)
		file_flush(f->f_dir);
}
Esempio n. 3
0
/*!
 * Flushes any modified internal buffers.
 *
 * \see close
 *
 */
void file::flush()
{
	if (!is_open())
		throw std::logic_error("file::flush  not open");

	file_flush(_handle);
}
Esempio n. 4
0
/*!
 * \internal
 * TODO: write documentation
 * Closes a file.
 */
void file_close(file_handle* f)
{
	// f should never be null, so we assert that in debug builds
	// and simply return in release-builds.
	assert(f);
	if (!f)
		return;
	assert(f->inode);

	debug::tracer DT;
	DT << "closing file with ino " << f->inode->ino;

	// TODO: Until we've fixed fs_close_inode to be noexcept,
	//       we use this sentry to ensure that f is always deleted.
	struct close_sentry {
		file_handle* _f;
		close_sentry(file_handle* f) : _f(f) { }
		~close_sentry() {
			delete _f;
		}
	} cs(f);

	file_flush(f);

	fs_close_inode(f->inode);
}
Esempio n. 5
0
File: fs.c Progetto: ren85/jos2006
int
file_set_size(struct File *f, off_t newsize)
{
	if (f->f_size > newsize)
		file_truncate_blocks(f, newsize);
	f->f_size = newsize;
	if (f->f_dir)
		file_flush(f->f_dir);
	return 0;
}
Esempio n. 6
0
File: read.c Progetto: jlgale/dogfs2
int
dogfs_read(const char *path, char *buf, size_t len, off_t off,
           struct fuse_file_info *fi)
{
#ifdef SIZE_STATS
    histogram_add(&reads, len);
#endif
    file_t *f = get_file(fi);
    int r = file_flush(f);
    if (r < 0)
        return r;
    return run_with_connection(inode_read, f->i, buf, len, off, true);
}
Esempio n. 7
0
// Flush all data and metadata of req->req_fileid to disk.
int
serve_flush(envid_t envid, struct Fsreq_flush *req)
{
	struct OpenFile *o;
	int r;

	if (debug)
		cprintf("serve_flush %08x %08x\n", envid, req->req_fileid);

	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	file_flush(o->o_file);
	return 0;
}
Esempio n. 8
0
/*
 * Read the notes. Every new savefile has at least NOTES_MARK.
 */
static bool rd_notes(void)
{
	int alive = (!p_ptr->is_dead || arg_wizard);
	char tmpstr[100];

	if (alive && adult_take_notes)
	{
		/* Create the tempfile (notes_file & notes_fname are global) */
		create_notes_file();

		if (!notes_file)
		{
			note("Can't create a temporary file for notes");
			return (TRUE);
		}

		/* Append the notes in the savefile to the tempfile*/
		while (TRUE)
		{

			rd_string(tmpstr, sizeof(tmpstr));
			/* Found the end? */
			if (strstr(tmpstr, NOTES_MARK))
			break;
			file_putf(notes_file, "%s\n", tmpstr);
		}

		/* Paranoia. Remove the notes from memory */
		file_flush(notes_file);

	}
	/* Ignore the notes */
	else
	{
		while (TRUE)
		{

			rd_string(tmpstr, sizeof(tmpstr));

			/* Found the end? */
			if (strstr(tmpstr, NOTES_MARK))
			{
				break;
			}
		}
	}

	return (FALSE);
}
Esempio n. 9
0
File: fs.c Progetto: ren85/jos2006
// Remove a file by truncating it and then zeroing the name.
int
file_remove(const char *path)
{
	int r;
	struct File *f;

	if ((r = walk_path(path, 0, &f, 0)) < 0)
		return r;

	file_truncate_blocks(f, 0);
	f->f_name[0] = '\0';
	f->f_size = 0;
	if (f->f_dir)
		file_flush(f->f_dir);

	return 0;
}
Esempio n. 10
0
void write_long(FIL *fp, uint32_t value)
{
    if (file_buffer_pointer) {
        // We get a massive speed boost by buffering up as much data as possible
        // before a write to the SD card. So much so that the time wasted by
        // all these operations does not cost us.
        for (size_t i = 0; i < sizeof(value); i++) {
            file_buffer_pointer[file_buffer_index++] = ((uint8_t *) &value)[i];
            file_flush(fp);
        }
    } else {
        UINT bytes;
        FRESULT res = f_write(fp, &value, sizeof(value), &bytes);
        if (res != FR_OK) ff_fail(fp, res);
        if (bytes != sizeof(value)) ff_write_fail(fp);
    }
}
Esempio n. 11
0
// Create "path".  On success set *pf to point at the file and return 0.
// On error return < 0.
int
file_create(const char *path, struct File **pf)
{
	char name[MAXNAMELEN];
	int r;
	struct File *dir, *f;

	if ((r = walk_path(path, &dir, &f, name)) == 0)
		return -E_FILE_EXISTS;
	if (r != -E_NOT_FOUND || dir == 0)
		return r;
	if (dir_alloc_file(dir, &f) < 0)
		return r;
	strcpy(f->f_name, name);
	*pf = f;
	file_flush(dir);
	return 0;
}
Esempio n. 12
0
CELL *
bi_fflush(CELL * sp)
{
    int ret = 0;

    if (sp->type == 0)
	fflush(stdout);
    else {
	sp--;
	if (sp->type < C_STRING)
	    cast1_to_s(sp);
	ret = file_flush(string(sp));
	free_STRING(string(sp));
    }

    sp->type = C_DOUBLE;
    sp->dval = (double) ret;
    return sp;
}
Esempio n. 13
0
int gsiftp_flush(int handle)
{
  FILE *gsiftpfile;
  int num_streams;

  if (getenv("GSIFTP_STREAMS")) {
    num_streams = (int)getenv("GSIFTP_STREAMS");
  } else {
    num_streams = 1;
  }
  
  int rc = file_flush(handle);

  if (gsiftpopen != 1) {
  
    if (setjmp(env) != 0) {
      ffpmsg("Timeout (gsiftp_write)");
      goto error;
    }
  
    signal(SIGALRM, signal_handler);
    alarm(NETTIMEOUT);

    if (gsiftp_put(gsiftpurl,&gsiftpfile,num_streams)) {
      alarm(0);
      ffpmsg("Unable to open gsiftp file (gsiftp_flush)");
      ffpmsg(gsiftpurl);
      goto error;
    } 
  
    fclose(gsiftpfile);
  
    signal(SIGALRM, SIG_DFL);
    alarm(0);
  }
  
  return rc;

  error:
   alarm(0);
   signal(SIGALRM, SIG_DFL);
   return (FILE_NOT_OPENED);
}
Esempio n. 14
0
// Write req->req_n bytes from req->req_buf to req_fileid, starting at
// the current seek position, and update the seek position
// accordingly.  Extend the file if necessary.  Returns the number of
// bytes written, or < 0 on error.
int
serve_write(envid_t envid, struct Fsreq_write *req)
{
	struct OpenFile *o;
	int r = -1;
	int count = 0;
	if (debug)
		cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// LAB 5: Your code here.
	//panic("serve_write not implemented");
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	//cprintf("\ncopying data\n");
	count = file_write(o->o_file, req->req_buf, req->req_n, o->o_fd->fd_offset);
	o->o_fd->fd_offset += count; 
	file_flush(o->o_file);
	return count;
}
Esempio n. 15
0
// Read at most ipc->read.req_n bytes from the current seek position
// in ipc->read.req_fileid.  Return the bytes read from the file to
// the caller in ipc->readRet, then update the seek position.  Returns
// the number of bytes successfully read, or < 0 on error.
int
serve_read(envid_t envid, union Fsipc *ipc)
{
	struct Fsreq_read *req = &ipc->read;
	struct Fsret_read *ret = &ipc->readRet;
	struct OpenFile *o;
	int r;
	int count=0;

	if (debug)
		cprintf("serve_read %08x %08x %08x\n", envid, req->req_fileid, req->req_n);

	// Lab 5: Your code here:
	if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0)
		return r;
	count = file_read(o->o_file, ret->ret_buf, req->req_n, o->o_fd->fd_offset);
	o->o_fd->fd_offset += count; 
	file_flush(o->o_file);
	return count;
}
Esempio n. 16
0
/*!
 * \internal
 * Marks a file's internal buffer as dirty.
 *
 * \param f  a pointer to the file_handle
 *
 * \throws fs_error               thrown if a filesystem error occurs
 * \throws std::invalid_argument  thrown if \a f is null
 *
 */
void file_mark_dirty(file_handle* f)
{
	if (!f)
		throw std::invalid_argument("file_mark_dirty  null arg: f");

	if (f->rw_buf_dirty)
		return;

	debug::tracer DT;
	DT << "marking file dirty...";

	f->rw_buf_dirty = true;

	assert(f->inode);
	assert(f->inode->fs);

	if (f->inode->fs->auto_flush) {
		DT << "auto-flushing enabled...";
		file_flush(f);
	}
}
Esempio n. 17
0
void write_data(FIL *fp, const void *data, UINT size)
{
    if (file_buffer_pointer) {
        // We get a massive speed boost by buffering up as much data as possible
        // before a write to the SD card. So much so that the time wasted by
        // all these operations does not cost us.
        while (size) {
            uint32_t file_buffer_space_left = file_buffer_size - file_buffer_index;
            uint32_t can_do = FF_MIN(size, file_buffer_space_left);
            memcpy(file_buffer_pointer+file_buffer_index, data, can_do);
            file_buffer_index += can_do;
            data += can_do;
            size -= can_do;
            file_flush(fp);
        }
    } else {
        UINT bytes;
        FRESULT res = f_write(fp, data, size, &bytes);
        if (res != FR_OK) ff_fail(fp, res);
        if (bytes != size) ff_write_fail(fp);
    }
}
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    size_t write_size;
//        printf("write_data %d\n",i);
    /*
        if (i == 5) {
    		sleep(5);
        }
        i++;
    */
    curl_buffer_t cbuf = (curl_buffer_t)stream;
    write_size = file_write(ptr, cbuf->offset, size*nmemb, cbuf->file);
    if (write_size != size*nmemb) {
        printf("write _ data error......\n");
    }
    cbuf->offset += write_size;
    file_flush(cbuf->file);

    if (is_going_to_next(curl_flag) || is_going_to_close(curl_flag)) {
        return -1;
    }

    return write_size;
}
Esempio n. 19
0
int file_write(file_t file, void *buffer, int amount) {
  int rv = PR_Write((PRFileDesc*)file, buffer, amount) == amount;
  file_flush(file);
  return rv;
}
Esempio n. 20
0
static int modfile_fflush( INSTANCE * my, int * params )
{
    return file_flush(( file * )params[0] ) ;
}
Esempio n. 21
0
int
rd_wrfile(ARCHD *arcn, int ofd, off_t *left)
{
	int cnt = 0;
	off_t size = arcn->sb.st_size;
	int res = 0;
	char *fnm = arcn->name;
	int isem = 1;
	int rem;
	int sz = MINFBSZ;
	struct stat sb;
	u_int32_t crc = 0;

	/*
	 * pass the blocksize of the file being written to the write routine,
	 * if the size is zero, use the default MINFBSZ
	 */
	if (ofd < 0)
		sz = PAXPATHLEN + 1;		/* GNU tar long link/file */
	else if (fstat(ofd, &sb) == 0) {
		if (sb.st_blksize > 0)
			sz = (int)sb.st_blksize;
	} else
		syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
	rem = sz;
	*left = 0L;

	/*
	 * Copy the archive to the file the number of bytes specified. We have
	 * to assume that we want to recover file holes as none of the archive
	 * formats can record the location of file holes.
	 */
	while (size > 0L) {
		cnt = bufend - bufpt;
		/*
		 * if we get a read error, we do not want to skip, as we may
		 * miss a header, so we do not set left, but if we get a write
		 * error, we do want to skip over the unprocessed data.
		 */
		if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))
			break;
		cnt = MIN(cnt, size);
		if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {
			*left = size;
			break;
		}

		if (docrc) {
			/*
			 * update the actual crc value
			 */
			cnt = res;
			while (--cnt >= 0)
				crc += *bufpt++ & 0xff;
		} else
			bufpt += res;
		size -= res;
	}

	/*
	 * if the last block has a file hole (all zero), we must make sure this
	 * gets updated in the file. We force the last block of zeros to be
	 * written. just closing with the file offset moved forward may not put
	 * a hole at the end of the file.
	 */
	if (isem && (arcn->sb.st_size > 0L))
		file_flush(ofd, fnm, isem);

	/*
	 * if we failed from archive read, we do not want to skip
	 */
	if ((size > 0L) && (*left == 0L))
		return(-1);

	/*
	 * some formats record a crc on file data. If so, then we compare the
	 * calculated crc to the crc stored in the archive
	 */
	if (docrc && (size == 0L) && (arcn->crc != crc))
		paxwarn(1,"Actual crc does not match expected crc %s",arcn->name);
	return(0);
}
Esempio n. 22
0
void
cp_file(ARCHD *arcn, int fd1, int fd2)
{
	int cnt;
	off_t cpcnt = 0L;
	int res = 0;
	char *fnm = arcn->name;
	int no_hole = 0;
	int isem = 1;
	int rem;
	int sz = MINFBSZ;
	struct stat sb;

	/*
	 * check for holes in the source file. If none, we will use regular
	 * write instead of file write.
	 */
	 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)
		++no_hole;

	/*
	 * pass the blocksize of the file being written to the write routine,
	 * if the size is zero, use the default MINFBSZ
	 */
	if (fstat(fd2, &sb) == 0) {
		if (sb.st_blksize > 0)
			sz = sb.st_blksize;
	} else
		syswarn(0,errno,"Unable to obtain block size for file %s",fnm);
	rem = sz;

	/*
	 * read the source file and copy to destination file until EOF
	 */
	for (;;) {
		if ((cnt = read(fd1, buf, blksz)) <= 0)
			break;
		if (no_hole)
			res = write(fd2, buf, cnt);
		else
			res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);
		if (res != cnt)
			break;
		cpcnt += cnt;
	}

	/*
	 * check to make sure the copy is valid.
	 */
	if (res < 0)
		syswarn(1, errno, "Failed write during copy of %s to %s",
			arcn->org_name, arcn->name);
	else if (cpcnt != arcn->sb.st_size)
		paxwarn(1, "File %s changed size during copy to %s",
			arcn->org_name, arcn->name);
	else if (fstat(fd1, &sb) < 0)
		syswarn(1, errno, "Failed stat of %s", arcn->org_name);
	else if (arcn->sb.st_mtime != sb.st_mtime)
		paxwarn(1, "File %s was modified during copy to %s",
			arcn->org_name, arcn->name);

	/*
	 * if the last block has a file hole (all zero), we must make sure this
	 * gets updated in the file. We force the last block of zeros to be
	 * written. just closing with the file offset moved forward may not put
	 * a hole at the end of the file.
	 */
	if (!no_hole && isem && (arcn->sb.st_size > 0L))
		file_flush(fd2, fnm, isem);
}