Пример #1
0
off_t fs_size(const char *path) {
	off_t size;
	rp_t file;

	file = fs_find(path);
	size = rp_size(file);

	return size;
}
Пример #2
0
void *load_exec(const char *name) {
	int fd;
	uint64_t size;
	char *path;
	void *image;

	/* attempt to find requested file */
	if (name[0] == '/' || name[0] == '@') {
		path = strdup(name);
	}
	else {
		path = strvcat(getenv("PATH"), "/", name, NULL);
	}

	fd = ropen(-1, fs_find(path), STAT_READER);

	if (fd < 0 || !rp_type(fd_rp(fd), "file")) {
		/* file not found */
		return NULL;
	}
	else {
		/* read whole file into buffer */
		size = rp_size(fd_rp(fd));

		if (!size) {
			return NULL;
		}

		image = aalloc(size, PAGESZ);
		
		if (!image) {
			return NULL;
		}

		if (rp_read(fd_rp(fd), image, size, 0) != size) {
			free(image);
			close(fd);
			return NULL;
		}

		close(fd);
		return image;
	}
}
Пример #3
0
FILE *fdopen(int fd, const char *mode) {
	FILE *stream;
	int status;
	
	if (fd < 0) {
		return NULL;
	}

	// check mode
	if (mode[0] != 'a' && mode[0] != 'w' && mode[0] != 'r') {
		errno = EINVAL;
		return NULL;
	}

	// check if the object is not a file
	if (!rp_type(fd_rp(fd), "file")) {
		errno = EISDIR;
		return NULL;
	}

	status = 0;

	// check read permissions
	if (mode[0] == 'r' || mode[1] == '+') {
		status |= STAT_READER;
	}

	// check write permissions
	if (mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
		status |= STAT_WRITER;
	}

	// open file for real
	fd = ropen(fd, fd_rp(fd), status);
	if (fd < 0) {
		return NULL;
	}

	// reset (erase) the file contents
	if (mode[0] == 'w') {
		rp_reset(fd_rp(fd));
	}

	stream = calloc(sizeof(FILE), 1);

	if (!stream) {
		errno = ENOMEM;
		return NULL;
	}

	stream->fd       = fd;
	stream->mutex    = false;
	stream->position = 0;
	stream->size     = rp_size(fd_rp(fd));
	stream->buffer   = NULL;
	stream->buffsize = 0;
	stream->buffpos  = 0;
	stream->revbuf   = EOF;
	stream->flags    = FILE_NBF | FILE_READ;

	if (mode[0] == 'w' || mode[0] == 'a' || mode[1] == '+') {
		stream->flags |= FILE_WRITE;
	}

	// position the stream properly
	if (mode[0] == 'a' && mode[1] != '+') {
		fseek(stream, 0, SEEK_END);
	}
	else {
		fseek(stream, 0, SEEK_SET);
	}

	return stream;
}