Exemple #1
0
void
umain(void)
{
	int r;
	int fileid;
	struct Fd *fd;

	if ((r = fsipc_open("/not-found", O_RDONLY, FVA)) < 0 && r != -E_NOT_FOUND)
		panic("serve_open /not-found: %e", r);
	else if (r == 0)
		panic("serve_open /not-found succeeded!");

	if ((r = fsipc_open("/newmotd", O_RDONLY, FVA)) < 0)
		panic("serve_open /newmotd: %e", r);
	fd = (struct Fd*) FVA;
	if (strlen(msg) != fd->fd_file.file.f_size)
		panic("serve_open returned size %d wanted %d\n", fd->fd_file.file.f_size, strlen(msg));
	cprintf("serve_open is good\n");

	if ((r = fsipc_map(fd->fd_file.id, 0, UTEMP)) < 0)
		panic("serve_map: %e", r);
	if (strecmp(UTEMP, msg) != 0)
		panic("serve_map returned wrong data");
	cprintf("serve_map is good\n");

	if ((r = fsipc_close(fd->fd_file.id)) < 0)
		panic("serve_close: %e", r);
	cprintf("serve_close is good\n");
	fileid = fd->fd_file.id;
	sys_page_unmap(0, (void*) FVA);

	if ((r = fsipc_map(fileid, 0, UTEMP)) != -E_INVAL)
		panic("serve_map does not handle stale fileids correctly");
	cprintf("stale fileid is good\n");
}
Exemple #2
0
// Open a file (or directory),
// returning the file descriptor index on success, < 0 on failure.
int
open(const char *path, int mode)
{
	// Find an unused file descriptor page using fd_alloc.
	// Then send a message to the file server to open a file
	// using a function in fsipc.c.
	// (fd_alloc does not allocate a page, it just returns an
	// unused fd address.  Do you need to allocate a page?  Look
	// at fsipc.c if you aren't sure.)
	// Then map the file data (you may find fmap() helpful).
	// Return the file descriptor index.
	// If any step fails, use fd_close to free the file descriptor.

	// LAB 5: Your code here.
	
	int r;
	struct Fd *fd;

	if ((r = fd_alloc(&fd)) < 0)
		return r;
	
	if ((r = fsipc_open(path, mode, fd)) < 0){
		fd_close(fd, 0);	
		return r;
	}

	if ((r = fmap(fd, 0, fd->fd_file.file.f_size) ) < 0){
		fd_close(fd, 0);	
		return r;
	}	
	
	return fd2num(fd);
}