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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
////////////////////////////////////////////////////////////////////////////
//
// Project: SMB kioslave for KDE2
//
// File: kio_smb_file.cpp
//
// Abstract: member function implementations for SMBSlave that deal with
// SMB file access
//
// Author(s): Matthew Peterson <mpeterson@caldera.com>
//
//---------------------------------------------------------------------------
//
// Copyright (c) 2000 Caldera Systems, Inc.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, please obtain
// a copy from http://www.gnu.org/copyleft/gpl.html
//
/////////////////////////////////////////////////////////////////////////////
#include "kio_smb.h"
#include "kio_smb_internal.h"
#include <QVarLengthArray>
#include <QDateTime>
#include <QMimeDatabase>
#include <QMimeType>
//===========================================================================
void SMBSlave::get( const QUrl& kurl )
{
char buf[MAX_XFER_BUF_SIZE];
int filefd = 0;
int errNum = 0;
ssize_t bytesread = 0;
// time_t curtime = 0;
// time_t lasttime = 0; // Disabled durint port to Qt5/KF5. Seems to be unused.
// time_t starttime = 0; // Disabled durint port to Qt5/KF5. Seems to be unused.
KIO::filesize_t totalbytesread = 0;
QByteArray filedata;
SMBUrl url;
qCDebug(KIO_SMB) << kurl;
// check (correct) URL
QUrl kvurl = checkURL(kurl);
// if URL is not valid we have to redirect to correct URL
if (kvurl != kurl) {
redirection(kvurl);
finished();
return;
}
if(!auth_initialize_smbc())
return;
// Stat
url = kurl;
errNum = cache_stat(url,&st);
if( errNum != 0 )
{
if ( errNum == EACCES )
error( KIO::ERR_ACCESS_DENIED, url.toDisplayString());
else
error( KIO::ERR_DOES_NOT_EXIST, url.toDisplayString());
return;
}
if ( S_ISDIR( st.st_mode ) ) {
error( KIO::ERR_IS_DIRECTORY, url.toDisplayString());
return;
}
// Set the total size
totalSize( st.st_size );
// Open and read the file
filefd = smbc_open(url.toSmbcUrl(),O_RDONLY,0);
if(filefd >= 0)
{
bool isFirstPacket = true;
// lasttime = starttime = time(NULL); // This seems to be unused..
while(1)
{
bytesread = smbc_read(filefd, buf, MAX_XFER_BUF_SIZE);
if(bytesread == 0)
{
// All done reading
break;
}
else if(bytesread < 0)
{
error( KIO::ERR_COULD_NOT_READ, url.toDisplayString());
return;
}
filedata = QByteArray::fromRawData(buf,bytesread);
if (isFirstPacket)
{
QMimeDatabase db;
QMimeType type = db.mimeTypeForFileNameAndData(url.fileName(), filedata);
mimeType(type.name());
isFirstPacket = false;
}
data( filedata );
filedata.clear();
// increment total bytes read
totalbytesread += bytesread;
processedSize(totalbytesread);
}
smbc_close(filefd);
data( QByteArray() );
processedSize(static_cast<KIO::filesize_t>(st.st_size));
}
else
{
error( KIO::ERR_CANNOT_OPEN_FOR_READING, url.toDisplayString());
return;
}
finished();
}
//===========================================================================
void SMBSlave::open( const QUrl& kurl, QIODevice::OpenMode mode)
{
int errNum = 0;
qCDebug(KIO_SMB) << kurl;
// check (correct) URL
QUrl kvurl = checkURL(kurl);
// if URL is not valid we have to redirect to correct URL
if (kvurl != kurl) {
redirection(kvurl);
finished();
return;
}
if(!auth_initialize_smbc()) {
error( KIO::ERR_ACCESS_DENIED, kurl.toDisplayString());
return;
}
// Save the URL as a private member
// FIXME For some reason m_openUrl has be be declared in bottom private
// section of the class SMBSlave declaratiom instead of the top section
// or else this assignment fails
m_openUrl = kurl;
// Stat
errNum = cache_stat(m_openUrl,&st);
if( errNum != 0 )
{
if ( errNum == EACCES )
error( KIO::ERR_ACCESS_DENIED, m_openUrl.toDisplayString());
else
error( KIO::ERR_DOES_NOT_EXIST, m_openUrl.toDisplayString());
return;
}
if ( S_ISDIR( st.st_mode ) ) {
error( KIO::ERR_IS_DIRECTORY, m_openUrl.toDisplayString());
return;
}
// Set the total size
totalSize( st.st_size );
// Convert permissions
int flags = 0;
if (mode & QIODevice::ReadOnly) {
if (mode & QIODevice::WriteOnly) {
flags = O_RDWR | O_CREAT;
} else {
flags = O_RDONLY;
}
} else if (mode & QIODevice::WriteOnly) {
flags = O_WRONLY | O_CREAT;
}
if (mode & QIODevice::Append) {
flags |= O_APPEND;
} else if (mode & QIODevice::Truncate) {
flags |= O_TRUNC;
}
// Open the file
m_openFd = smbc_open(m_openUrl.toSmbcUrl(), flags, 0);
if(m_openFd < 0)
{
error( KIO::ERR_CANNOT_OPEN_FOR_READING, m_openUrl.toDisplayString());
return;
}
// Determine the mimetype of the file to be retrieved, and emit it.
// This is mandatory in all slaves (for KRun/BrowserRun to work).
// If we're not opening the file ReadOnly or ReadWrite, don't attempt to
// read the file and send the mimetype.
if (mode & QIODevice::ReadOnly){
ssize_t bytesRequested = 1024;
ssize_t bytesRead = 0;
QVarLengthArray<char> buffer(bytesRequested);
bytesRead = smbc_read(m_openFd, buffer.data(), bytesRequested);
if(bytesRead < 0)
{
error( KIO::ERR_COULD_NOT_READ, m_openUrl.toDisplayString());
close();
return;
}
else
{
QByteArray fileData = QByteArray::fromRawData(buffer.data(),bytesRead);
QMimeDatabase db;
QMimeType type = db.mimeTypeForFileNameAndData(m_openUrl.fileName(), fileData);
mimeType(type.name());
off_t res = smbc_lseek(m_openFd, 0, SEEK_SET);
if (res == (off_t)-1) {
error(KIO::ERR_COULD_NOT_SEEK, m_openUrl.path());
close();
return;
}
}
}
position( 0 );
emit opened();
}
void SMBSlave::read( KIO::filesize_t bytesRequested )
{
Q_ASSERT(m_openFd != -1);
QVarLengthArray<char> buffer(bytesRequested);
ssize_t bytesRead = 0;
bytesRead = smbc_read(m_openFd, buffer.data(), bytesRequested);
Q_ASSERT(bytesRead <= static_cast<ssize_t>(bytesRequested));
if(bytesRead < 0)
{
qCDebug(KIO_SMB) << "Could not read " << m_openUrl;
error( KIO::ERR_COULD_NOT_READ, m_openUrl.toDisplayString());
close();
return;
}
QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead);
data( fileData );
}
void SMBSlave::write(const QByteArray &fileData)
{
Q_ASSERT(m_openFd != -1);
QByteArray buf(fileData);
ssize_t size = smbc_write(m_openFd, buf.data(), buf.size());
if (size < 0)
{
qCDebug(KIO_SMB) << "Could not write to " << m_openUrl;
error( KIO::ERR_COULD_NOT_WRITE, m_openUrl.toDisplayString());
close();
return;
}
written(size);
}
void SMBSlave::seek(KIO::filesize_t offset)
{
off_t res = smbc_lseek(m_openFd, static_cast<off_t>(offset), SEEK_SET);
if (res == (off_t)-1) {
error(KIO::ERR_COULD_NOT_SEEK, m_openUrl.path());
close();
} else {
qCDebug( KIO_SMB ) << "res" << res;
position( res );
}
}
void SMBSlave::close()
{
smbc_close(m_openFd);
finished();
}
//===========================================================================
void SMBSlave::put( const QUrl& kurl,
int permissions,
KIO::JobFlags flags )
{
void *buf;
size_t bufsize;
m_current_url = kurl;
int filefd;
bool exists;
int errNum = 0;
off_t retValLSeek = 0;
mode_t mode;
QByteArray filedata;
qCDebug(KIO_SMB) << kurl;
errNum = cache_stat(m_current_url, &st);
exists = (errNum == 0);
if ( exists && !(flags & KIO::Overwrite) && !(flags & KIO::Resume))
{
if (S_ISDIR(st.st_mode))
{
qCDebug(KIO_SMB) << kurl <<" already isdir !!";
error( KIO::ERR_DIR_ALREADY_EXIST, m_current_url.toDisplayString());
}
else
{
qCDebug(KIO_SMB) << kurl <<" already exist !!";
error( KIO::ERR_FILE_ALREADY_EXIST, m_current_url.toDisplayString());
}
return;
}
if (exists && !(flags & KIO::Resume) && (flags & KIO::Overwrite))
{
qCDebug(KIO_SMB) << "exists try to remove " << m_current_url.toSmbcUrl();
// remove(m_current_url.url().toLocal8Bit());
}
if (flags & KIO::Resume)
{
// append if resuming
qCDebug(KIO_SMB) << "resume " << m_current_url.toSmbcUrl();
filefd = smbc_open(m_current_url.toSmbcUrl(), O_RDWR, 0 );
if (filefd < 0) {
errNum = errno;
} else {
errNum = 0;
}
retValLSeek = smbc_lseek(filefd, 0, SEEK_END);
if (retValLSeek == (off_t)-1) {
errNum = errno;
} else {
errNum = 0;
}
}
else
{
if (permissions != -1)
{
mode = permissions | S_IWUSR | S_IRUSR;
}
else
{
mode = 600;//0666;
}
qCDebug(KIO_SMB) << "NO resume " << m_current_url.toSmbcUrl();
filefd = smbc_open(m_current_url.toSmbcUrl(), O_CREAT | O_TRUNC | O_WRONLY, mode);
if (filefd < 0) {
errNum = errno;
} else {
errNum = 0;
}
}
if ( filefd < 0 )
{
if ( errNum == EACCES )
{
qCDebug(KIO_SMB) << "error " << kurl <<" access denied !!";
error( KIO::ERR_WRITE_ACCESS_DENIED, m_current_url.toDisplayString());
}
else
{
qCDebug(KIO_SMB) << "error " << kurl <<" can not open for writing !!";
error( KIO::ERR_CANNOT_OPEN_FOR_WRITING, m_current_url.toDisplayString());
}
return;
}
// Loop until we got 0 (end of data)
while(1)
{
qCDebug(KIO_SMB) << "request data ";
dataReq(); // Request for data
qCDebug(KIO_SMB) << "write " << m_current_url.toSmbcUrl();
if (readData(filedata) <= 0)
{
qCDebug(KIO_SMB) << "readData <= 0";
break;
}
qCDebug(KIO_SMB) << "write " << m_current_url.toSmbcUrl();
buf = filedata.data();
bufsize = filedata.size();
ssize_t size = smbc_write(filefd, buf, bufsize);
if ( size < 0)
{
qCDebug(KIO_SMB) << "error " << kurl << "could not write !!";
error( KIO::ERR_COULD_NOT_WRITE, m_current_url.toDisplayString());
return;
}
qCDebug(KIO_SMB ) << "wrote " << size;
}
qCDebug(KIO_SMB) << "close " << m_current_url.toSmbcUrl();
if(smbc_close(filefd) < 0)
{
qCDebug(KIO_SMB) << kurl << "could not write !!";
error( KIO::ERR_COULD_NOT_WRITE, m_current_url.toDisplayString());
return;
}
// set final permissions, if the file was just created
if ( permissions != -1 && !exists )
{
// TODO: did the smbc_chmod fail?
// TODO: put in call to chmod when it is working!
// smbc_chmod(url.toSmbcUrl(),permissions);
}
#ifdef HAVE_UTIME_H
// set modification time
const QString mtimeStr = metaData( "modified" );
if ( !mtimeStr.isEmpty() ) {
QDateTime dt = QDateTime::fromString( mtimeStr, Qt::ISODate );
if ( dt.isValid() ) {
if (cache_stat( m_current_url, &st ) == 0) {
struct utimbuf utbuf;
utbuf.actime = st.st_atime; // access time, unchanged
utbuf.modtime = dt.toTime_t(); // modification time
smbc_utime( m_current_url.toSmbcUrl(), &utbuf );
}
}
}
#endif
// We have done our job => finish
finished();
}
|