summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier/AvancedQFile.cpp
blob: 3d867fb0ba773eae00700961c2809e622a7427ab (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** \file AvancedQFile.cpp
\brief Define the QFile herited class to set file date/time
\author alpha_one_x86 */

#include "AvancedQFile.h"

#ifdef Q_CC_GNU
//this next header is needed to change file time/date under gcc
#include <utime.h>
#include <errno.h>
#endif

//source
//hSrc=CreateFile(pData->pfiSrcFile->GetFullFilePath(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | (bNoBuffer ? FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH : 0), NULL);
//destination
//hDst=CreateFile(pData->strDstFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | (bNoBuffer ? FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH : 0), NULL);

bool AvancedQFile::setCreated(const QDateTime &time)
{
    time_t ctime=time.toTime_t();
    #ifdef Q_CC_GNU
        //creation time not exists into unix world
        Q_UNUSED(ctime)
        return true;
    #else
        setErrorString(tr("Not supported on this platform"));
        return false;
    #endif
}

bool AvancedQFile::setLastModified(const QDateTime &time)
{
    time_t actime=QFileInfo(*this).lastRead().toTime_t();
    //protect to wrong actime
    if(actime<0)
        actime=0;
    time_t modtime=time.toTime_t();
    if(modtime<0)
    {
        setErrorString(tr("Last modified date is wrong"));
        return false;
    }
    #ifdef Q_CC_GNU
        //this function avalaible on unix and mingw
        utimbuf butime;
        butime.actime=actime;
        butime.modtime=modtime;
        int returnVal=utime(this->fileName().toLocal8Bit().data(),&butime);
        if(returnVal==0)
            return true;
        else
        {
            setErrorString(strerror(errno));
            return false;
        }
    #else
        setErrorString(tr("Not supported on this platform"));
        return false;
    #endif
}

bool AvancedQFile::setLastRead(const QDateTime &time)
{
    time_t modtime=QFileInfo(*this).lastModified().toTime_t();
    //protect to wrong actime
    if(modtime<0)
        modtime=0;
    time_t actime=time.toTime_t();
    if(actime<0)
    {
        setErrorString(tr("Last access date is wrong"));
        return false;
    }
    #ifdef Q_CC_GNU
        //this function avalaible on unix and mingw
        utimbuf butime;
        butime.actime=actime;
        butime.modtime=modtime;
        int returnVal=utime(this->fileName().toLocal8Bit().data(),&butime);
        if(returnVal==0)
            return true;
        else
        {
            setErrorString(strerror(errno));
            return false;
        }
    #else
        setErrorString(tr("Not supported on this platform"));
        return false;
    #endif
}

#ifdef ULTRACOPIER_OVERLAPPED_FILE
AvancedQFile::avancedQFile()
{
    handle=INVALID_HANDLE_VALUE;
    fileError=QFileDevice::NoError;
    fileErrorString.clear();
}

AvancedQFile::~avancedQFile()
{
    close();
}

QString AvancedQFile::getLastWindowsError()
{
    WCHAR ErrorStringW[65535];
    DWORD dw = GetLastError();

    int size=FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    ErrorStringW,
    0, NULL );
    if(size<0)
        tr("Unknown error: %1").arg(dw);
    return QString::fromWCharArray(ErrorStringW,size);
}

bool AvancedQFile::open(OpenMode mode)
{
    fileError=QFileDevice::NoError;
    fileErrorString.clear();
    WCHAR fileNameW[fileName().size()+1];
    if(QDir::toNativeSeparators("\\\\?\\"+fileName()).toWCharArray(fileNameW)!=fileName().size())
    {
        fileError=QFileDevice::OpenError;
        fileErrorString=tr("Path conversion error");
        return false;
    }
    fileNameW[fileName().size()]='\0';

    DWORD dwDesiredAccess=0;
    if(mode & QIODevice::ReadOnly)
        dwDesiredAccess|=GENERIC_READ;
    if(mode & QIODevice::WriteOnly)
        dwDesiredAccess|=GENERIC_Write;

    DWORD dwCreationDisposition;
    if(mode & QIODevice::WriteOnly)
        dwCreationDisposition=CREATE_ALWAYS;
    else
        dwCreationDisposition=OPEN_EXISTING;

    handle=CreateFile(
      fileNameW,
      dwDesiredAccess,
      0,
      0,
      dwCreationDisposition,
      FILE_FLAG_WRITE_THROUGH | FILE_FLAG_SEQUENTIAL_SCAN,
      0
    );
    if(handle==INVALID_HANDLE_VALUE)
    {
        fileError=QFileDevice::OpenError;
        fileErrorString=getLastWindowsError();
    }
    return (handle!=INVALID_HANDLE_VALUE);
}

void AvancedQFile::close()
{
    if(handle==INVALID_HANDLE_VALUE)
        return;
    CloseHandle(handle);
}

bool AvancedQFile::seek(qint64 pos)
{
    toto
}

bool AvancedQFile::resize(qint64 size)
{
    toto
}

QString AvancedQFile::errorString() const
{
    if(fileErrorString.isEmpty())
        return tr("Unknown error");
    return fileErrorString;
}

bool AvancedQFile::isOpen() const
{
    return (handle!=INVALID_HANDLE_VALUE);
}

qint64 AvancedQFile::write(const QByteArray &data)
{
}

QByteArray AvancedQFile::read(qint64 maxlen)
{
}

QFileDevice::FileError AvancedQFile::error() const
{
    return fileError;
}
#endif