1/*
2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2009,2010
4 * All Rights Reserved.  See COPYING.
5 */
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif /* HAVE_CONFIG_H */
10
11#include <string.h>
12#include <errno.h>
13#include <atalk/logger.h>
14#include <netatalk/endian.h>
15#include <atalk/cnid_dbd_private.h>
16
17
18#include "pack.h"
19#include "dbif.h"
20#include "dbd.h"
21
22
23/*
24   cnid_update: takes the given cnid and updates the metadata.
25   First, delete given CNID, then re-insert.
26*/
27
28int dbd_update(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
29{
30    DBT key, data;
31
32    memset(&key, 0, sizeof(key));
33    memset(&data, 0, sizeof(data));
34    rply->namelen = 0;
35
36    /* Try to wipe everything, also using the indexes */
37    if (dbd_delete(dbd, rqst, rply, DBIF_CNID) < 0)
38        goto err_db;
39    if (dbd_delete(dbd, rqst, rply, DBIF_IDX_DEVINO) < 0)
40        goto err_db;
41    if (dbd_delete(dbd, rqst, rply, DBIF_IDX_DIDNAME) < 0)
42        goto err_db;
43
44    /* Make a new entry. */
45    key.data = &rqst->cnid;
46    key.size = sizeof(rqst->cnid);
47    data.data = pack_cnid_data(rqst);
48    data.size = CNID_HEADER_LEN + rqst->namelen + 1;
49    memcpy(data.data, &rqst->cnid, sizeof(rqst->cnid));
50
51    if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
52        goto err_db;
53
54    LOG(log_debug, logtype_cnid, "dbd_update: Updated dbd with dev/ino: 0x%llx/0x%llx, did: %u, name: %s, cnid: %u",
55        (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rqst->did), rqst->name, ntohl(rqst->cnid));
56
57    rply->result = CNID_DBD_RES_OK;
58    return 1;
59
60err_db:
61    LOG(log_error, logtype_cnid, "dbd_update: Unable to update CNID: %u, dev/ino: 0x%llx/0x%llx, DID: %u: %s",
62        ntohl(rqst->cnid), (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rqst->did), rqst->name);
63
64    rply->result = CNID_DBD_RES_ERR_DB;
65    return -1;
66}
67