File: blockdev.cpp

package info (click to toggle)
aethera 0.9.3-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,588 kB
  • ctags: 7,282
  • sloc: cpp: 64,544; sh: 9,913; perl: 1,756; makefile: 1,680; python: 258
file content (112 lines) | stat: -rw-r--r-- 2,983 bytes parent folder | download
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
/*
 * This file is part of Magellan <http://www.kAlliance.org/Magellan>
 *
 * Copyright (c) 1998-1999 Teodor Mihai <teddy@ireland.com>
 * Copyright (c) 1998-1999 Laur Ivan <laur.ivan@ul.ie>
 * Copyright (c) 1999 Virgil Palanciuc <vv@ulise.cs.pub.ro>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 * Also requires the KDE libraries, available at no cost at
 * http://www.kde.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.
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <blockdev.h>
#include <qfile.h>
#include <qdatastream.h>
#include <transactionserver.h>

//#define DEBUG_BLOCKDEV

BlockDevice::BlockDevice(QString filename, QString prefix):DataCollection(prefix)
{
#ifdef DEBUG_BLOCKDEV
	printf("BlockDevice: entering the constructor...\n");
#endif
  storageDevice=filename;
  type=FileStorage;
  buildEntryList();
}

BlockDevice::~BlockDevice()
{
}

void BlockDevice::buildEntryList()
{
#ifdef DEBUG_BLOCKDEV
	printf("BlockDevice: Building entry list...\n");
#endif
  QFile dfile(storageDevice);
  if(dfile.open(IO_ReadOnly))
  {
    QDataStream stream(&dfile);
    while(!stream.atEnd())
    {
			// create a new object
      int offset=dfile.at();
      ObjectReference *tobj=new ObjectReference;
			QString id;
		
			// read the object id
			
			stream>>id;
			
			// test the id
			
			if(!isValidID(id) || getEntryDict()[id])
				id=nextID(); // not valid, reassign id
			
			// read the object data
      stream>>tobj->rawData;
      tobj->length=tobj->rawData.size();
      tobj->offset=offset;
			tobj->id=id;
			tobj->isNewObject=false;

			// debug
#ifdef DEBUG_BLOCKDEV
      printf("  -block object id: %s, device: %s, offset: %d, length: %d\n",
					(const char *)id,
					(const char *)storageDevice, 
          tobj->offset, tobj->length);
#endif
			addEntry(tobj);
			// set lastID accordingly
			unsigned long idNumber=id.mid(idPrefix.length()).toULong();
			if(idNumber>lastIDNum)
				lastIDNum=idNumber;			
    }
  }
}

bool BlockDevice::removeEntry(QString id, bool sync)
{
  ObjectReference *t=ref(id);
  if(t)
  {
    objectList.remove(t);
    delete(t);
    if(sync)
      return TransactionServer::thisInstance()->save(this);
    else
      return true; // sync later
  }
  
  return false; // object does not exist
}