Add an fs.inafs function for determining if a path is in AFS at all.
[invirt/packages/python-afs.git] / afs / afs.pyx
1 """
2 General PyAFS utilities, such as error handling
3 """
4
5 import sys
6
7 # otherwise certain headers are unhappy
8 cdef import from "netinet/in.h": pass
9 cdef import from "afs/vice.h": pass
10
11 cdef int _init = 0
12
13 # pioctl convenience wrappers
14
15 cdef extern int pioctl_read(char *dir, afs_int32 op, void *buffer, unsigned short size, afs_int32 follow) except -1:
16     cdef ViceIoctl blob
17     cdef afs_int32 code
18     blob.in_size  = 0
19     blob.out_size = size
20     blob.out = buffer
21     code = pioctl(dir, op, &blob, follow)
22     # This might work with the rest of OpenAFS, but I'm not convinced
23     # the rest of it is consistent
24     if code == -1:
25         raise OSError(errno, strerror(errno))
26     pyafs_error(code)
27     return code
28
29 cdef extern int pioctl_write(char *dir, afs_int32 op, char *buffer, afs_int32 follow) except -1:
30     cdef ViceIoctl blob
31     cdef afs_int32 code
32     blob.cin = buffer
33     blob.in_size = 1 + strlen(buffer)
34     blob.out_size = 0
35     code = pioctl(dir, op, &blob, follow)
36     # This might work with the rest of OpenAFS, but I'm not convinced
37     # the rest of it is consistent
38     if code == -1:
39         raise OSError(errno, strerror(errno))
40     pyafs_error(code)
41     return code
42
43 # Error handling
44
45 class AFSException(Exception):
46     def __init__(self, errno):
47         self.errno = errno
48         self.strerror = afs_error_message(errno)
49
50     def __repr__(self):
51         return "AFSException(%s)" % (self.errno)
52
53     def __str__(self):
54         return "[%s] %s" % (self.errno, self.strerror)
55
56 def pyafs_error(code):
57     if not _init:
58         initialize_ACFG_error_table()
59         initialize_KTC_error_table()
60         initialize_PT_error_table()
61         initialize_RXK_error_table()
62         initialize_U_error_table()
63
64         _init = 1
65
66     if code != 0:
67         raise AFSException(code)