summaryrefslogtreecommitdiff
path: root/rdiff-backup/src/cmodule.c
blob: ea1041548d9ed1a7acdfb3cabb038ced952cd6b7 (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Python.h>
#include <errno.h>

static PyObject *c_make_file_dict(self, args)
	 PyObject *self;
	 PyObject *args;
{
  char *filename, filetype[5];
  struct stat sbuf;
  mode_t mode;

  if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
  if (lstat(filename, &sbuf) != 0) {
	if (errno == ENOENT || errno == ENOTDIR)
	  return Py_BuildValue("{s:s}", "type", NULL);
	else {
	  PyErr_SetFromErrno(PyExc_OSError);
	  return NULL;
	}
  }
  mode = sbuf.st_mode;

  /* Build return dictionary from stat struct */
  if (S_ISREG(mode) || S_ISDIR(mode) || S_ISSOCK(mode) || S_ISFIFO(mode)) {
	/* Regular files, directories, sockets, and fifos */
	if S_ISREG(mode) strcpy(filetype, "reg");
	else if S_ISDIR(mode) strcpy(filetype, "dir");
	else if S_ISSOCK(mode) strcpy(filetype, "sock");
	else strcpy(filetype, "fifo");
	return Py_BuildValue("{s:s,s:l,s:i,s:i,s:i,s:l,s:i,s:i,s:l,s:l}",
						 "type", filetype,
						 "size", (long int)sbuf.st_size,
						 "perms", (int)(mode & S_IRWXU),
						 "uid", (int)sbuf.st_uid,
						 "gid", (int)sbuf.st_gid,
						 "inode", (long int)sbuf.st_ino,
						 "devloc", (int)sbuf.st_dev,
						 "nlink", (int)sbuf.st_nlink,
						 "mtime", (long int)sbuf.st_mtime,
						 "atime", (long int)sbuf.st_atime);
  } else if S_ISLNK(mode) {
	/* Symbolic links */
	char linkname[1024];
	int len_link = readlink(filename, linkname, 1023);
	if (len_link < 0) {
	  PyErr_SetFromErrno(PyExc_OSError);
	  return NULL;
	}

	linkname[len_link] = '\0';
	return Py_BuildValue("{s:s,s:l,s:i,s:i,s:i,s:l,s:i,s:i,s:s}",
						 "type", "sym",
						 "size", (long int)sbuf.st_size,
						 "perms", (int)(mode & S_IRWXU),
						 "uid", (int)sbuf.st_uid,
						 "gid", (int)sbuf.st_gid,
						 "inode", (long int)sbuf.st_ino,
						 "devloc", (int)sbuf.st_dev,
						 "nlink", (int)sbuf.st_nlink,
						 "linkname", linkname);
  } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
	/* Device files */
	char devtype[2];
	int devnums = (int)sbuf.st_rdev;
	if S_ISCHR(mode) strcpy(devtype, "c");
	else strcpy(devtype, "b");
	return Py_BuildValue("{s:s,s:l,s:i,s:i,s:i,s:l,s:i,s:i,s:O}",
						 "type", "dev",
						 "size", (long int)sbuf.st_size,
						 "perms", (int)(mode & S_IRWXU),
						 "uid", (int)sbuf.st_uid,
						 "gid", (int)sbuf.st_gid,
						 "inode", (long int)sbuf.st_ino,
						 "devloc", (int)sbuf.st_dev,
						 "nlink", (int)sbuf.st_nlink,
						 "devnums", Py_BuildValue("(s,i,i)", devtype,
												  devnums >> 8,
												  devnums & 0xff),
						 "mtime", (long int)sbuf.st_mtime,
						 "atime", (long int)sbuf.st_atime);
  } else {
	/* Unrecognized file type - pretend it isn't there */
	errno = ENOENT;
	PyErr_SetFromErrno(PyExc_OSError);
	return NULL;
  }
}

static PyObject *long2str(self, args)
	 PyObject *self;
	 PyObject *args;
{
  unsigned char s[7];
  int sindex;
  unsigned long long int l;
  PyObject *pylong;

  if (!PyArg_ParseTuple(args, "O", &pylong)) return NULL;
  l = PyLong_AsUnsignedLongLong(pylong);
  for(sindex = 0; sindex <= 6; sindex++) {
	s[sindex] = l % 256;
	l /= 256;
  }
  return Py_BuildValue("s#", s, 7);
}

static PyObject *str2long(self, args)
	 PyObject *self;
	 PyObject *args;
{
  unsigned char *s;
  unsigned long long int l = 0;
  int sindex, ssize;

  if (!PyArg_ParseTuple(args, "s#", &s, &ssize)) return NULL;
  if (ssize != 7) return Py_BuildValue("i", -1);
  for(sindex=6; sindex >= 0; sindex--)
	l = l*256 + s[sindex];
  return PyLong_FromLongLong(l);
}

static PyMethodDef CMethods[] = {
  {"make_file_dict", c_make_file_dict, METH_VARARGS,
   "Make dictionary from file stat"},
  {"long2str", long2str, METH_VARARGS,
   "Convert long int to 7 byte string"},
  {"str2long", str2long, METH_VARARGS,
   "Convert 7 byte string to long int"},
  {NULL, NULL, 0, NULL}
};

void initC(void)
{
  (void) Py_InitModule("C", CMethods);
}