10 #define OFFSET(s, m) ((uint) (&((s *) 0)->m))
11 #define MDWORD(a,b) ((((uint32)((uint16)(b)))<<16)+((uint16)(a)))
13 #define EAR_RECOGNITION { 'e', 'A', 'R', 228, 11, 12, 3, 0 }
15 static byte earRecognition[] = EAR_RECOGNITION;
17 static class FreeBlock : struct
23 static struct EARHeader
25 byte recognition[sizeof(earRecognition)] __attribute__((packed));
26 uint version  __attribute__((packed));
27 FileSize totalSize  __attribute__((packed));
30 static enum EAREntryType { ENTRY_FILE = 1, ENTRY_FOLDER = 2 };
32 static struct EAREntry
34 EAREntryType type __attribute__((packed));
35 TimeStamp32 created, modified __attribute__((packed));
36 FileSize size, cSize __attribute__((packed));
37 uint prev, next __attribute__((packed));
38 uint nameLen __attribute__((packed));
39 // null terminated file name follows
42 static File EAROpenArchive(char * archive, EARHeader header)
47 char moduleName[MAX_LOCATION];
48 char * name = archive + 1;
49 #if defined(__ANDROID__)
51 name = ((SubModule)__thisModule.application.modules.first).next.module.name;
54 if(LocateModule(name, moduleName))
55 f = FileOpen(moduleName, read);
58 f = FileOpen(archive, read);
63 // First attempt to treat this as an archive file
64 if(f.Read(header, sizeof(EARHeader), 1) == 1 &&
65 !strncmp(header.recognition, earRecognition, sizeof(earRecognition)))
68 // Then try to see if an archive is at the end of the file
69 f.Seek(-(int)sizeof(uint), end);
70 f.Read(&archiveSize, sizeof(uint), 1);
71 f.Seek(-(int)archiveSize, end);
72 if(f.Read(header, sizeof(EARHeader), 1) == 1 &&
73 !strncmp(header.recognition, earRecognition, sizeof(earRecognition)))
81 static FileAttribs EARGetEntry(File f, EAREntry entry, char * name, char * path)
83 uint first = 0, last = 0;
85 return FileAttribs { isDirectory = true };
86 if(!f.Read(&first, sizeof(uint), 1))
88 f.Read(&last, sizeof(uint), 1);
91 char namePart[MAX_FILENAME], nameRest[MAX_LOCATION];
92 SplitDirectory(name, namePart, nameRest);
94 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
95 strcpy(namePart, DIR_SEPS);
100 char fileName[MAX_FILENAME];
102 f.Read(entry, sizeof(EAREntry), 1);
103 f.Read(fileName, 1, entry.nameLen);
104 fileName[entry.nameLen] = '\0';
106 if(!strcmp(fileName, "/") || !strcmp(fileName, "\\"))
107 strcpy(fileName, DIR_SEPS);
109 if(!fstrcmp(fileName, namePart))
112 PathCat(path, fileName);
114 return EARGetEntry(f, entry, nameRest, path);
116 return (entry.type == ENTRY_FILE) ? FileAttribs { isFile = true } : FileAttribs { isDirectory = true };
119 f.Seek(entry.next, start);
124 return FileAttribs { };
127 #if !defined(ECERE_NOARCHIVE) && !defined(ECERE_VANILLA)
128 class EARArchive : Archive
131 //BufferedFile bf { };
132 // char path[MAX_LOCATION];
144 end = ((FreeBlock)freeBlocks.last).start;
147 f.Seek(archiveStart + OFFSET(EARHeader, totalSize), start);
148 f.Write(&totalSize, sizeof(uint), 1);
157 f.Write(&end, sizeof(uint), 1);
158 f.Truncate(archiveStart + end);
166 if(f && rootDir && writeAccess)
170 archiveStart += Update();
175 f.Unlock(0, 0, true);
179 /*if(rootDir && writeAccess)
181 // Fix the size of the archive
182 FileTruncate(path, archiveStart);
185 freeBlocks.Free(null);
194 ArchiveDir OpenDirectory(char * name, FileStats stats, ArchiveAddMode addMode)
196 ArchiveDir result = null;
197 EARArchiveDir dir { readOnly = addMode == readOnlyDir };
200 char namePart[MAX_LOCATION] = "", nameRest[MAX_LOCATION];
205 strcpy(nameRest, name);
207 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
208 strcpy(namePart, DIR_SEPS);
210 // Search for directory
213 dir.position = rootDir;
214 if(f.Seek(dir.position, start))
219 f.Read(&dir.first, sizeof(uint), 1);
220 f.Read(&dir.last, sizeof(uint), 1);
226 // If directory doesn't exist already
227 if(!result && addMode != refresh)
229 rootDir = Position(2*sizeof(uint));
230 dir.position = rootDir;
235 // Open rest of directory...
236 if(result && nameRest[0])
238 result = dir.OpenDirectory(nameRest, stats, addMode);
245 uint Position(uint size)
248 for(block = freeBlocks.first; block; block = block.next)
250 if(block.end - block.start + 1 >= size)
252 uint position = block.start;
253 if(block.end - block.start + 1 == size)
254 freeBlocks.Delete(block);
263 bool DefragOffset(uint * offset)
267 for(block = freeBlocks.first; block; block = block.next)
269 if(*offset > block.start)
270 subtract += block.end - block.start + 1;
282 #define MAX_BUFFERSIZE 0x400000
284 void Defrag(uint dirPosition)
286 // Update all offsets within the files
287 uint first = 0, last = 0;
288 uint position = 0, next = 0;
290 f.Seek(dirPosition, start);
291 f.Read(&first, sizeof(uint), 1);
292 f.Read(&last, sizeof(uint), 1);
296 if(first && DefragOffset(&first))
298 if(f.Seek(dirPosition, start))
299 f.Write(&first, sizeof(uint), 1);
301 if(last && DefragOffset(&last))
303 if(f.Seek(dirPosition + sizeof(uint), start))
304 f.Write(&last, sizeof(uint), 1);
307 for(; position; position = next)
311 if(f.Seek(position, start) && f.Read(entry, sizeof(EAREntry), 1))
315 if(entry.prev && DefragOffset(&entry.prev))
317 f.Seek(position + OFFSET(EAREntry, prev), start);
318 f.Write(&entry.prev, sizeof(uint), 1);
320 if(entry.next && DefragOffset(&entry.next))
322 f.Seek(position + OFFSET(EAREntry, next), start);
323 f.Write(&entry.next, sizeof(uint), 1);
326 if(entry.type == ENTRY_FOLDER)
327 Defrag(position + sizeof(EAREntry) + entry.nameLen);
333 // Move all the blocks
334 if(dirPosition == rootDir)
337 byte * buffer = null;
338 FreeBlock block, nextBlock;
339 for(block = freeBlocks.first; block && block.next; block = nextBlock)
343 nextBlock = block.next;
344 dataSize = nextBlock.start - (block.end + 1);
346 if(dataSize > bufferSize && (!bufferSize || bufferSize < MAX_BUFFERSIZE))
348 bufferSize = Min(dataSize, MAX_BUFFERSIZE);
349 buffer = renew buffer byte[bufferSize];
352 for(c = 0; c<dataSize; c += bufferSize)
354 uint size = (dataSize > c + bufferSize) ? bufferSize : (dataSize - c);
356 // Read block of data
357 f.Seek((block.end + 1) + c, start);
358 f.Read(buffer, size, 1);
360 // Write block of data
361 f.Seek(block.start + c, start);
362 f.Write(buffer, size, 1);
365 nextBlock.start -= (block.end - block.start) + 1;
367 freeBlocks.Delete(block);
373 uint Find(EARArchiveDir directory, char * namePart, EAREntry entry)
376 for(position = directory.first; position; position = entry.next)
378 char fileName[MAX_FILENAME];
380 if(f.Seek(position, start) && f.Read(entry, sizeof(EAREntry), 1))
382 if(entry.nameLen > MAX_FILENAME)
383 return 0; // CORRUPTION ERROR
384 f.Read(fileName, 1, entry.nameLen);
385 fileName[entry.nameLen] = '\0';
387 if(!strcmp(fileName, "/") || !strcmp(fileName, "\\"))
388 strcpy(fileName, DIR_SEPS);
390 if(!fstrcmp(fileName, namePart))
394 return 0; // ERROR OUT OF SPACE?
399 void AddFreeBlock(uint position, uint size)
401 FreeBlock block, prevBlock, nextBlock = null;
403 // Find the previous and next free block
405 for(block = freeBlocks.first; block; block = block.next)
406 if(block.end < position)
414 // Try to merge with previous block
415 if(prevBlock && prevBlock.end + 1 == position)
417 prevBlock.end += size;
418 // Try to merge with next block as well
419 if(nextBlock && nextBlock.start == prevBlock.end + 1)
421 prevBlock.end = nextBlock.end;
422 freeBlocks.Delete(nextBlock);
425 // Try to merge with next block
426 else if(nextBlock && nextBlock.start == position + size)
428 nextBlock.start = position;
430 // This free block is not connected to any other block
433 freeBlocks.Insert(prevBlock, FreeBlock { start = position, end = position + size - 1 });
437 void SubtractBlock(uint start, uint size)
440 for(block = freeBlocks.first; block; block = block.next)
442 if(block.end >= start - 1L && block.start <= start + size)
444 if(block.end > start + size && block.start < start - 1L)
446 FreeBlock newBlock { start = start + size, end = block.end };
447 block.end = start - 1L;
448 freeBlocks.Insert(block, newBlock);
450 else if(block.end > start + size)
452 block.start = start + size;
454 else if(block.start < start - 1L)
456 block.end = start - 1L;
460 freeBlocks.Remove(block);
468 void Delete(EARArchiveDir dir, uint position, EAREntry entry)
471 if(entry.type == ENTRY_FOLDER)
473 EARArchiveDir subDir {};
477 subDir.position = dir.position;
478 f.Read(&subDir.first, sizeof(uint), 1);
479 f.Read(&subDir.last, sizeof(uint), 1);
481 // Erase directory contents first
482 for(filePosition = subDir.first; filePosition; filePosition = fileEntry.next)
484 f.Seek(filePosition, start);
485 f.Read(&fileEntry, sizeof(EAREntry), 1);
486 f.Seek(fileEntry.nameLen, current);
487 Delete(subDir, filePosition, &fileEntry);
489 size = sizeof(EAREntry) + entry.nameLen + 2 * sizeof(uint);
493 size = sizeof(EAREntry) + entry.nameLen + (entry.cSize ? entry.cSize : entry.size);
498 f.Seek(entry.prev + OFFSET(EAREntry, next), start);
499 f.Write(&entry.next, sizeof(uint), 1);
503 f.Seek(entry.next + OFFSET(EAREntry, prev), start);
504 f.Write(&entry.prev, sizeof(uint), 1);
506 if(dir.last == position) dir.last = entry.prev;
507 if(dir.first == position) dir.first = entry.next;
509 AddFreeBlock(position, size);
510 totalSize -= entry.size;
516 File FileOpen(char * name)
522 char fileName[MAX_LOCATION];
525 f.Seek(archiveStart + sizeof(EARHeader), start);
526 if(EARGetEntry(f, entry, name, null).isFile)
530 byte * uncompressed = new byte[entry.size];
533 byte * compressed = new byte[entry.cSize];
536 if(f.Read(compressed, 1, entry.cSize) == entry.cSize)
538 unsigned long destLen = entry.size;
539 uncompress(uncompressed, &destLen, compressed, entry.cSize);
540 entry.size = (FileSize)destLen; // TODO: Support 64 bit file sizes
546 file.size = entry.size;
547 file.buffer = uncompressed;
554 file.start = f.Tell();
556 file.size = entry.size;
559 file.f.Seek(file.start, start);
569 File FileOpenAtPosition(uint position)
572 char fileName[MAX_LOCATION];
574 f.Seek(position, start);
575 f.Read(entry, sizeof(EAREntry), 1);
576 /*if(entry.nameLen > 1024)
578 f.Read(fileName, 1, entry.nameLen);
581 byte * uncompressed = new byte[entry.size];
584 byte * compressed = new byte[entry.cSize];
587 if(f.Read(compressed, 1, entry.cSize) == entry.cSize)
589 unsigned long destLen = entry.size;
590 uncompress(uncompressed, &destLen, compressed, entry.cSize);
591 entry.size = (FileSize)destLen;
597 file.size = entry.size;
598 file.buffer = uncompressed;
603 file.start = f.Tell();
605 file.size = entry.size;
607 file.f.Seek(file.start, start);
613 FileAttribs FileExists(char * fileName)
617 f.Seek(archiveStart + sizeof(EARHeader), start);
618 result = EARGetEntry(f, entry, fileName, null);
622 void SubtractUsedBlocks()
625 if(!f.Read(&first, sizeof(uint), 1))
628 if(first > f.GetSize())
633 f.Read(&last, sizeof(uint), 1);
639 char fileName[MAX_FILENAME];
642 f.Seek(first, start);
643 f.Read(entry, sizeof(EAREntry), 1);
644 if(entry.nameLen < MAX_FILENAME)
646 f.Read(fileName, 1, entry.nameLen);
647 fileName[entry.nameLen] = 0;
655 size += sizeof(EAREntry) + entry.nameLen;
657 if(entry.type == ENTRY_FILE)
659 size += entry.cSize ? entry.cSize : entry.size;
661 else if(entry.type == ENTRY_FOLDER)
663 size += 2 * sizeof(uint);
664 SubtractUsedBlocks();
666 SubtractBlock(first, size);
671 void SetBufferSize(uint bufferSize)
673 if(f && f._class == class(BufferedFile))
674 ((BufferedFile)f).bufferSize = bufferSize;
677 void SetBufferRead(uint bufferRead)
679 if(f && f._class == class(BufferedFile))
680 ((BufferedFile)f).bufferRead = bufferRead;
684 class EARArchiveDir : ArchiveDir
695 archive.f.Seek(position, start);
696 archive.f.Write(&first, sizeof(uint), 1);
697 archive.f.Write(&last, sizeof(uint), 1);
702 File FileOpen(char * name)
708 char fileName[MAX_LOCATION];
711 archive.f.Seek(position, start);
712 if(EARGetEntry(archive.f, entry, name, null).isFile)
716 byte * uncompressed = new byte[entry.size];
719 byte * compressed = new byte[entry.cSize];
722 if(archive.f.Read(compressed, 1, entry.cSize) == entry.cSize)
724 unsigned long destLen = entry.size;
725 uncompress(uncompressed, &destLen, compressed, entry.cSize);
726 entry.size = (FileSize)destLen;
732 file.size = entry.size;
733 file.buffer = uncompressed;
740 file.start = archive.f.Tell();
742 file.size = entry.size;
744 file.f.Seek(file.start, start);
755 FileAttribs FileExists(char * fileName)
759 archive.f.Seek(position, start);
760 result = EARGetEntry(archive.f, entry, fileName, null);
764 ArchiveDir OpenDirectory(char * name, FileStats stats, ArchiveAddMode addMode)
766 ArchiveDir result = null;
767 EARArchiveDir dir { readOnly = addMode == readOnlyDir };
770 char namePart[MAX_LOCATION] = "", nameRest[MAX_LOCATION];
774 dir.archive = archive;
776 SplitDirectory(name, namePart, nameRest);
778 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
779 strcpy(namePart, DIR_SEPS);
781 // Search for directory
783 position = archive.Find(this, namePart, entry);
786 // Fail if file of same name already exists
787 if(entry.type == ENTRY_FILE)
791 dir.position = position + sizeof(EAREntry) + entry.nameLen;
795 archive.f.Read(&dir.first, sizeof(uint), 1);
796 archive.f.Read(&dir.last, sizeof(uint), 1);
802 // If directory doesn't exist already
803 if(!result && addMode != refresh)
805 // Write Header if it's not the root directory
809 entry.nameLen = strlen(namePart);
812 entry.type = ENTRY_FOLDER;
813 if(!nameRest[0] && stats)
815 entry.created = (TimeStamp32)stats.created;
816 entry.modified = (TimeStamp32)stats.modified;
819 position = archive.Position(sizeof(EAREntry) + entry.nameLen + 2*sizeof(uint));
821 archive.f.Seek(position, start);
822 archive.f.Write(entry, sizeof(EAREntry), 1);
823 archive.f.Write(namePart, entry.nameLen, 1);
826 if(!first) first = position;
828 // Update the next pointer of previous entry
831 archive.f.Seek(entry.prev + OFFSET(EAREntry, next), start);
832 archive.f.Write(&position, sizeof(uint), 1);
835 // Make the dir position point after the header
836 dir.position = position + sizeof(EAREntry) + entry.nameLen;
838 // Just update the time stamps
839 else if(result && !nameRest[0] && stats)
841 archive.f.Seek(position + OFFSET(EAREntry, created), start);
842 archive.f.Write(&stats.created, sizeof(uint), 1);
843 archive.f.Write(&stats.modified, sizeof(uint), 1);
847 // Open rest of directory...
848 if(result && nameRest[0])
850 result = dir.OpenDirectory(nameRest, stats, addMode);
857 bool Delete(char * name)
861 char namePart[MAX_LOCATION];
863 strcpy(namePart, name);
864 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
865 strcpy(namePart, DIR_SEPS);
867 position = archive.Find(this, namePart, entry);
870 archive.Delete(this, position, entry);
876 bool Move(char * name, EARArchiveDir to)
879 if(position != to.position)
883 char namePart[MAX_LOCATION];
885 strcpy(namePart, name);
886 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
887 strcpy(namePart, DIR_SEPS);
889 position = archive.Find(this, name, entry);
892 // Unlink from old directory
895 archive.f.Seek(entry.prev + OFFSET(EAREntry, next), start);
896 archive.f.Write(&entry.next, sizeof(uint), 1);
900 archive.f.Seek(entry.next + OFFSET(EAREntry, prev), start);
901 archive.f.Write(&entry.prev, sizeof(uint), 1);
903 if(last == position) last = entry.prev;
904 if(first == position) first = entry.next;
906 // Relink to new directory
907 entry.prev = to.last;
912 archive.f.Seek(entry.prev + OFFSET(EAREntry, next), start);
913 archive.f.Write(&position, sizeof(uint), 1);
919 archive.f.Seek(position + OFFSET(EAREntry, prev), start);
920 archive.f.Write(&entry.prev, sizeof(uint), 1);
921 archive.f.Write(&entry.next, sizeof(uint), 1);
929 bool Rename(char * name, char * newName)
934 char namePart[MAX_LOCATION];
936 strcpy(namePart, name);
937 if(!strcmp(namePart, "/") || !strcmp(namePart, "\\"))
938 strcpy(namePart, DIR_SEPS);
940 position = archive.Find(this, namePart, entry);
944 EAREntry newEntry = entry;
945 uint newPosition = position;
947 if(entry.type == ENTRY_FOLDER)
948 dataSize = 2 * sizeof(uint);
950 dataSize = entry.cSize ? entry.cSize : entry.size;
952 newEntry.nameLen = strlen(newName);
953 if(newEntry.nameLen > entry.nameLen)
956 newPosition = archive.Position(sizeof(EAREntry) + newEntry.nameLen + dataSize);
958 archive.f.Seek(newPosition, start);
959 archive.f.Write(&newEntry, sizeof(EAREntry), 1);
960 archive.f.Write(newName, sizeof(char), newEntry.nameLen);
965 archive.f.Seek(entry.prev + OFFSET(EAREntry, next), start);
966 archive.f.Write(&newPosition, sizeof(uint), 1);
970 archive.f.Seek(entry.next + OFFSET(EAREntry, prev), start);
971 archive.f.Write(&newPosition, sizeof(uint), 1);
973 if(first == position) first = newPosition;
974 if(last == position) last = newPosition;
979 archive.f.Seek(position + OFFSET(EAREntry, nameLen), start);
980 archive.f.Write(&newEntry.nameLen, sizeof(uint), 1);
981 archive.f.Seek(position + sizeof(EAREntry), start);
982 archive.f.Write(newName, sizeof(char), newEntry.nameLen);
984 // There will be free space at the end of an entry with a shorter new name
985 if(newEntry.nameLen < entry.nameLen)
986 archive.AddFreeBlock(position + sizeof(EAREntry) + newEntry.nameLen + dataSize, entry.nameLen - newEntry.nameLen);
988 if(entry.nameLen != newEntry.nameLen)
991 uint bufferSize = Min(dataSize, MAX_BUFFERSIZE);
992 buffer = new byte[bufferSize];
995 uint readPosition = position + sizeof(EAREntry) + entry.nameLen;
996 uint writePosition = newPosition + sizeof(EAREntry) + newEntry.nameLen;
999 for(c = 0; c<dataSize; c += bufferSize)
1001 uint size = (dataSize > c + bufferSize) ? bufferSize : (dataSize - c);
1003 archive.f.Seek(readPosition + c, start);
1004 archive.f.Read(buffer, size, 1);
1006 archive.f.Seek(writePosition + c, start);
1007 archive.f.Write(buffer, size, 1);
1012 if(newEntry.nameLen > entry.nameLen)
1014 // Prevent the children to be deleted
1015 if(entry.type == ENTRY_FOLDER)
1017 uint first = 0, last = 0;
1018 archive.f.Seek(position + sizeof(EAREntry) + entry.nameLen, start);
1019 archive.f.Write(&first, sizeof(uint), 1);
1020 archive.f.Write(&last, sizeof(uint), 1);
1023 // Delete the old entry
1024 entry.prev = entry.next = 0;
1025 archive.f.Seek(position + sizeof(EAREntry) + entry.nameLen, start);
1026 archive.Delete(this, position, entry);
1034 bool AddFromFile(char * name, File input, FileStats stats, ArchiveAddMode addMode, int compression, int * ratio, uint * newPosition)
1036 // Search for identical entry
1038 uint oldPosition = archive.Find(this, name, oldEntry);
1039 return _AddFromFileAtPosition(oldEntry, oldPosition, name, input, stats, addMode, compression, ratio, newPosition);
1042 bool AddFromFileAtPosition(uint oldPosition, char * name, File input, FileStats stats, ArchiveAddMode addMode, int compression, int * ratio, uint * newPosition)
1047 if(!archive.f.Seek(oldPosition, start) || !archive.f.Read(oldEntry, sizeof(EAREntry), 1))
1050 return _AddFromFileAtPosition(oldEntry, oldPosition, name, input, stats, addMode, compression, ratio, newPosition);
1053 bool _AddFromFileAtPosition(EAREntry oldEntry, uint oldPosition, char * name, File input, FileStats stats, ArchiveAddMode addMode, int compression, int * ratio, uint * newPosition)
1055 bool result = false;
1057 FileStats oldStats { };
1061 oldStats.modified = (TimeStamp)oldEntry.modified;
1062 oldStats.created = (TimeStamp)oldEntry.created;
1066 oldStats.size = input.GetSize();
1075 archive.Delete(this, oldPosition, oldEntry);
1077 // Only updates changed files
1080 (oldEntry.size != stats.size ||
1081 oldEntry.modified != (TimeStamp32)stats.modified ||
1082 oldEntry.created != (TimeStamp32)stats.created))
1083 archive.Delete(this, oldPosition, oldEntry);
1087 // Only updates changed or new files
1091 if(oldEntry.size != stats.size ||
1092 oldEntry.modified != (TimeStamp32)stats.modified ||
1093 oldEntry.created != (TimeStamp32)stats.created)
1094 archive.Delete(this, oldPosition, oldEntry);
1104 uint position, size;
1105 byte * compressed = null;
1108 entry.nameLen = strlen(name);
1111 entry.type = ENTRY_FILE;
1113 entry.size = stats.size;
1114 entry.created = (TimeStamp32)stats.created;
1115 entry.modified = (TimeStamp32)stats.modified;
1119 byte * uncompressed = new byte[entry.size];
1122 if(input.Read(uncompressed, 1, entry.size) == entry.size)
1124 unsigned long destLen = entry.size + entry.size / 1000 + 12;
1126 compressed = new byte[destLen];
1129 compress2(compressed, &destLen, uncompressed, entry.size, compression);
1130 entry.cSize = (FileSize)destLen;
1133 delete uncompressed;
1144 *ratio = entry.size ? (entry.cSize * 1000 / entry.size) : 0;
1147 size = sizeof(EAREntry) + entry.nameLen + (entry.cSize ? entry.cSize : entry.size);
1148 position = archive.Position(size);
1151 if(!archive.f.Seek(position, start) ||
1152 !archive.f.Write(entry, sizeof(EAREntry), 1) ||
1153 !archive.f.Write(name, entry.nameLen, 1))
1162 if(!archive.f.Write(compressed, 1, entry.cSize))
1174 for(c = 0; c<entry.size && count; c+= count)
1176 count = input.Read(buffer, 1, sizeof(buffer));
1177 if(!archive.f.Write(buffer, 1, count))
1182 // Update the next pointer previous entry
1185 archive.f.Seek(entry.prev + OFFSET(EAREntry, next), start);
1186 archive.f.Write(&position, sizeof(uint), 1);
1189 // Update total size of archive
1190 archive.totalSize += entry.size;
1193 if(!first) first = position;
1194 if(newPosition) *newPosition = position;
1198 if(newPosition) *newPosition = 0;
1201 // archive.f.handle = archive.f;
1207 // Directory Description for file listing
1208 class EARDir : struct
1210 char path[MAX_LOCATION];
1215 class EARFile : File
1220 // -- For reading compressed file (entirely buffered)
1223 // -- For reading uncompressed file (not buffered)
1245 int Read(byte * buffer, uint size, uint count)
1249 f.Seek(position + start, start);
1250 read = Min(count, (this.size - position) / size);
1252 CopyBytes(buffer, this.buffer + position, read * size);
1254 read = f.Read(buffer, size, read);
1255 position += read * size;
1259 int Write(byte * buffer, uint size, uint count)
1264 bool Getc(char * ch)
1270 char b = buffer[position++];
1276 f.Seek(position + start, start);
1289 bool Puts(char * string)
1294 bool Seek(int pos, FileSeekMode mode)
1296 bool result = false;
1300 if(pos <= (int)size)
1304 result = f.Seek(position + start, start);
1310 if(position + pos <= (int)size && (int)position >= -pos)
1314 result = f.Seek(position + start, start);
1320 if(pos < 0 && -pos <= (int)size)
1322 position = size + pos;
1324 f.Seek(position + start, start);
1340 return position >= size || (f && f.Eof());
1349 class EARFileSystem : FileSystem
1351 File ::Open(char * archive, char * name, FileOpenMode mode)
1359 char fileName[MAX_LOCATION];
1361 File f = EAROpenArchive(archive, &header);
1362 strcpy(fileName, name);
1364 if(!f && archive[0] == ':')
1366 f = EAROpenArchive(":", &header);
1369 strcpy(fileName, archive + 1);
1370 PathCat(fileName, name);
1377 if(EARGetEntry(f, entry, fileName, null).isFile)
1381 byte * uncompressed = new byte[entry.size];
1384 byte * compressed = new byte[entry.cSize];
1387 if(f.Read(compressed, 1, entry.cSize) == entry.cSize)
1389 unsigned long destLen = entry.size;
1390 uncompress(uncompressed, &destLen, compressed, entry.cSize);
1391 entry.size = (FileSize)destLen;
1397 file.size = entry.size;
1398 file.buffer = uncompressed;
1405 file.start = f.Tell();
1407 file.size = entry.size;
1423 FileAttribs ::Exists(char * archive, char * fileName)
1427 File f = EAROpenArchive(archive, &header);
1431 result = EARGetEntry(f, entry, fileName, null);
1437 bool ::GetSize(char * archive, char * fileName, FileSize * size)
1439 bool result = false;
1441 File f = EAROpenArchive(archive, &header);
1445 if(EARGetEntry(f, entry, fileName, null))
1453 bool ::Stats(char * archive, char * fileName, FileStats stats)
1455 bool result = false;
1457 File f = EAROpenArchive(archive, &header);
1461 if(EARGetEntry(f, entry, fileName, null))
1463 stats.size = entry.size;
1465 stats.modified = (TimeStamp)entry.modified;
1466 stats.created = (TimeStamp)entry.created;
1474 void ::FixCase(char * archive, char * name)
1478 File f = EAROpenArchive(archive, &header);
1482 char fileName[MAX_LOCATION] = "";
1483 if(EARGetEntry(f, entry, name, fileName))
1484 strcpy(name, fileName);
1490 bool ::Find(FileDesc file, char * archive, char * name)
1492 bool result = false;
1497 File f = EAROpenArchive(archive, &header);
1501 if(EARGetEntry(f, entry, name, null).isDirectory)
1505 sprintf(d.path, "<%s>%s", archive, name);
1507 f.Read(&first, sizeof(uint), 1);
1508 f.Read(&last, sizeof(uint), 1);
1513 d.f.Seek(d.next, start);
1514 d.f.Read(entry, sizeof(EAREntry), 1);
1515 d.f.Read(file.name, 1, entry.nameLen);
1516 file.name[entry.nameLen] = '\0';
1517 file.stats.attribs = { isDirectory = (entry.type == ENTRY_FOLDER), isFile = (entry.type != ENTRY_FOLDER) };
1518 file.stats.accessed = file.stats.modified = (TimeStamp)entry.modified;
1519 file.stats.created = (TimeStamp)entry.created;
1520 file.stats.size = entry.size;
1522 strcpy(file.path, d.path);
1523 PathCat(file.path, file.name);
1524 d.next = entry.next;
1540 bool ::FindNext(FileDesc file)
1542 bool result = false;
1543 EARDir d = (EARDir)file.dir;
1547 d.f.Seek(d.next, start);
1548 d.f.Read(entry, sizeof(EAREntry), 1);
1549 d.f.Read(file.name, 1, entry.nameLen);
1550 file.name[entry.nameLen] = '\0';
1551 file.stats.attribs = FileAttribs { isDirectory = (entry.type == ENTRY_FOLDER), isFile = (entry.type != ENTRY_FOLDER) };
1552 file.stats.accessed = file.stats.modified = (TimeStamp)entry.modified;
1553 file.stats.created = (TimeStamp)entry.created;
1554 file.stats.size = entry.size;
1556 strcpy(file.path, d.path);
1557 PathCat(file.path, file.name);
1558 d.next = entry.next;
1565 void ::CloseDir(FileDesc file)
1567 EARDir d = (EARDir)file.dir;
1574 #if !defined(ECERE_NOARCHIVE) && !defined(ECERE_VANILLA)
1575 Archive ::OpenArchive(char * fileName, ArchiveOpenFlags flags)
1577 Archive result = null;
1578 EARArchive archive { writeAccess = flags.writeAccess };
1581 int try = flags.waitLock ? 10 : 0;
1582 for(; try >= 0; try--)
1584 // Check for existing Archive
1585 if((archive.f = fileName ? (flags.buffered ? FileOpenBuffered : FileOpen)(fileName, flags.writeAccess ? readWrite : read) : TempFile { openMode = readWrite } ))
1588 bool opened = false;
1589 uint archiveSize = 0;
1590 archive.f.Seek(-(int)sizeof(uint), end);
1591 archive.f.Read(&archiveSize, sizeof(uint), 1);
1592 archive.f.Seek(-(int)archiveSize, end);
1594 archive.archiveStart = archive.f.Tell();
1595 if(archive.f.Read(&header, sizeof(EARHeader), 1) == 1 &&
1596 !strncmp(header.recognition, earRecognition, sizeof(earRecognition)))
1601 archive.f.Seek(0, start);
1602 archive.archiveStart = archive.f.Tell();
1603 archiveSize = archive.f.GetSize();
1604 if(archive.f.Read(&header, sizeof(EARHeader), 1) == 1 &&
1605 !strncmp(header.recognition, earRecognition, sizeof(earRecognition)))
1611 // At this point we recognized the file as a valid eAR archive
1612 archive.rootDir = archive.archiveStart + sizeof(EARHeader);
1613 archive.totalSize = header.totalSize;
1615 archive.f.Seek(archive.rootDir, start);
1618 archive.freeBlocks.Add(FreeBlock { start = archive.rootDir + 2 * sizeof(uint), end = MAXDWORD });
1619 archive.SubtractUsedBlocks();
1623 archive.freeBlocks.Add(FreeBlock { start = archive.archiveStart + (archiveSize - sizeof(uint)), end = MAXDWORD });
1627 if(!flags.writeAccess)
1630 archive.f = FileOpen(fileName, readWrite);
1645 // This piece of code will create a new archive as a new file or at the footer
1646 // of an existing file.
1647 if(!result && flags.writeAccess)
1649 // If the file doesn't exist, create it
1652 archive.f = FileOpen(fileName, writeRead);
1654 archive.f = (flags.buffered ? FileOpenBuffered : FileOpen)(fileName, readWrite);
1664 archive.f.Seek(0, end);
1666 archive.archiveStart = archive.f.Tell();
1667 archive.freeBlocks.Add(FreeBlock { start = archive.archiveStart + sizeof(EARHeader), end = MAXDWORD });
1670 archive.f.Write(&header, sizeof(EARHeader), 1);
1672 uint first = 0, last = 0;
1673 archive.f.Write(&first, sizeof(first), 1);
1674 archive.f.Write(&last, sizeof(last), 1);
1677 archive.rootDir = 0;
1682 if(archive.f && flags.writeAccess && flags.exclusive && !archive.f.Lock(flags.exclusive ? exclusive : shared, 0, 0, flags.waitLock))
1691 // archive.f.handle = archive.f;
1697 bool ::QuerySize(char * archive, FileSize * size)
1699 bool result = false;
1701 File f = EAROpenArchive(archive, &header);
1704 *size = header.totalSize;