~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kioslave/smb/kio_smb_mount.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the KDE project
2
 
 
3
 
    Copyright (C) 2000 Alexander Neundorf <neundorf@kde.org>
4
 
 
5
 
    This library is free software; you can redistribute it and/or
6
 
    modify it under the terms of the GNU Library General Public
7
 
    License as published by the Free Software Foundation; either
8
 
    version 2 of the License, or (at your option) any later version.
9
 
 
10
 
    This library is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
    Library General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU Library General Public License
16
 
    along with this library; see the file COPYING.LIB.  If not, write to
17
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
 
    Boston, MA 02110-1301, USA.
19
 
*/
20
 
 
21
 
#include "kio_smb.h"
22
 
#include <kstandarddirs.h>
23
 
#include <qcstring.h>
24
 
#include <unistd.h>
25
 
#include <qdir.h>
26
 
#include <kprocess.h>
27
 
 
28
 
void SMBSlave::readOutput(KProcess *, char *buffer, int buflen)
29
 
{
30
 
    mybuf += QString::fromLocal8Bit(buffer, buflen);
31
 
}
32
 
 
33
 
void SMBSlave::readStdErr(KProcess *, char *buffer, int buflen)
34
 
{
35
 
    mystderr += QString::fromLocal8Bit(buffer, buflen);
36
 
}
37
 
 
38
 
void SMBSlave::special( const QByteArray & data)
39
 
