From eb67a724359cd55defe1a9a199f1df54905f054f Mon Sep 17 00:00:00 2001 From: Camilo Aguilar Date: Sun, 15 Nov 2015 00:27:55 -0500 Subject: [PATCH] Getting closer... --- reader.go | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/reader.go b/reader.go index 3759281..2464f53 100644 --- a/reader.go +++ b/reader.go @@ -87,47 +87,35 @@ func (r *Reader) Next() (os.FileInfo, error) { } // We only dequeue the directory when it does not contain more children - // or when it is empty and there is no children to go over. + // or when it is empty and there is no children to iterate over. item, err := r.queue.Peek() if err != nil { panic(err) } fi := item.(FileStat) - fmt.Println(r.sector) - // If there is no more entries in the current directory, dequeue it - // and move on the next directory in the queue. - if r.read > fi.ExtentLengthBE { - r.read = 0 - r.queue.Dequeue() - r.sector = 0 - return &fi, nil - } - if r.sector == 0 { r.sector = fi.ExtentLocationBE } var drecord FileStat var len byte - // We need this loop to skip parent and self directories: .. and . + // This loops exists so we can skip .. and . directories for { - // If we are at the end of the sector, move onto the next one. if (r.read % sectorSize) == 0 { - r.sector++ _, err := r.image.Seek(int64(r.sector*sectorSize), os.SEEK_SET) if err != nil { return nil, ErrCorruptedImage(err) } + r.sector++ } if len, err = r.unpackDRecord(&drecord); err != nil && err != io.EOF { return nil, ErrCorruptedImage(err) } - r.read += uint32(len) if err == io.EOF { - // directory record is empty, sector lost, move onto next sector. + // directory record is empty, sector wasted, move onto next sector. rsize := (sectorSize - (r.read % sectorSize)) buf := make([]byte, rsize) if err := binary.Read(r.image, binary.BigEndian, buf); err != nil { @@ -135,17 +123,27 @@ func (r *Reader) Next() (os.FileInfo, error) { } r.read += rsize } + r.read += uint32(len) if drecord.fileID != "\x00" && drecord.fileID != "\x01" { - if drecord.IsDir() { - r.queue.Enqueue(drecord) - } else { - drecord.image = r.image - } break } } + if drecord.IsDir() { + r.queue.Enqueue(drecord) + } else { + drecord.image = r.image + } + + // If there is no more entries in the current directory, dequeue it + // and move on the next directory in the queue. + if r.read > fi.ExtentLengthBE { + r.read = 0 + r.queue.Dequeue() + r.sector = 0 + } + return &drecord, nil }