~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/os_win32/os_fid.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1996-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 */
7
 
 
8
 
#include "db_config.h"
9
 
 
10
 
#ifndef lint
11
 
static const char revid[] = "$Id$";
12
 
#endif /* not lint */
13
 
 
14
 
#include "db_int.h"
15
 
 
16
 
#define SERIAL_INIT     0
17
 
static u_int32_t fid_serial = SERIAL_INIT;
18
 
 
19
 
/*
20
 
 * __os_fileid --
21
 
 *      Return a unique identifier for a file.
22
 
 */
23
 
int
24
 
__os_fileid(dbenv, fname, unique_okay, fidp)
25
 
        DB_ENV *dbenv;
26
 
        const char *fname;
27
 
        int unique_okay;
28
 
        u_int8_t *fidp;
29
 
{
30
 
        size_t i;
31
 
        u_int32_t tmp;
32
 
        u_int8_t *p;
33
 
        int ret;
34
 
 
35
 
        /*
36
 
         * The documentation for GetFileInformationByHandle() states that the
37
 
         * inode-type numbers are not constant between processes.  Actually,
38
 
         * they are, they're the NTFS MFT indexes.  So, this works on NTFS,
39
 
         * but perhaps not on other platforms, and perhaps not over a network.
40
 
         * Can't think of a better solution right now.
41
 
         */
42
 
        DB_FH fh;
43
 
        BY_HANDLE_FILE_INFORMATION fi;
44
 
        BOOL retval = FALSE;
45
 
 
46
 
        DB_ASSERT(fname != NULL);
47
 
 
48
 
        /* Clear the buffer. */
49
 
        memset(fidp, 0, DB_FILE_ID_LEN);
50
 
 
51
 
        /*
52
 
         * Initialize/increment the serial number we use to help avoid
53
 
         * fileid collisions.  Note that we don't bother with locking;
54
 
         * it's unpleasant to do from down in here, and if we race on
55
 
         * this no real harm will be done, since the finished fileid
56
 
         * has so many other components.
57
 
         *
58
 
         * We increment by 100000 on each call as a simple way of
59
 
         * randomizing;  simply incrementing seems potentially less useful
60
 
         * if pids are also simply incremented, since this is process-local
61
 
         * and we may be one of a set of processes starting up.  100000
62
 
         * pushes us out of pid space on most platforms, and has few
63
 
         * interesting properties in base 2.
64
 
         */
65
 
        if (fid_serial == SERIAL_INIT)
66
 
                __os_id(&fid_serial);
67
 
        else
68
 
                fid_serial += 100000;
69
 
 
70
 
        /*
71
 
         * First we open the file, because we're not given a handle to it.
72
 
         * If we can't open it, we're in trouble.
73
 
         */
74
 
        if ((ret = __os_open(dbenv, fname, DB_OSO_RDONLY, _S_IREAD, &fh)) != 0)
75
 
                return (ret);
76
 
 
77
 
        /* File open, get its info */
78
 
        if ((retval = GetFileInformationByHandle(fh.handle, &fi)) == FALSE)
79
 
                ret = __os_win32_errno();
80
 
        __os_closehandle(dbenv, &fh);
81
 
 
82
 
        if (retval == FALSE)
83
 
                return (ret);
84
 
 
85
 
        /*
86
 
         * We want the three 32-bit words which tell us the volume ID and
87
 
         * the file ID.  We make a crude attempt to copy the bytes over to
88
 
         * the callers buffer.
89
 
         *
90
 
         * We don't worry about byte sexing or the actual variable sizes.
91
 
         *
92
 
         * When this routine is called from the DB access methods, it's only
93
 
         * called once -- whatever ID is generated when a database is created
94
 
         * is stored in the database file's metadata, and that is what is
95
 
         * saved in the mpool region's information to uniquely identify the
96
 
         * file.
97
 
         *
98
 
         * When called from the mpool layer this routine will be called each
99
 
         * time a new thread of control wants to share the file, which makes
100
 
         * things tougher.  As far as byte sexing goes, since the mpool region
101
 
         * lives on a single host, there's no issue of that -- the entire
102
 
         * region is byte sex dependent.  As far as variable sizes go, we make
103
 
         * the simplifying assumption that 32-bit and 64-bit processes will
104
 
         * get the same 32-bit values if we truncate any returned 64-bit value
105
 
         * to a 32-bit value.
106
 
         */
107
 
        tmp = (u_int32_t)fi.nFileIndexLow;
108
 
        for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
109
 
                *fidp++ = *p++;
110
 
        tmp = (u_int32_t)fi.nFileIndexHigh;
111
 
        for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
112
 
                *fidp++ = *p++;
113
 
 
114
 
        if (unique_okay) {
115
 
                /*
116
 
                 * Use the system time to try to get a unique value
117
 
                 * within this process.  A millisecond counter
118
 
                 * overflows 32 bits in about 49 days.  So we use 8
119
 
                 * bytes, and don't bother with the volume ID, which
120
 
                 * is not very useful for our purposes.
121
 
                 */
122
 
                SYSTEMTIME st;
123
 
 
124
 
                GetSystemTime(&st);
125
 
                tmp = (st.wYear - 1900) * 12 + (st.wMonth - 1);
126
 
                for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
127
 
                        *fidp++ = *p++;
128
 
                tmp = ((((st.wDay - 1) * 24 + st.wHour) * 60 +
129
 
                        st.wMinute) * 60 + st.wSecond) * 1000 +
130
 
                        st.wMilliseconds;
131
 
                for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
132
 
                        *fidp++ = *p++;
133
 
                for (p = (u_int8_t *)&fid_serial, i = sizeof(u_int32_t);
134
 
                    i > 0; --i)
135
 
                        *fidp++ = *p++;
136
 
        } else {
137
 
                tmp = (u_int32_t)fi.dwVolumeSerialNumber;
138
 
                for (p = (u_int8_t *)&tmp, i = sizeof(u_int32_t); i > 0; --i)
139
 
                        *fidp++ = *p++;
140
 
        }
141
 
 
142
 
        return (0);
143
 
}