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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
* Copyright 2005-2009 Fabrice Colin
*
* 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.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#ifdef HAVE_ATTR_XATTR_H
#include <attr/xattr.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <set>
#include "Document.h"
#include "TimeConverter.h"
#include "Memory.h"
using std::clog;
using std::endl;
using std::string;
using std::set;
#ifdef HAVE_ATTR_XATTR_H
static char *getXAttr(int fd, const string &attrName)
{
ssize_t attrSize = fgetxattr(fd, attrName.c_str(), NULL, 0);
if (attrSize > 0)
{
char *pAttr = new char[attrSize];
if ((pAttr != NULL) &&
(fgetxattr(fd, attrName.c_str(), pAttr, attrSize) > 0))
{
return pAttr;
}
}
return NULL;
}
#endif
Document::Document() :
DocumentInfo(),
m_pData(NULL),
m_dataLength(0),
m_isMapped(false)
{
}
Document::Document(const string &title, const string &location,
const string &type, const string &language) :
DocumentInfo(title, location, type, language),
m_pData(NULL),
m_dataLength(0),
m_isMapped(false)
{
}
Document::Document(const DocumentInfo &info) :
DocumentInfo(info),
m_pData(NULL),
m_dataLength(0),
m_isMapped(false)
{
}
Document::Document(const Document &other) :
DocumentInfo(other),
m_pData(NULL),
m_dataLength(0),
m_isMapped(false)
{
// Copying does a deep copy
setData(other.m_pData, other.m_dataLength);
}
Document::~Document()
{
resetData();
}
Document& Document::operator=(const Document& other)
{
if (this != &other)
{
// Copying does a deep copy
DocumentInfo::operator=(other);
setData(other.m_pData, other.m_dataLength);
m_isMapped = false;
}
return *this;
}
bool Document::operator<(const Document& other) const
{
if (DocumentInfo::operator<(other) == false)
{
if (m_dataLength < other.m_dataLength)
{
return true;
}
return false;
}
return true;
}
/// Copies the given data in the document.
bool Document::setData(const char *data, unsigned int length)
{
if ((data == NULL) ||
(length == 0))
{
return false;
}
// Discard existing data
resetData();
m_pData = Memory::allocateBuffer(length + 1);
if (m_pData != NULL)
{
memcpy(m_pData, data, length);
m_pData[length] = '\0';
m_dataLength = length;
return true;
}
return false;
}
/// Maps the given file.
bool Document::setDataFromFile(const string &fileName)
{
struct stat fileStat;
int openFlags = O_RDONLY;
#ifdef O_CLOEXEC
openFlags = openFlags|O_CLOEXEC;
#endif
if (fileName.empty() == true)
{
return false;
}
// Make sure the file exists
if (stat(fileName.c_str(), &fileStat) != 0)
{
return false;
}
if ((!S_ISDIR(fileStat.st_mode)) &&
(!S_ISREG(fileStat.st_mode)))
{
return false;
}
if ((S_ISDIR(fileStat.st_mode)) ||
(fileStat.st_size == 0))
{
// The file is empty
resetData();
return true;
}
// Open the file in read-only mode
#ifdef O_NOATIME
int fd = open(fileName.c_str(), openFlags|O_NOATIME);
#else
int fd = open(fileName.c_str(), openFlags);
#endif
#ifdef O_NOATIME
if ((fd < 0) &&
(errno == EPERM))
{
// Try again
fd = open(fileName.c_str(), openFlags);
}
#endif
if (fd < 0)
{
clog << "Document::setDataFromFile: " << fileName << " couldn't be opened" << endl;
return false;
}
#ifndef O_CLOEXEC
int fdFlags = fcntl(fd, F_GETFD);
fcntl(fd, F_SETFD, fdFlags|FD_CLOEXEC);
#endif
// Discard existing data
resetData();
#ifdef HAVE_MMAP
// Request a private mapping of the whole file
void *mapSpace = mmap(NULL, (size_t)fileStat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapSpace != MAP_FAILED)
{
m_pData = (char*)mapSpace;
m_dataLength = fileStat.st_size;
m_isMapped = true;
#ifdef HAVE_MADVISE
if (madvise(mapSpace, (size_t)fileStat.st_size, MADV_SEQUENTIAL) != 0)
{
#ifdef DEBUG
clog << "Document::setDataFromFile: ignored memory advice" << endl;
#endif
}
#endif
}
else clog << "Document::setDataFromFile: mapping failed" << endl;
#else
m_pData = Memory::allocateBuffer(fileStat.st_size + 1);
if (m_pData != NULL)
{
if (read(fd, (void*)m_pData, fileStat.st_size) == fileStat.st_size)
{
m_pData[fileStat.st_size] = '\0';
m_dataLength = (unsigned int)fileStat.st_size;
}
else
{
Memory::freeBuffer(m_pData, fileStat.st_size + 1);
m_pData = NULL;
}
}
else clog << "Document::setDataFromFile: reading failed" << endl;
#endif
setTimestamp(TimeConverter::toTimestamp(fileStat.st_mtime));
setSize(fileStat.st_size);
#ifdef HAVE_ATTR_XATTR_H
// Any extended attributes ?
ssize_t listSize = flistxattr(fd, NULL, 0);
if (listSize > 0)
{
char *pList = new char[listSize];
if (flistxattr(fd, pList, listSize) > 0)
{
set<string> labels;
string attrList(pList, listSize);
string::size_type startPos = 0, endPos = attrList.find('\0');
while (endPos != string::npos)
{
string attrName(attrList.substr(startPos, endPos - startPos));
char *pAttr = NULL;
// FIXME: support common attributes defined at
// http://www.freedesktop.org/wiki/CommonExtendedAttributes
if (attrName == "user.mime_type")
{
pAttr = getXAttr(fd, attrName);
if (pAttr != NULL)
{
// Set the MIME type
setType(pAttr);
}
}
if (pAttr != NULL)
{
#ifdef DEBUG
clog << "Document::setDataFromFile: xattr " << attrName << "=" << pAttr << endl;
#endif
delete[] pAttr;
}
// Next
startPos = endPos + 1;
if (startPos < listSize)
{
endPos = attrList.find('\0', startPos);
}
else
{
endPos = string::npos;
}
}
if (labels.empty() == false)
{
setLabels(labels);
}
}
delete[] pList;
}
#endif
// Close the file
if (close(fd) == -1)
{
#ifdef DEBUG
clog << "Document::setDataFromFile: close failed" << endl;
#endif
}
return m_isMapped;
}
/// Returns the document's data; NULL if document is empty.
const char *Document::getData(unsigned int &length) const
{
length = m_dataLength;
return m_pData;
}
/// Resets the document's data.
void Document::resetData(void)
{
if (m_pData != NULL)
{
if (m_isMapped == false)
{
// Free
Memory::freeBuffer(m_pData, m_dataLength + 1);
}
#ifdef HAVE_MMAP
else
{
#ifdef HAVE_MADVISE
if (madvise((void*)m_pData, (size_t)m_dataLength, MADV_DONTNEED) != 0)
{
#ifdef DEBUG
clog << "Document::resetData: ignored memory advice" << endl;
#endif
}
#endif
// Unmap
munmap((void*)m_pData, (size_t)m_dataLength);
}
#endif
}
m_pData = NULL;
m_dataLength = 0;
m_isMapped = false;
}
/// Checks whether the document is binary.
bool Document::isBinary(void) const
{
unsigned int maxLen = 100;
// Look at the first 100 bytes or so
if (m_dataLength < 100)
{
maxLen = m_dataLength;
}
for (unsigned int i = 0; i < maxLen; ++i)
{
if (isascii(m_pData[i]) == 0)
{
#ifdef DEBUG
clog << "Document::isBinary: " << m_pData[i] << endl;
#endif
return true;
}
}
return false;
}
|