1 /////////////////////////////////////////////////////////////////////////////
3 // Project: SMB kioslave for KDE2
5 // File: kio_smb_dir.cpp
7 // Abstract: member function implementations for SMBSlave that deal with
8 // SMB directory access
10 // Author(s): Matthew Peterson <mpeterson@caldera.com>
12 ////---------------------------------------------------------------------------
14 // Copyright (c) 2000 Caldera Systems, Inc.
16 // This program is free software; you can redistribute it and/or modify it
17 // under the terms of the GNU General Public License as published by the
18 // Free Software Foundation; either version 2.1 of the License, or
19 // (at your option) any later version.
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU Lesser General Public License for more details.
26 // You should have received a copy of the GNU General Public License
27 // along with this program; see the file COPYING. If not, please obtain
28 // a copy from http://www.gnu.org/copyleft/gpl.html
30 /////////////////////////////////////////////////////////////////////////////
33 #include "kio_smb_internal.h"
36 //===========================================================================
37 // TODO: add when libsmbclient supports it
38 void SMBSlave::copy( const KUrl
& ksrc
,
52 KIO::filesize_t processed_size
= 0;
53 unsigned char buf
[MAX_XFER_BUF_SIZE
];
55 kDebug(KIO_SMB
) << "SMBSlave::copy with src = " << ksrc
<< "and dest = " << kdst
;
61 // Obtain information about source
62 errNum
= cache_stat(src
, &st
);
65 if ( errNum
== EACCES
)
67 error( KIO::ERR_ACCESS_DENIED
, src
.prettyUrl());
71 error( KIO::ERR_DOES_NOT_EXIST
, src
.prettyUrl());
75 if ( S_ISDIR( st
.st_mode
) )
77 error( KIO::ERR_IS_DIRECTORY
, src
.prettyUrl() );
80 totalSize(st
.st_size
);
82 // Check to se if the destination exists
83 errNum
= cache_stat(dst
, &st
);
86 if(S_ISDIR(st
.st_mode
))
88 error( KIO::ERR_DIR_ALREADY_EXIST
, dst
.prettyUrl());
91 if(!(flags
& KIO::Overwrite
))
93 error( KIO::ERR_FILE_ALREADY_EXIST
, dst
.prettyUrl());
98 // Open the source file
99 srcfd
= smbc_open(src
.toSmbcUrl(), O_RDONLY
, 0);
110 error( KIO::ERR_ACCESS_DENIED
, src
.prettyUrl() );
114 error( KIO::ERR_DOES_NOT_EXIST
, src
.prettyUrl() );
119 // Determine initial creation mode
120 if(permissions
!= -1)
122 initialmode
= permissions
| S_IWUSR
;
126 initialmode
= 0 | S_IWUSR
;//0666;
130 // Open the destination file
131 dstflags
= O_CREAT
| O_TRUNC
| O_WRONLY
;
132 if(!(flags
& KIO::Overwrite
))
136 dstfd
= smbc_open(dst
.toSmbcUrl(), dstflags
, initialmode
);
147 error(KIO::ERR_WRITE_ACCESS_DENIED
, dst
.prettyUrl());
151 error(KIO::ERR_CANNOT_OPEN_FOR_READING
, dst
.prettyUrl());
165 n
= smbc_read(srcfd
, buf
, MAX_XFER_BUF_SIZE
);
168 n
= smbc_write(dstfd
, buf
, n
);
171 kDebug(KIO_SMB
) << "SMBSlave::copy copy now KIO::ERR_COULD_NOT_WRITE";
172 error( KIO::ERR_COULD_NOT_WRITE
, dst
.prettyUrl());
177 processedSize(processed_size
);
185 error( KIO::ERR_COULD_NOT_READ
, src
.prettyUrl());
200 if(smbc_close(dstfd
) == 0)
203 // TODO: set final permissions
207 error( KIO::ERR_COULD_NOT_WRITE
, dst
.prettyUrl());
215 //===========================================================================
216 void SMBSlave::del( const KUrl
&kurl
, bool isfile
)
218 kDebug(KIO_SMB
) << "SMBSlave::del on " << kurl
;
219 m_current_url
= kurl
;
226 kDebug(KIO_SMB
) << "SMBSlave:: unlink " << kurl
;
227 retVal
= smbc_unlink(m_current_url
.toSmbcUrl());
236 reportError(kurl
, errNum
);
241 kDebug(KIO_SMB
) << "SMBSlave:: rmdir " << kurl
;
243 retVal
= smbc_rmdir(m_current_url
.toSmbcUrl());
252 reportError(kurl
, errNum
);
259 //===========================================================================
260 void SMBSlave::mkdir( const KUrl
&kurl
, int permissions
)
262 kDebug(KIO_SMB
) << "SMBSlave::mkdir on " << kurl
;
265 m_current_url
= kurl
;
267 retVal
= smbc_mkdir(m_current_url
.toSmbcUrl(), 0777);
276 if (errNum
== EEXIST
) {
277 errNum
= cache_stat(m_current_url
, &st
);
280 if(S_ISDIR(st
.st_mode
))
282 error( KIO::ERR_DIR_ALREADY_EXIST
, m_current_url
.prettyUrl());
287 error( KIO::ERR_FILE_ALREADY_EXIST
, m_current_url
.prettyUrl());
290 reportError(kurl
, errNum
);
291 kDebug(KIO_SMB
) << "SMBSlave::mkdir exit with error " << kurl
;
295 if(permissions
!= -1)
297 // TODO enable the following when complete
298 //smbc_chmod( url.toSmbcUrl(), permissions );
306 //===========================================================================
307 void SMBSlave::rename( const KUrl
& ksrc
, const KUrl
& kdest
, KIO::JobFlags flags
)
315 kDebug(KIO_SMB
) << "SMBSlave::rename, old name = " << ksrc
<< ", new name = " << kdest
;
320 // Check to se if the destination exists
322 kDebug(KIO_SMB
) << "SMBSlave::rename stat dst";
323 errNum
= cache_stat(dst
, &st
);
326 if(S_ISDIR(st
.st_mode
))
328 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_DIR_ALREADY_EXIST";
329 error( KIO::ERR_DIR_ALREADY_EXIST
, dst
.prettyUrl());
333 if(!(flags
& KIO::Overwrite
))
335 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_FILE_ALREADY_EXIST";
336 error( KIO::ERR_FILE_ALREADY_EXIST
, dst
.prettyUrl());
341 kDebug(KIO_SMB
) << "smbc_rename " << src
.toSmbcUrl() << " " << dst
.toSmbcUrl();
342 retVal
= smbc_rename(src
.toSmbcUrl(), dst
.toSmbcUrl());
351 kDebug(KIO_SMB
) << "failed ";
355 errNum
= cache_stat(src
, &st
);
360 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_ACCESS_DENIED";
361 error(KIO::ERR_ACCESS_DENIED
, src
.prettyUrl());
365 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_DOES_NOT_EXIST";
366 error(KIO::ERR_DOES_NOT_EXIST
, src
.prettyUrl());
373 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_ACCESS_DENIED";
374 error( KIO::ERR_ACCESS_DENIED
, dst
.prettyUrl() );
378 kDebug(KIO_SMB
) << "SMBSlave::rename KIO::ERR_CANNOT_RENAME";
379 error( KIO::ERR_CANNOT_RENAME
, src
.prettyUrl() );
383 kDebug(KIO_SMB
) << "SMBSlave::rename exit with error";
387 kDebug(KIO_SMB
) << "everything fine\n";