File: cow_sys.h

package info (click to toggle)
uml-utilities 20070815.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: ansic: 3,392; perl: 1,277; makefile: 239; exp: 129; sh: 122
file content (86 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download
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
#ifndef __COW_SYS_H__
#define __COW_SYS_H__

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

static inline void *cow_malloc(int size)
{
	return(malloc(size));
}

static inline void cow_free(void *ptr)
{
	free(ptr);
}

#define cow_printf printf

static inline char *cow_strdup(char *str)
{
	return(strdup(str));
}

static inline int cow_seek_file(int fd, __u64 offset)
{
	__u64 actual;

	actual = lseek64(fd, offset, SEEK_SET);
	if(actual != offset)
		return(-errno);
	return(0);
}

static inline int cow_file_size(char *file, unsigned long long *size_out)
{
	struct stat64 buf;

	if (stat64(file, &buf) == -1) {
		cow_printf("Couldn't stat \"%s\" : errno = %d\n", file, errno);
		return(-errno);
	}
	if(S_ISBLK(buf.st_mode)){
		int fd, blocks;

		if((fd = open(file, O_RDONLY)) < 0){
			cow_printf("Couldn't open \"%s\", errno = %d\n", file,
				   errno);
			return(-errno);
		}
		if(ioctl(fd, BLKGETSIZE, &blocks) < 0){
			cow_printf("Couldn't get the block size of \"%s\", "
				   "errno = %d\n", file, errno);
			close(fd);
			return(-errno);
		}
		*size_out = ((long long) blocks) * 512;
		close(fd);
		return(0);
	}
	*size_out = buf.st_size;
	return(0);
}

static inline int cow_write_file(int fd, void *buf, int size)
{
	int ret = write(fd, buf, size);

	if (ret == -1)
		return(-errno);
	return(ret);
}

#endif

/*
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */