]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_mangle_add.c
MFH:
[netatalk.git] / libatalk / cnid / cnid_mangle_add.c
1 /*
2  * $Id: cnid_mangle_add.c,v 1.4.2.1 2003-02-08 03:16:53 jmarcus Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #ifdef FILE_MANGLING
10 #include <stdio.h>
11 #include <string.h>
12 #include <sys/param.h>
13 #include <sys/stat.h>
14 #include <string.h>
15 #include <atalk/logger.h>
16 #include <errno.h>
17
18 #include <db.h>
19 #include <netatalk/endian.h>
20 #include <atalk/adouble.h>
21 #include <atalk/cnid.h>
22
23 #include "cnid_private.h"
24
25 #ifdef CNID_DB_CDB
26     #define tid    NULL
27 #endif /* CNID_DB_CDB */
28
29 /* Add a mangled filename. */
30 int
31 cnid_mangle_add(void *CNID, char *mfilename, char *filename)
32 {
33     CNID_private *db;
34     DBT key, data;
35 #ifndef CNID_DB_CDB
36     DB_TXN *tid;
37 #endif /* CNID_DB_CDB */
38     cnid_t id;
39     int rc, ret;
40
41     if (!(db = CNID)) {
42         return -1;
43     }
44
45     memset(&key, 0, sizeof(key));
46     memset(&data, 0, sizeof(data));
47
48     key.data = mfilename;
49     key.size = strlen(mfilename);
50     data.data = filename;
51     data.size = strlen(filename) + 1;
52
53 #ifndef CNID_DB_CDB
54 retry:
55     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
56         LOG(log_error, logtype_default, "cnid_mangle_add: Failed to begin transaction: %s", db_strerror(rc));
57         return -1;
58     }
59 #endif /* CNID_DB_CDB */
60
61     if ((rc = db->db_mangle->put(db->db_mangle, tid, &key, &data, 0))) {
62 #ifndef CNID_DB_CDB
63         if ((ret = txn_abort(tid)) != 0) {
64             LOG(log_error, logtype_default, "cnid_mangle_add: txn_abort: %s", db_strerror(ret));
65             return -1;
66         }
67 #endif /* CNID_DB_CDB */
68         switch (rc) {
69 #ifndef CNID_DB_CDB
70         case DB_LOCK_DEADLOCK:
71             goto retry;
72 #endif /* CNID_DB_CDB */
73         default:
74             LOG(log_error, logtype_default, "cnid_mangle_add: Failed to add mangled filename to the database: %s", db_strerror(rc));
75             return -1;
76         }
77     }
78
79 #ifndef CNID_DB_CDB
80     if ((rc = txn_commit(tid, 0)) != 0) {
81         LOG(log_error, logtype_default, "cnid_mangle_add: Unable to commit transaction: %s", db_strerror(rc));
82         return -1;
83     }
84 #endif /* CNID_DB_CDB */
85
86     return 0;
87 }
88 #endif /* FILE_MANGLING */