add more spacing
[personal-kdebase.git] / runtime / kioslave / smb / kio_smb_dir.cpp
blob6b67412bfbb12a50968dec6ac7405600461c2576
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // Project: SMB kioslave for KDE2
4 //
5 // File: kio_smb_dir.cpp
6 //
7 // Abstract: member function implementations for SMBSlave that deal with
8 // SMB directory access
9 //
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 /////////////////////////////////////////////////////////////////////////////
32 #include "kio_smb.h"
33 #include "kio_smb_internal.h"
36 //===========================================================================
37 // TODO: add when libsmbclient supports it
38 void SMBSlave::copy( const KUrl& ksrc,
39 const KUrl& kdst,
40 int permissions,
41 KIO::JobFlags flags )
44 SMBUrl src;
45 SMBUrl dst;
46 mode_t initialmode;
47 ssize_t n;
48 int dstflags;
49 int srcfd = -1;
50 int dstfd = -1;
51 int errNum = 0;
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;
57 // setup urls
58 src = ksrc;
59 dst = kdst;
61 // Obtain information about source
62 errNum = cache_stat(src, &st );
63 if( errNum != 0 )
65 if ( errNum == EACCES )
67 error( KIO::ERR_ACCESS_DENIED, src.prettyUrl());
69 else
71 error( KIO::ERR_DOES_NOT_EXIST, src.prettyUrl());
73 return;
75 if ( S_ISDIR( st.st_mode ) )
77 error( KIO::ERR_IS_DIRECTORY, src.prettyUrl() );
78 return;
80 totalSize(st.st_size);
82 // Check to se if the destination exists
83 errNum = cache_stat(dst, &st);
84 if( errNum == 0 )
86 if(S_ISDIR(st.st_mode))
88 error( KIO::ERR_DIR_ALREADY_EXIST, dst.prettyUrl());
89 return;
91 if(!(flags & KIO::Overwrite))
93 error( KIO::ERR_FILE_ALREADY_EXIST, dst.prettyUrl());
94 return;
98 // Open the source file
99 srcfd = smbc_open(src.toSmbcUrl(), O_RDONLY, 0);
100 if (srcfd < 0){
101 errNum = errno;
102 } else {
103 errNum = 0;
106 if(srcfd < 0)
108 if(errNum == EACCES)
110 error( KIO::ERR_ACCESS_DENIED, src.prettyUrl() );
112 else
114 error( KIO::ERR_DOES_NOT_EXIST, src.prettyUrl() );
116 return;
119 // Determine initial creation mode
120 if(permissions != -1)
122 initialmode = permissions | S_IWUSR;
124 else
126 initialmode = 0 | S_IWUSR;//0666;
130 // Open the destination file
131 dstflags = O_CREAT | O_TRUNC | O_WRONLY;
132 if(!(flags & KIO::Overwrite))
134 dstflags |= O_EXCL;
136 dstfd = smbc_open(dst.toSmbcUrl(), dstflags, initialmode);
137 if (dstfd < 0){
138 errNum = errno;
139 } else {
140 errNum = 0;
143 if(dstfd < 0)
145 if(errNum == EACCES)
147 error(KIO::ERR_WRITE_ACCESS_DENIED, dst.prettyUrl());
149 else
151 error(KIO::ERR_CANNOT_OPEN_FOR_READING, dst.prettyUrl());
154 if(srcfd >= 0 )
156 smbc_close(srcfd);
158 return;
162 // Perform copy
163 while(1)
165 n = smbc_read(srcfd, buf, MAX_XFER_BUF_SIZE );
166 if(n > 0)
168 n = smbc_write(dstfd, buf, n);
169 if(n == -1)
171 kDebug(KIO_SMB) << "SMBSlave::copy copy now KIO::ERR_COULD_NOT_WRITE";
172 error( KIO::ERR_COULD_NOT_WRITE, dst.prettyUrl());
173 break;
176 processed_size += n;
177 processedSize(processed_size);
179 else if(n == 0)
181 break; // finished
183 else
185 error( KIO::ERR_COULD_NOT_READ, src.prettyUrl());
186 break;
191 // FINISHED:
193 if(srcfd >= 0 )
195 smbc_close(srcfd);
198 if(dstfd >= 0)
200 if(smbc_close(dstfd) == 0)
203 // TODO: set final permissions
205 else
207 error( KIO::ERR_COULD_NOT_WRITE, dst.prettyUrl());
208 return;
212 finished();
215 //===========================================================================
216 void SMBSlave::del( const KUrl &kurl, bool isfile)
218 kDebug(KIO_SMB) << "SMBSlave::del on " << kurl;
219 m_current_url = kurl;
220 int errNum = 0;
221 int retVal = 0;
223 if(isfile)
225 // Delete file
226 kDebug(KIO_SMB) << "SMBSlave:: unlink " << kurl;
227 retVal = smbc_unlink(m_current_url.toSmbcUrl());
228 if ( retVal < 0 ){
229 errNum = errno;
230 } else {
231 errNum = 0;
234 if( retVal < 0 )
236 reportError(kurl, errNum);
239 else
241 kDebug(KIO_SMB) << "SMBSlave:: rmdir " << kurl;
242 // Delete directory
243 retVal = smbc_rmdir(m_current_url.toSmbcUrl());
244 if( retVal < 0 ) {
245 errNum = errno;
246 } else {
247 errNum = 0;
250 if( retVal < 0 )
252 reportError(kurl, errNum);
256 finished();
259 //===========================================================================
260 void SMBSlave::mkdir( const KUrl &kurl, int permissions )
262 kDebug(KIO_SMB) << "SMBSlave::mkdir on " << kurl;
263 int errNum = 0;
264 int retVal = 0;
265 m_current_url = kurl;
267 retVal = smbc_mkdir(m_current_url.toSmbcUrl(), 0777);
268 if( retVal < 0 ){
269 errNum = errno;
270 } else {
271 errNum = 0;
274 if( retVal < 0 )
276 if (errNum == EEXIST) {
277 errNum = cache_stat(m_current_url, &st );
278 if( errNum == 0 )
280 if(S_ISDIR(st.st_mode ))
282 error( KIO::ERR_DIR_ALREADY_EXIST, m_current_url.prettyUrl());
285 else
287 error( KIO::ERR_FILE_ALREADY_EXIST, m_current_url.prettyUrl());
289 } else
290 reportError(kurl, errNum);
291 kDebug(KIO_SMB) << "SMBSlave::mkdir exit with error " << kurl;
293 else
295 if(permissions != -1)
297 // TODO enable the following when complete
298 //smbc_chmod( url.toSmbcUrl(), permissions );
302 finished();
306 //===========================================================================
307 void SMBSlave::rename( const KUrl& ksrc, const KUrl& kdest, KIO::JobFlags flags )
310 SMBUrl src;
311 SMBUrl dst;
312 int errNum = 0;
313 int retVal = 0;
315 kDebug(KIO_SMB) << "SMBSlave::rename, old name = " << ksrc << ", new name = " << kdest;
317 src = ksrc;
318 dst = kdest;
320 // Check to se if the destination exists
322 kDebug(KIO_SMB) << "SMBSlave::rename stat dst";
323 errNum = cache_stat(dst, &st);
324 if( errNum == 0 )
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());
330 finished();
331 return;
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());
337 finished();
338 return;
341 kDebug(KIO_SMB ) << "smbc_rename " << src.toSmbcUrl() << " " << dst.toSmbcUrl();
342 retVal = smbc_rename(src.toSmbcUrl(), dst.toSmbcUrl());
343 if( retVal < 0 ){
344 errNum = errno;
345 } else {
346 errNum = 0;
349 if( retVal < 0 )
351 kDebug(KIO_SMB ) << "failed ";
352 switch(errNum)
354 case ENOENT:
355 errNum = cache_stat(src, &st);
356 if( errNum != 0 )
358 if(errNum == EACCES)
360 kDebug(KIO_SMB) << "SMBSlave::rename KIO::ERR_ACCESS_DENIED";
361 error(KIO::ERR_ACCESS_DENIED, src.prettyUrl());
363 else
365 kDebug(KIO_SMB) << "SMBSlave::rename KIO::ERR_DOES_NOT_EXIST";
366 error(KIO::ERR_DOES_NOT_EXIST, src.prettyUrl());
369 break;
371 case EACCES:
372 case EPERM:
373 kDebug(KIO_SMB) << "SMBSlave::rename KIO::ERR_ACCESS_DENIED";
374 error( KIO::ERR_ACCESS_DENIED, dst.prettyUrl() );
375 break;
377 default:
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";
384 return;
387 kDebug(KIO_SMB ) << "everything fine\n";
388 finished();