forked from ChrisJohnsen/gibak
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ometastore_stub.c
158 lines (122 loc) · 3.68 KB
/
ometastore_stub.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Copyright (C) 2008 Mauricio Fernandez <[email protected]> http//eigenclass.org
* See README.txt and LICENSE for the redistribution and modification terms */
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/alloc.h>
#include <fnmatch.h>
#include <stdio.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <utime.h>
#include <string.h>
#if defined(HAVE_LINUX_XATTR) || defined(HAVE_OSX_XATTR)
#include <sys/xattr.h>
#endif
#ifdef HAVE_LINUX_XATTR
#define LLISTXATTR(f, buf, len) (llistxattr(f, buf, len))
#define LGETXATTR(f, nam, buf, len) (lgetxattr(f, nam, buf, len))
#define LSETXATTR(f, nam, v, len) lsetxattr(f, nam, v, len, 0)
#define LREMOVEXATTR(f, nam) lremovexattr(f, nam)
#elif defined(HAVE_OSX_XATTR)
#define LLISTXATTR(f, buf, len) (listxattr(f, buf, len, XATTR_NOFOLLOW))
#define LGETXATTR(f, nam, buf, len) (getxattr(f, nam, buf, len, 0, XATTR_NOFOLLOW))
#define LSETXATTR(f, nam, v, len) (setxattr(f, nam, v, len, 0, XATTR_NOFOLLOW))
#define LREMOVEXATTR(f, nam) removexattr(f, nam, XATTR_NOFOLLOW)
#endif
CAMLprim value perform_fnmatch(value fnm_pathname, value pattern, value string)
{
char *patt = String_val(pattern);
if(*patt == '/') patt++;
return Val_bool(!fnmatch(patt, String_val(string),
Bool_val(fnm_pathname) ? FNM_PATHNAME : 0));
}
CAMLprim value perform_utime(value file, value time)
{
struct utimbuf tbuf;
tbuf.actime = Nativeint_val(time);
tbuf.modtime = Nativeint_val(time);
if(utime(String_val(file), &tbuf)) {
printf("utime on %s to %"ARCH_INTNAT_PRINTF_FORMAT"d failed, error %i: %s\n", String_val(file), Nativeint_val(time), errno, strerror(errno));
caml_failwith("utime");
}
return(Val_int(0));
}
#ifdef LLISTXATTR
CAMLprim value perform_llistxattr(value file)
{
CAMLparam1(file);
CAMLlocal2(l, prev);
ssize_t siz, i;
char *p, *porig;
siz = LLISTXATTR(String_val(file), NULL, 0);
if (siz == 0 || errno == EPERM || errno == EACCES)
CAMLreturn(Val_int(0));
if(siz < 0) {
printf("llistxattr on %s failed, error %i: %s\n", String_val(file), errno, strerror(errno));
caml_failwith("llistxattr");
}
porig = p = malloc(siz);
siz = LLISTXATTR(String_val(file), p, siz);
if(siz < 0) {
free(p);
caml_failwith("llistxattr");
}
prev = Val_int(0);
for(i = 0; i < siz;) {
l = caml_alloc(2, 0);
Store_field(l, 0, caml_copy_string(p));
Store_field(l, 1, prev);
prev = l;
while(*p++) /* skip */ i++;
++i;
}
free(porig);
CAMLreturn(l);
}
CAMLprim value perform_lgetxattr(value file, value name)
{
CAMLparam2(file, name);
CAMLlocal1(ret);
ssize_t siz;
siz = LGETXATTR(String_val(file), String_val(name), NULL, 0);
if(siz < 0)
caml_failwith("lgetxattr");
ret = caml_alloc_string(siz);
if(LGETXATTR(String_val(file), String_val(name), String_val(ret), siz) < 0) {
caml_failwith("lgetxattr");
}
CAMLreturn(ret);
}
CAMLprim value perform_lsetxattr(value file, value name, value val)
{
CAMLparam3(file, name, val);
if(LSETXATTR(String_val(file), String_val(name), String_val(val), string_length(val)))
caml_failwith("lsetxattr");
CAMLreturn(Val_unit);
}
CAMLprim value perform_lremovexattr(value file, value name)
{
CAMLparam2(file, name);
if(LREMOVEXATTR(String_val(file), String_val(name)))
caml_failwith("lremovexattr");
CAMLreturn(Val_unit);
}
#else
CAMLprim value perform_llistxattr(value file)
{
return(Val_int(0));
}
CAMLprim value perform_lgetxattr(value file, value name)
{
caml_failwith("lgetxattr");
}
CAMLprim value perform_lsetxattr(value file, value name, value val)
{
caml_failwith("lsetxattr");
}
CAMLprim value perform_lremovexattr(value file, value name)
{
caml_failwith("lremovexattr");
}
#endif