~ubuntu-branches/ubuntu/quantal/smb4k/quantal

« back to all changes in this revision

Viewing changes to helpers/smb4kmounthelper.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-05-19 18:54:34 UTC
  • mfrom: (1.1.20)
  • Revision ID: package-import@ubuntu.com-20120519185434-duffny2n87214n1n
Tags: 1.0.1-1
* New upstream release.
* Update debian/compat: bump to 9.
* Update debian/control:
  - bump debhelper to 9.
  - bump kdelibs5-dev build dependency to 4:4.4.0.
  - bump Standards-Version to 3.9.3 (no changes needed).
  - Replace smbfs dependency by cifs-utils. (Closes: #638162)
* Update debian/copyright:
  - update upstream URL.
  - update upstream e-mail.
* Update debian/smb4k.lintian-overrides file.
* Update debian/watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    smb4kmounthelper  -  The helper that mounts and unmounts shares.
 
3
                             -------------------
 
4
    begin                : Sa Okt 16 2010
 
5
    copyright            : (C) 2010 by Alexander Reinholdt
 
6
    email                : alexander.reinholdt@kdemail.net
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *   This program is free software; you can redistribute it and/or modify  *
 
11
 *   it under the terms of the GNU General Public License as published by  *
 
12
 *   the Free Software Foundation; either version 2 of the License, or     *
 
13
 *   (at your option) any later version.                                   *
 
14
 *                                                                         *
 
15
 *   This program is distributed in the hope that it will be useful, but   *
 
16
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 
17
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 
18
 *   General Public License for more details.                              *
 
19
 *                                                                         *
 
20
 *   You should have received a copy of the GNU General Public License     *
 
21
 *   along with this program; if not, write to the                         *
 
22
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,   *
 
23
 *   MA  02111-1307 USA                                                    *
 
24
 ***************************************************************************/
 
25
 
 
26
// Qt includes
 
27
#include <QProcessEnvironment>
 
28
#include <QUrl>
 
29
#include <QDebug>
 
30
 
 
31
// KDE includes
 
32
#include <kglobal.h>
 
33
#include <kstandarddirs.h>
 
34
#include <klocale.h>
 
35
#include <kprocess.h>
 
36
#include <kmountpoint.h>
 
37
 
 
38
// application specific includes
 
39
#include "smb4kmounthelper.h"
 
40
 
 
41
KDE4_AUTH_HELPER_MAIN( "de.berlios.smb4k.mounthelper", Smb4KMountHelper )
 
42
 
 
43
 
 
44
ActionReply Smb4KMountHelper::mount( const QVariantMap &args )
 
45
{
 
46
  ActionReply reply;
 
47
  reply.addData( "share_url", args["share_url"] );
 
48
  reply.addData( "share_workgroup", args["share_workgroup"] );
 
49
  reply.addData( "share_comment", args["share_comment"] );
 
50
  reply.addData( "share_ip", args["share_ip"] );
 
51
  reply.addData( "share_mountpoint", args["share_mountpoint"] );
 
52
  reply.addData( "key", args["key"] );
 
53
 
 
54
  KProcess proc( this );
 
55
  proc.setOutputChannelMode( KProcess::SeparateChannels );
 
56
  proc.setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
 
57
  proc.setEnv( "PASSWD", args["share_url"].toUrl().password(), true );
 
58
  // The $HOME environment variable is needed under FreeBSD to
 
59
  // point the mount process to the right ~/.nsmbrc file. Under
 
60
  // Linux it is not needed.
 
61
  proc.setEnv( "HOME", args["home_dir"].toString() );
 
62
  proc.setProgram( args["mount_command"].toStringList() );
 
63
 
 
64
  // Run the mount process.
 
65
  proc.start();
 
66
 
 
67
  if ( proc.waitForStarted( -1 ) )
 
68
  {
 
69
    // We want to be able to terminate the process from outside.
 
70
    // Thus, we implement a loop that checks periodically, if we
 
71
    // need to kill the process.
 
72
    bool user_kill = false;
 
73
 
 
74
    while ( !proc.waitForFinished( 10 ) )
 
75
    {
 
76
      if ( HelperSupport::isStopped() )
 
77
      {
 
78
        proc.kill();
 
79
        user_kill = true;
 
80
        break;
 
81
      }
 
82
      else
 
83
      {
 
84
        // Do nothing
 
85
      }
 
86
    }
 
87
 
 
88
    if ( proc.exitStatus() == KProcess::CrashExit )
 
89
    {
 
90
      if ( !user_kill )
 
91
      {
 
92
        reply.setErrorCode( ActionReply::HelperError );
 
93
        reply.setErrorDescription( i18n( "The mount process crashed." ) );
 
94
        return reply;
 
95
      }
 
96
      else
 
97
      {
 
98
        // Do nothing
 
99
      }
 
100
    }
 
101
    else
 
102
    {
 
103
      // Check if there is output on stderr.
 
104
      QString stderr = QString::fromUtf8( proc.readAllStandardError() );
 
105
      reply.addData( "stderr", stderr );
 
106
 
 
107
      // Check if there is output on stdout.
 
108
      QString stdout = QString::fromUtf8( proc.readAllStandardOutput() );
 
109
      reply.addData( "stdout", stdout );
 
110
    }
 
111
  }
 
112
  else
 
113
  {
 
114
    reply.setErrorCode( ActionReply::HelperError );
 
115
    reply.setErrorDescription( i18n( "The mount process could not be started." ) );
 
116
    return reply;
 
117
  }
 
118
 
 
119
  return reply;
 
120
}
 
121
 
 
122
 
 
123
 
 
124
ActionReply Smb4KMountHelper::unmount( const QVariantMap &args )
 
125
{
 
126
  ActionReply reply;
 
127
  reply.addData( "share_url", args["share_url"] );
 
128
  reply.addData( "share_mountpoint", args["share_mountpoint"] );
 
129
  reply.addData( "key", args["key"] );
 
130
 
 
131
  // Check if the mountpoint is valid and the file system is
 
132
  // also correct.
 
133
  bool mountpoint_ok = false;
 
134
  KMountPoint::List mountpoints = KMountPoint::currentMountPoints( KMountPoint::BasicInfoNeeded|KMountPoint::NeedMountOptions );
 
135
 
 
136
  for( int j = 0; j < mountpoints.size(); j++ )
 
137
  {
 
138
#ifndef Q_OS_FREEBSD
 
139
    if ( QString::compare( args["share_mountpoint"].toString(),
 
140
                           mountpoints.at( j )->mountPoint(), Qt::CaseInsensitive ) == 0 &&
 
141
         QString::compare( mountpoints.at( j )->mountType(), "cifs", Qt::CaseInsensitive ) == 0 )
 
142
#else
 
143
    if ( QString::compare( args["share_mountpoint"].toString(),
 
144
                           mountpoints.at( j )->mountPoint(), Qt::CaseInsensitive ) == 0 &&
 
145
         QString::compare( mountpoints.at( j )->mountType(), "smbfs", Qt::CaseInsensitive ) == 0 )
 
146
#endif
 
147
    {
 
148
      mountpoint_ok = true;
 
149
      break;
 
150
    }
 
151
    else
 
152
    {
 
153
      continue;
 
154
    }
 
155
  }
 
156
 
 
157
  if ( !mountpoint_ok )
 
158
  {
 
159
    reply.setErrorCode( ActionReply::HelperError );
 
160
    reply.setErrorDescription( i18n( "The mountpoint is invalid." ) );
 
161
    return reply;
 
162
  }
 
163
  else
 
164
  {
 
165
    // Do nothing
 
166
  }
 
167
 
 
168
  KProcess proc( this );
 
169
  proc.setOutputChannelMode( KProcess::SeparateChannels );
 
170
  proc.setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
 
171
  proc.setProgram( args["umount_command"].toStringList() );
 
172
 
 
173
  // Run the unmount process.
 
174
  proc.start();
 
175
 
 
176
  if ( proc.waitForStarted( -1 ) )
 
177
  {
 
178
    // We want to be able to terminate the process from outside.
 
179
    // Thus, we implement a loop that checks periodically, if we
 
180
    // need to kill the process.
 
181
    bool user_kill = false;
 
182
 
 
183
    while ( !proc.waitForFinished( 10 ) )
 
184
    {
 
185
      if ( HelperSupport::isStopped() )
 
186
      {
 
187
        proc.kill();
 
188
        user_kill = true;
 
189
        break;
 
190
      }
 
191
      else
 
192
      {
 
193
        // Do nothing
 
194
      }
 
195
    }
 
196
 
 
197
    if ( proc.exitStatus() == KProcess::CrashExit )
 
198
    {
 
199
      if ( !user_kill )
 
200
      {
 
201
        reply.setErrorCode( ActionReply::HelperError );
 
202
        reply.setErrorDescription( i18n( "The unmount process crashed." ) );
 
203
        return reply;
 
204
      }
 
205
      else
 
206
      {
 
207
        // Do nothing
 
208
      }
 
209
    }
 
210
    else
 
211
    {
 
212
      // Check if there is output on stderr.
 
213
      QString stderr = QString::fromUtf8( proc.readAllStandardError() );
 
214
      reply.addData( "stderr", stderr );
 
215
 
 
216
      // Check if there is output on stdout.
 
217
      QString stdout = QString::fromUtf8( proc.readAllStandardOutput() );
 
218
      reply.addData( "stdout", stdout );
 
219
    }
 
220
  }
 
221
  else
 
222
  {
 
223
    reply.setErrorCode( ActionReply::HelperError );
 
224
    reply.setErrorDescription( i18n( "The unmount process could not be started." ) );
 
225
    return reply;
 
226
  }
 
227
 
 
228
  return reply;
 
229
}
 
230
 
 
231
#include "smb4kmounthelper.moc"