summaryrefslogtreecommitdiffstats
path: root/server/filer.c
blob: b8412b78189b23fd1341b0b9d3bed2028545819c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * filer.c - open, seeks in and reads from a file 
 * 
 * Copyright (C) 2006 Thorsten Zitterell <thorsten@zitterell.de>
 */
 
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>

#include "filer.h"

/* 
 * function filer_getcapacity()
 * returns: size/capacity of file/device 
 */
unsigned long long filer_getcapacity(filer_info_t * filer_info)
{
	return filer_info->size;
}

/* 
 * function filer_seekblock(): seek to position in file/block device 
 * returns: 1 on success, otherwise 0
 */
static inline int filer_seekblock(filer_info_t * filer_info, off_t newpos)
{
	if (lseek(filer_info->fd, newpos, SEEK_SET) == (off_t) -1) {
		return 0;
	}
	filer_info->pos = newpos;
	return 1;
}

/* 
 * function filer_readblock(): read bytes at specific position 
 * returns: 1 on success, otherwise 0
 */
inline int filer_readblock(filer_info_t * filer_info, void *buf, size_t size,
		     off_t pos)
{
	size_t remain = size;
	int result = 0;
	int numblocks = 0;
	
	if (!filer_seekblock(filer_info, pos)) goto leave;		
	
	while (remain > 0) {
		if ((numblocks = read(filer_info->fd, buf, remain)) <= 0) {
			if (errno == EINTR)
				continue;
			goto leave;
		}
		
		if (numblocks == 0) {
			goto leave;
		}
		remain -= numblocks;
		buf += numblocks;
	}
	result = 1;
      leave:
	filer_info->pos += (size - remain);
	return result;
}

/* 
 * function filer_init(): open file to be served
 * returns: data structure with file information 
 */
filer_info_t *filer_init(const char *filename)
{
	filer_info_t *filer_info;
	struct stat64 stbuf;

	filer_info = (filer_info_t *) malloc(sizeof(filer_info_t));
	if (!filer_info)
		return NULL;

	filer_info->filename = strdup(filename);
	if ((filer_info->fd = open(filename, O_RDONLY | O_LARGEFILE)) < 0) {
		fprintf(stderr, "ERROR: Cannot open filename \"%s\"\n",
			filename);
		goto out_free;
	}

	stbuf.st_size = 0;
	
	if (fstat64(filer_info->fd, &stbuf) < 0) {
		fprintf(stderr, "ERROR: Cannot stat file \"%s\"\n",
			filename);
		goto out_free;
	}

	/* get file/device size */
	if ((filer_info->size = stbuf.st_size) == 0) {
		filer_info->size = lseek64(filer_info->fd, (off_t) 0, SEEK_END);
	}

	if (filer_info->size == 0) {
	    fprintf(stderr, "ERROR: File/device has zero size\n");
		goto out_free;
	}

	goto out;

      out_free:
	if (filer_info)
		free(filer_info);

	filer_info = NULL;
      out:
	return filer_info;
}