summaryrefslogtreecommitdiff
path: root/shared/fileID.h
blob: 7944b368fccbee50a834461d3ccfa66e34b0faa0 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef FILEID_H_INCLUDED
#define FILEID_H_INCLUDED

#include <wx/stream.h>
#include "zstring.h"

#ifdef FFS_WIN
#elif defined FFS_LINUX
#include <sys/stat.h>
#endif


//unique file identifier
namespace Utility
{
class FileID
{
public:
    //standard copy constructor and assignment operator are okay!

    FileID(wxInputStream& stream);  //read
    void toStream(wxOutputStream& stream) const; //write

    bool isNull() const;
    bool operator==(const FileID& rhs) const;
    bool operator!=(const FileID& rhs) const;
    bool operator<(const FileID& rhs) const;

    FileID();
#ifdef FFS_WIN
    typedef unsigned long DWORD_FFS; //we don't want do include "windows.h" or "<wx/msw/wrapwin.h>" here, do we?

    FileID(DWORD_FFS dwVolumeSN,
           DWORD_FFS fileIndexHi,
           DWORD_FFS fileIndexLo);
#elif defined FFS_LINUX
    FileID(dev_t devId,
           ino_t inId);
#endif
private:
#ifdef FFS_WIN
    DWORD_FFS dwVolumeSerialNumber;
    DWORD_FFS nFileIndexHigh;
    DWORD_FFS nFileIndexLow;
#elif defined FFS_LINUX
    dev_t deviceId;
    ino_t inodeId;
#endif
};

//get unique file id (symbolic link handling: opens the link!!!)
//error condition: returns FileID ()
FileID retrieveFileID(const Zstring& filename);

//test whether two distinct paths point to the same file or directory:
//      true: paths point to same files/dirs
//      false: error occured OR point to different files/dirs
bool sameFileSpecified(const Zstring& file1, const Zstring& file2);
}



















//---------------Inline Implementation---------------------------------------------------
#ifdef FFS_WIN
inline
Utility::FileID::FileID() :
    dwVolumeSerialNumber(0),
    nFileIndexHigh(0),
    nFileIndexLow(0) {}

inline
Utility::FileID::FileID(DWORD_FFS dwVolumeSN,
                        DWORD_FFS fileIndexHi,
                        DWORD_FFS fileIndexLo) :
    dwVolumeSerialNumber(dwVolumeSN),
    nFileIndexHigh(fileIndexHi),
    nFileIndexLow(fileIndexLo) {}

inline
bool Utility::FileID::isNull() const
{
    return dwVolumeSerialNumber == 0 &&
           nFileIndexHigh       == 0 &&
           nFileIndexLow        == 0;
}

inline
bool Utility::FileID::operator==(const FileID& rhs) const
{
    return dwVolumeSerialNumber == rhs.dwVolumeSerialNumber &&
           nFileIndexHigh       == rhs.nFileIndexHigh       &&
           nFileIndexLow        == rhs.nFileIndexLow;
}

inline
bool Utility::FileID::operator<(const FileID& rhs) const
{
    if (dwVolumeSerialNumber != rhs.dwVolumeSerialNumber)
        return dwVolumeSerialNumber < rhs.dwVolumeSerialNumber;

    if (nFileIndexHigh != rhs.nFileIndexHigh)
        return nFileIndexHigh < rhs.nFileIndexHigh;

    return nFileIndexLow < rhs.nFileIndexLow;
}

inline
Utility::FileID::FileID(wxInputStream& stream)  //read
{
    stream.Read(&dwVolumeSerialNumber, sizeof(dwVolumeSerialNumber));
    stream.Read(&nFileIndexHigh,       sizeof(nFileIndexHigh));
    stream.Read(&nFileIndexLow,        sizeof(nFileIndexLow));
}

inline
void Utility::FileID::toStream(wxOutputStream& stream) const //write
{
    stream.Write(&dwVolumeSerialNumber, sizeof(dwVolumeSerialNumber));
    stream.Write(&nFileIndexHigh,       sizeof(nFileIndexHigh));
    stream.Write(&nFileIndexLow,        sizeof(nFileIndexLow));
}

#elif defined FFS_LINUX
inline
Utility::FileID::FileID() :
    deviceId(0),
    inodeId(0) {}

inline
Utility::FileID::FileID(dev_t devId,
                        ino_t inId) :
    deviceId(devId),
    inodeId(inId) {}

inline
bool Utility::FileID::isNull() const
{
    return deviceId == 0 &&
           inodeId  == 0;
}

inline
bool Utility::FileID::operator==(const FileID& rhs) const
{
    return deviceId == rhs.deviceId &&
           inodeId  == rhs.inodeId;
}

inline
bool Utility::FileID::operator<(const FileID& rhs) const
{
    if (deviceId != rhs.deviceId)
        return deviceId < rhs.deviceId;

    return inodeId < rhs.inodeId;
}

inline
Utility::FileID::FileID(wxInputStream& stream)  //read
{
    stream.Read(&deviceId, sizeof(deviceId));
    stream.Read(&inodeId,  sizeof(inodeId));
}

inline
void Utility::FileID::toStream(wxOutputStream& stream) const //write
{
    stream.Write(&deviceId, sizeof(deviceId));
    stream.Write(&inodeId,  sizeof(inodeId));
}
#endif
inline
bool Utility::FileID::operator!=(const FileID& rhs) const
{
    return !(*this == rhs);
}

#endif // FILEID_H_INCLUDED
bgstack15