• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • io
kar.cpp
Go to the documentation of this file.
1 
2 /* This file is part of the KDE libraries
3  Copyright (C) 2002 Laurence Anderson <l.d.anderson@warwick.ac.uk>
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 version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kar.h"
21 
22 #include <QtCore/QFile>
23 #include <QtCore/QDir>
24 #include <time.h>
25 #include <kdebug.h>
26 #include <kmimetype.h>
27 #include <QtCore/QRegExp>
28 
29 #include "kfilterdev.h"
30 //#include "klimitediodevice_p.h"
31 
35 
36 class KAr::KArPrivate
37 {
38 public:
39  KArPrivate() {}
40 };
41 
42 KAr::KAr( const QString& filename )
43  : KArchive( filename ), d(new KArPrivate)
44 {
45 }
46 
47 KAr::KAr( QIODevice * dev )
48  : KArchive( dev ), d(new KArPrivate)
49 {
50 }
51 
52 KAr::~KAr()
53 {
54  if( isOpen() )
55  close();
56  delete d;
57 }
58 
59 bool KAr::doPrepareWriting( const QString&, const QString&, const QString&,
60  qint64, mode_t, time_t, time_t, time_t )
61 {
62  return false;
63 }
64 
65 bool KAr::doFinishWriting( qint64 )
66 {
67  return false;
68 }
69 
70 bool KAr::doWriteDir( const QString&, const QString&, const QString&,
71  mode_t, time_t, time_t, time_t )
72 {
73  return false;
74 }
75 
76 bool KAr::doWriteSymLink( const QString&, const QString&, const QString&,
77  const QString&, mode_t, time_t, time_t, time_t )
78 {
79  return false;
80 }
81 
82 bool KAr::openArchive( QIODevice::OpenMode mode )
83 {
84  // Open archive
85 
86  if ( mode == QIODevice::WriteOnly )
87  return true;
88  if ( mode != QIODevice::ReadOnly && mode != QIODevice::ReadWrite )
89  {
90  kWarning(7042) << "Unsupported mode " << mode;
91  return false;
92  }
93 
94  QIODevice* dev = device();
95  if ( !dev )
96  return false;
97 
98  QByteArray magic = dev->read( 7 );
99  if ( magic != "!<arch>" ) {
100  kWarning(7042) << "Invalid main magic";
101  return false;
102  }
103 
104  char *ar_longnames = 0;
105  while (! dev->atEnd()) {
106  QByteArray ar_header;
107  ar_header.resize(61);
108  QByteArray name;
109  int date, uid, gid, mode;
110  qint64 size;
111 
112  dev->seek( dev->pos() + (2 - (dev->pos() % 2)) % 2 ); // Ar headers are padded to byte boundary
113 
114  if ( dev->read(ar_header.data(), 60) != 60 ) { // Read ar header
115  kWarning(7042) << "Couldn't read header";
116  delete[] ar_longnames;
117  //return false;
118  return true; // Probably EOF / trailing junk
119  }
120 
121  if (!ar_header.endsWith("`\n")) { // Check header magic // krazy:exclude=strings
122  kWarning(7042) << "Invalid magic";
123  delete[] ar_longnames;
124  return false;
125  }
126 
127  name = ar_header.mid( 0, 16 ); // Process header
128  date = ar_header.mid( 16, 12 ).toInt();
129  uid = ar_header.mid( 28, 6 ).toInt();
130  gid = ar_header.mid( 34, 6 ).toInt();
131  mode = ar_header.mid( 40, 8 ).toInt();
132  size = ar_header.mid( 48, 10 ).toInt();
133 
134  bool skip_entry = false; // Deal with special entries
135  if (name.mid(0, 1) == "/") {
136  if (name.mid(1, 1) == "/") { // Longfilename table entry
137  delete[] ar_longnames;
138  ar_longnames = new char[size + 1];
139  ar_longnames[size] = '\0';
140  dev->read(ar_longnames, size);
141  skip_entry = true;
142  kDebug(7042) << "Read in longnames entry";
143  } else if (name.mid(1, 1) == " ") { // Symbol table entry
144  kDebug(7042) << "Skipped symbol entry";
145  dev->seek( dev->pos() + size );
146  skip_entry = true;
147  } else { // Longfilename
148  kDebug(7042) << "Longfilename #" << name.mid(1, 15).toInt();
149  if (! ar_longnames) {
150  kWarning(7042) << "Invalid longfilename reference";
151  delete[] ar_longnames;
152  return false;
153  }
154  name = &ar_longnames[name.mid(1, 15).toInt()];
155  name = name.left(name.indexOf("/"));
156  }
157  }
158  if (skip_entry) continue;
159 
160  name = name.trimmed(); // Process filename
161  name.replace( '/', QByteArray() );
162  kDebug(7042) << "Filename: " << name << " Size: " << size;
163 
164  KArchiveEntry* entry = new KArchiveFile(this, QString::fromLocal8Bit(name), mode, date,
165  /*uid*/ QString(), /*gid*/ QString(), /*symlink*/ QString(),
166  dev->pos(), size);
167  rootDir()->addEntry(entry); // Ar files don't support directories, so everything in root
168 
169  dev->seek( dev->pos() + size ); // Skip contents
170  }
171  delete[] ar_longnames;
172 
173  return true;
174 }
175 
176 bool KAr::closeArchive()
177 {
178  // Close the archive
179  return true;
180 }
181 
182 void KAr::virtual_hook( int id, void* data )
183 { KArchive::virtual_hook( id, data ); }
KAr::closeArchive
virtual bool closeArchive()
Closes the archive.
Definition: kar.cpp:176
qint64
KAr::openArchive
virtual bool openArchive(QIODevice::OpenMode mode)
Opens the archive for reading.
Definition: kar.cpp:82
KArchive::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: karchive.cpp:860
kdebug.h
KArchive::rootDir
virtual KArchiveDirectory * rootDir()
Retrieves or create the root directory.
Definition: karchive.cpp:391
kmimetype.h
kfilterdev.h
KArchive::device
QIODevice * device() const
The underlying device.
Definition: karchive.cpp:475
KArchive
KArchive is a base class for reading and writing archives.
Definition: karchive.h:43
QString
KArchive::close
virtual bool close()
Closes the archive.
Definition: karchive.cpp:164
KArchive::mode
QIODevice::OpenMode mode() const
Returns the mode in which the archive was opened.
Definition: karchive.cpp:470
kar.h
KArchiveEntry
A base class for entries in an KArchive.
Definition: karchive.h:369
KAr::doWriteSymLink
virtual bool doWriteSymLink(const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, time_t atime, time_t mtime, time_t ctime)
Writes a symbolic link to the archive.
Definition: kar.cpp:76
KAr::doPrepareWriting
virtual bool doPrepareWriting(const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, time_t atime, time_t mtime, time_t ctime)
This virtual method must be implemented by subclasses.
Definition: kar.cpp:59
KAr::doWriteDir
virtual bool doWriteDir(const QString &name, const QString &user, const QString &group, mode_t perm, time_t atime, time_t mtime, time_t ctime)
Write a directory to the archive.
Definition: kar.cpp:70
KArchive::isOpen
bool isOpen() const
Checks whether the archive is open.
Definition: karchive.cpp:480
kWarning
#define kWarning
Definition: kdebug.h:322
KArchiveDirectory::addEntry
void addEntry(KArchiveEntry *)
Definition: karchive.cpp:757
KAr::KAr
KAr(const QString &filename)
Creates an instance that operates on the given filename.
Definition: kar.cpp:42
KAr::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: kar.cpp:182
KArchiveFile
Represents a file entry in a KArchive.
Definition: karchive.h:457
kDebug
#define kDebug
Definition: kdebug.h:316
KAr::~KAr
virtual ~KAr()
If the ar file is still opened, then it will be closed automatically by the destructor.
Definition: kar.cpp:52
QIODevice
KAr::doFinishWriting
virtual bool doFinishWriting(qint64 size)
Called after writing the data.
Definition: kar.cpp:65
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal