summaryrefslogtreecommitdiffstats
path: root/src/k3blsofwrapper.cpp
blob: ee44f0667a71d9aa54da10157281325c8937a319 (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
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3blsofwrapper.h"

#include <k3bdevice.h>
#include <k3bprocess.h>
#include <k3bglobals.h>

#include <tqfile.h>
#include <tqfileinfo.h>

#include <sys/types.h>
#include <unistd.h>

static K3bLsofWrapper::Process createProcess( const TQString& name, int pid )
{
  K3bLsofWrapper::Process p;
  p.name = name;
  p.pid = pid;
  return p;
}


class K3bLsofWrapper::Private
{
public:
  TQValueList<Process> apps;
  TQString lsofBin;
};


K3bLsofWrapper::K3bLsofWrapper()
{
  d = new Private;
}


K3bLsofWrapper::~K3bLsofWrapper()
{
  delete d;
}


bool K3bLsofWrapper::checkDevice( K3bDevice::Device* dev )
{
  d->apps.clear();

  if( !findLsofExecutable() )
    return false;

  // run lsof
  TDEProcess p;
  K3bProcessOutputCollector out( &p );

  //
  // We use the following output form: 
  // p<PID>
  // c<COMMAND_NAME>
  //
  p << d->lsofBin << "-Fpc" << dev->blockDeviceName();

  if( !p.start( TDEProcess::Block, TDEProcess::Stdout ) )
    return false;

  //
  // now process its output
  TQStringList l = TQStringList::split( "\n", out.output() );
  for( TQStringList::iterator it = l.begin(); it != l.end(); ++it ) {
    int pid = (*it).mid(1).toInt();
    TQString app = (*(++it)).mid(1);

    kdDebug() << "(K3bLsofWrapper) matched: app: " << app << " pid: " << pid << endl;

    // we don't care about ourselves using the device ;)
    if( pid != (int)::getpid() )
      d->apps.append( createProcess( app, pid ) );
  }

  return true;
}


const TQValueList<K3bLsofWrapper::Process>& K3bLsofWrapper::usingApplications() const
{
  return d->apps;
}


bool K3bLsofWrapper::findLsofExecutable()
{
  if( d->lsofBin.isEmpty() )
    d->lsofBin = K3b::findExe( "lsof" );

  return !d->lsofBin.isEmpty();
}