summaryrefslogtreecommitdiffstats
path: root/tdeioslave/smb/tdeio_smb_dir.cpp
blob: 1e707e14e2c921a88256e6ec127b8b3aedc92e08 (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
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
/////////////////////////////////////////////////////////////////////////////
//
// Project:     SMB tdeioslave for KDE2
//
// File:        tdeio_smb_dir.cpp
//
// Abstract:    member function implementations for SMBSlave that deal with
//              SMB directory 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 "tdeio_smb.h"
#include "tdeio_smb_internal.h"


//===========================================================================
// TODO: add when libsmbclient supports it
void SMBSlave::copy( const KURL& ksrc,
                     const KURL& kdst,
                     int permissions,
                     bool overwrite)
{

    SMBUrl          src;
    SMBUrl          dst;
    mode_t          initialmode;
    int             n;
    int             dstflags;
    int             srcfd = -1;
    int             dstfd = -1;
    TDEIO::filesize_t processed_size = 0;
    unsigned char   buf[MAX_XFER_BUF_SIZE];

    kdDebug(TDEIO_SMB) << "SMBSlave::copy with src = " << ksrc << "and dest = " << kdst << endl;

    // setup urls
    src = ksrc;
    dst = kdst;

    // Obtain information about source
    if(cache_stat(src, &st ) == -1)
    {
        if ( errno == EACCES )
        {
            error( TDEIO::ERR_ACCESS_DENIED, src.prettyURL());
        }
        else
        {
             error( TDEIO::ERR_DOES_NOT_EXIST, src.prettyURL());
        }
        return;
    }
    if ( S_ISDIR( st.st_mode ) )
    {
        error( TDEIO::ERR_IS_DIRECTORY, src.prettyURL() );
        return;
    }
    totalSize(st.st_size);

    // Check to se if the destination exists
    if(cache_stat(dst, &st) != -1)
    {
        if(S_ISDIR(st.st_mode))
        {
            error( TDEIO::ERR_DIR_ALREADY_EXIST, dst.prettyURL());
	    return;
        }
        if(!overwrite)
        {
            error( TDEIO::ERR_FILE_ALREADY_EXIST, dst.prettyURL());
	    return;
	}
    }

    // Open the source file
    srcfd = smbc_open(src.toSmbcUrl(), O_RDONLY, 0);
    if(srcfd < 0)
    {
        if(errno == EACCES)
        {
            error( TDEIO::ERR_ACCESS_DENIED, src.prettyURL() );
        }
        else
        {
            error( TDEIO::ERR_DOES_NOT_EXIST, src.prettyURL() );
        }
	return;
    }

    // Determine initial creation mode
    if(permissions != -1)
    {
        initialmode = permissions | S_IWUSR;
    }
    else
    {
        initialmode = 0 | S_IWUSR;//0666;
    }


    // Open the destination file
    dstflags = O_CREAT | O_TRUNC | O_WRONLY;
    if(!overwrite)
    {
        dstflags |= O_EXCL;
    }
    dstfd = smbc_open(dst.toSmbcUrl(), dstflags, initialmode);
    if(dstfd < 0)
    {
        if(errno == EACCES)
        {
            error(TDEIO::ERR_WRITE_ACCESS_DENIED, dst.prettyURL());
        }
        else
        {
            error(TDEIO::ERR_CANNOT_OPEN_FOR_READING, dst.prettyURL());
        }
	if(srcfd >= 0 )
	{
	  smbc_close(srcfd);
	}
        return;
    }


    // Perform copy
    while(1)
    {
        n = smbc_read(srcfd, buf, MAX_XFER_BUF_SIZE );
        if(n > 0)
        {
            n = smbc_write(dstfd, buf, n);
            if(n == -1)
            {
	        kdDebug(TDEIO_SMB) << "SMBSlave::copy copy now TDEIO::ERR_COULD_NOT_WRITE" << endl;
                error( TDEIO::ERR_COULD_NOT_WRITE, dst.prettyURL());
                break;
            }

            processed_size += n;
	    processedSize(processed_size);
	}
        else if(n == 0)
	{
	      break; // finished
	}
	else
	{
            error( TDEIO::ERR_COULD_NOT_READ, src.prettyURL());
	    break;
        }
    }


    //    FINISHED:

    if(srcfd >= 0 )
    {
        smbc_close(srcfd);
    }

    if(dstfd >= 0)
    {
        if(smbc_close(dstfd) == 0)
        {

            // TODO: set final permissions
        }
        else
        {
            error( TDEIO::ERR_COULD_NOT_WRITE, dst.prettyURL());
	    return;
        }
    }

    finished();
}

//===========================================================================
void SMBSlave::del( const KURL &kurl, bool isfile)
{
    kdDebug(TDEIO_SMB) << "SMBSlave::del on " << kurl << endl;
    m_current_url = kurl;

    if(isfile)
    {
        // Delete file
        kdDebug(TDEIO_SMB) << "SMBSlave:: unlink " << kurl << endl;
        if(smbc_unlink(m_current_url.toSmbcUrl()) == -1)
        {
            switch(errno)
            {
            case EISDIR:
                error( TDEIO::ERR_IS_DIRECTORY, m_current_url.prettyURL());
                break;
            default:
                reportError(kurl);
            }
        }
    }
    else
    {
        kdDebug(TDEIO_SMB) << "SMBSlave:: rmdir " << kurl << endl;
        // Delete directory
        if(smbc_rmdir(m_current_url.toSmbcUrl()) == -1)
        {
            reportError(kurl);
        }
    }

    finished();
}

//===========================================================================
void SMBSlave::mkdir( const KURL &kurl, int permissions )
{
    kdDebug(TDEIO_SMB) << "SMBSlave::mkdir on " << kurl << endl;
    m_current_url = kurl;

    if(smbc_mkdir(m_current_url.toSmbcUrl(), 0777) != 0)
    {
        if (errno == EEXIST) {
            if(cache_stat(m_current_url, &st ) == 0)
            {
                if(S_ISDIR(st.st_mode ))
                {
                    error( TDEIO::ERR_DIR_ALREADY_EXIST, m_current_url.prettyURL());
                }
            }
            else
            {
                error( TDEIO::ERR_FILE_ALREADY_EXIST, m_current_url.prettyURL());
            }
        } else
            reportError(kurl);
	kdDebug(TDEIO_SMB) << "SMBSlave::mkdir exit with error " << kurl << endl;
    }
    else
    {
        if(permissions != -1)
        {
            // TODO enable the following when complete
            //smbc_chmod( url.toSmbcUrl(), permissions );
        }
    }

    finished();
}


//===========================================================================
void SMBSlave::rename( const KURL& ksrc, const KURL& kdest, bool overwrite )
{

    SMBUrl      src;
    SMBUrl      dst;

    kdDebug(TDEIO_SMB) << "SMBSlave::rename, old name = " << ksrc << ", new name = " << kdest << endl;

    src = ksrc;
    dst = kdest;

    // Check to se if the destination exists

    kdDebug(TDEIO_SMB) << "SMBSlave::rename stat dst" << endl;
    if(cache_stat(dst, &st) != -1)
    {
        if(S_ISDIR(st.st_mode))
        {
	    kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_DIR_ALREADY_EXIST" << endl;
            error( TDEIO::ERR_DIR_ALREADY_EXIST, dst.prettyURL());
	    finished();
	    return;
        }
        if(!overwrite)
        {
	    kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_FILE_ALREADY_EXIST" << endl;
            error( TDEIO::ERR_FILE_ALREADY_EXIST, dst.prettyURL());
	    finished();
	    return;
	}
    }
    kdDebug(TDEIO_SMB ) << "smbc_rename " << src.toSmbcUrl() << " " << dst.toSmbcUrl() << endl;
    if(smbc_rename(src.toSmbcUrl(), dst.toSmbcUrl())!=0)
    {
        kdDebug(TDEIO_SMB ) << "failed " << perror << endl;
      switch(errno)
      {
        case ENOENT:
          if(cache_stat(src, &st) == -1)
          {
              if(errno == EACCES)
	      {
	        kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_ACCESS_DENIED" << endl;
                error(TDEIO::ERR_ACCESS_DENIED, src.prettyURL());
              }
              else
              {
		kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_DOES_NOT_EXIST" << endl;
                error(TDEIO::ERR_DOES_NOT_EXIST, src.prettyURL());
              }
          }
          break;

        case EACCES:
        case EPERM:
  	  kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_ACCESS_DENIED" << endl;
          error( TDEIO::ERR_ACCESS_DENIED, dst.prettyURL() );
          break;

        default:
  	  kdDebug(TDEIO_SMB) << "SMBSlave::rename TDEIO::ERR_CANNOT_RENAME" << endl;
          error( TDEIO::ERR_CANNOT_RENAME, src.prettyURL() );

      }

      kdDebug(TDEIO_SMB) << "SMBSlave::rename exit with error" << endl;
      return;
    }

    kdDebug(TDEIO_SMB ) << "everything fine\n";
    finished();
}