Fix error messages that are actually errno.
[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 # Error handling
30
31 class AFSException(Exception):
32     def __init__(self, errno):
33         self.errno = errno
34         self.strerror = afs_error_message(errno)
35
36     def __repr__(self):
37         return "AFSException(%s)" % (self.errno)
38
39     def __str__(self):
40         return "[%s] %s" % (self.errno, self.strerror)
41
42 def pyafs_error(code):
43     if not _init:
44         initialize_ACFG_error_table()
45         initialize_KTC_error_table()
46         initialize_PT_error_table()
47         initialize_RXK_error_table()
48         initialize_U_error_table()
49
50         _init = 1
51
52     if code != 0:
53         raise AFSException(code)