{
40
 
   kdDebug(KIO_SMB)<<"Smb::special()"<<endl;
41
 
   int tmp;
42
 
   QDataStream stream(data, IO_ReadOnly);
43
 
   stream >> tmp;
44
 
   //mounting and umounting are both blocking, "guarded" by a SIGALARM in the future
45
 
   switch (tmp)
46
 
   {
47
 
   case 1:
48
 
   case 3:
49
 
      {
50
 
         QString remotePath, mountPoint, user;
51
 
         stream >> remotePath >> mountPoint;
52
 
 
53
 
         QStringList sl=QStringList::split("/",remotePath);
54
 
         QString share,host;
55
 
         if (sl.count()>=2)
56
 
         {
57
 
            host=(*sl.at(0)).mid(2);
58
 
            share=(*sl.at(1));
59
 
            kdDebug(KIO_SMB)<<"special() host -"<< host <<"- share -" << share <<"-"<<endl;
60
 
         }
61
 
 
62
 
         remotePath.replace('\\', '/');  // smbmounterplugin sends \\host/share
63
 
 
64
 
         kdDebug(KIO_SMB) << "mounting: " << remotePath.local8Bit() << " to " << mountPoint.local8Bit() << endl;
65
 
 
66
 
         if (tmp==3) {
67
 
             if (!KStandardDirs::makeDir(mountPoint)) {
68
 
                 error(KIO::ERR_COULD_NOT_MKDIR, mountPoint);
69
 
                 return;
70
 
             }
71
 
         }
72
 
         mybuf.truncate(0);
73
 
         mystderr.truncate(0);
74
 
 
75
 
         SMBUrl smburl("smb:///");
76
 
         smburl.setHost(host);
77
 
         smburl.setPath("/" + share);
78
 
 
79
 
         if ( !checkPassword(smburl) )
80
 
         {
81
 
           finished();
82
 
           return;
83
 
         }
84
 
 
85
 
         // using smbmount instead of "mount -t smbfs", because mount does not allow a non-root
86
 
         // user to do a mount, but a suid smbmnt does allow this
87
 
 
88
 
         KProcess proc;
89
 
         proc.setUseShell(true);  // to have the path to smbmnt (which is used by smbmount); see man smbmount
90
 
         proc << "smbmount";
91
 
 
92
 
         QString options;
93
 
 
94
 
         if ( smburl.user().isEmpty() )
95
 
         {
96
 
           user = "guest";
97
 
           options = "-o guest";
98
 
         }
99
 
         else
100
 
         {
101
 
           options = "-o username=" + KProcess::quote(smburl.user());
102
 
           user = smburl.user();
103
 
 
104
 
           if ( ! smburl.pass().isEmpty() )
105
 
             options += ",password=" + KProcess::quote(smburl.pass());
106
 
         }
107
 
 
108
 
         // TODO: check why the control center uses encodings with a blank char, e.g. "cp 1250"
109
 
         //if ( ! m_default_encoding.isEmpty() )
110
 
           //options += ",codepage=" + KProcess::quote(m_default_encoding);
111
 
 
112
 
         proc << KProcess::quote(remotePath.local8Bit());
113
 
         proc << KProcess::quote(mountPoint.local8Bit());
114
 
         proc << options;
115
 
 
116
 
         connect(&proc, SIGNAL( receivedStdout(KProcess *, char *, int )),
117
 
                 SLOT(readOutput(KProcess *, char *, int)));
118
 
 
119
 
         connect(&proc, SIGNAL( receivedStderr(KProcess *, char *, int )),
120
 
                 SLOT(readStdErr(KProcess *, char *, int)));
121
 
 
122
 
         if (!proc.start( KProcess::Block, KProcess::AllOutput ))
123
 
         {
124
 
            error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
125
 
                  "smbmount"+i18n("\nMake sure that the samba package is installed properly on your system."));
126
 
            return;
127
 
         }
128
 
 
129
 
         kdDebug(KIO_SMB) << "mount exit " << proc.exitStatus() << endl
130
 
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;
131
 
 
132
 
         if (proc.exitStatus() != 0)
133
 
         {
134
 
           error( KIO::ERR_COULD_NOT_MOUNT,
135
 
               i18n("Mounting of share \"%1\" from host \"%2\" by user \"%3\" failed.\n%4")
136
 
               .arg(share).arg(host).arg(user).arg(mybuf + "\n" + mystderr));
137
 
           return;
138
 
         }
139
 
 
140
 
         finished();
141
 
      }
142
 
      break;
143
 
   case 2:
144
 
   case 4:
145
 
      {
146
 
         QString mountPoint;
147
 
         stream >> mountPoint;
148
 
 
149
 
         KProcess proc;
150
 
         proc.setUseShell(true);
151
 
         proc << "smbumount";
152
 
         proc << KProcess::quote(mountPoint);
153
 
 
154
 
         mybuf.truncate(0);
155
 
         mystderr.truncate(0);
156
 
 
157
 
         connect(&proc, SIGNAL( receivedStdout(KProcess *, char *, int )),
158
 
                 SLOT(readOutput(KProcess *, char *, int)));
159
 
 
160
 
         connect(&proc, SIGNAL( receivedStderr(KProcess *, char *, int )),
161
 
                 SLOT(readStdErr(KProcess *, char *, int)));
162
 
 
163
 
         if ( !proc.start( KProcess::Block, KProcess::AllOutput ) )
164
 
         {
165
 
           error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
166
 
                 "smbumount"+i18n("\nMake sure that the samba package is installed properly on your system."));
167
 
           return;
168
 
         }
169
 
 
170
 
         kdDebug(KIO_SMB) << "smbumount exit " << proc.exitStatus() << endl
171
 
                          << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;
172
 
 
173
 
         if (proc.exitStatus() != 0)
174
 
         {
175
 
           error(KIO::ERR_COULD_NOT_UNMOUNT,
176
 
               i18n("Unmounting of mountpoint \"%1\" failed.\n%2")
177
 
               .arg(mountPoint).arg(mybuf + "\n" + mystderr));
178
 
           return;
179
 
         }
180
 
 
181
 
         if ( tmp == 4 )
182
 
         {
183
 
           bool ok;
184
 
 
185
 
           QDir dir(mountPoint);
186
 
           dir.cdUp();
187
 
           ok = dir.rmdir(mountPoint);
188
 
           if ( ok )
189
 
           {
190
 
             QString p=dir.path();
191
 
             dir.cdUp();
192
 
             ok = dir.rmdir(p);
193
 
           }
194
 
 
195
 
           if ( !ok )
196
 
           {
197
 
             error(KIO::ERR_COULD_NOT_RMDIR, mountPoint);
198
 
             return;
199
 
           }
200
 
         }
201
 
 
202
 
         finished();
203
 
      }
204
 
      break;
205
 
   default:
206
 
      break;
207
 
   }
208
 
   finished();
209
 
}
210
 
 
211
 
#include "kio_smb.moc"