File: MultiOutStream.h

package info (click to toggle)
lzma 24.08%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,472 kB
  • sloc: cpp: 78,089; ansic: 26,903; asm: 4,195; cs: 3,846; java: 3,077; makefile: 1,010; sh: 74
file content (160 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download | duplicates (8)
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
// MultiOutStream.h

#ifndef ZIP7_INC_MULTI_OUT_STREAM_H
#define ZIP7_INC_MULTI_OUT_STREAM_H

#include "FileStreams.h"

Z7_CLASS_IMP_COM_2(
  CMultiOutStream
  , IOutStream
  , IStreamSetRestriction
)
  Z7_IFACE_COM7_IMP(ISequentialOutStream)

  Z7_CLASS_NO_COPY(CMultiOutStream)

  struct CVolStream
  {
    COutFileStream *StreamSpec;
    CMyComPtr<IOutStream> Stream;
    UInt64 Start;   // start pos of current Stream in global stream
    UInt64 Pos;     // pos in current Stream
    UInt64 RealSize;
    int Next;       // next older
    int Prev;       // prev newer
    AString Postfix;

    HRESULT SetSize2(UInt64 size)
    {
      const HRESULT res = Stream->SetSize(size);
      if (res == SZ_OK)
        RealSize = size;
      return res;
    }
  };

  unsigned _streamIndex; // (_streamIndex >= Stream.Size()) is allowed in some internal code
  UInt64 _offsetPos;     // offset relative to Streams[_streamIndex] volume. (_offsetPos >= volSize is allowed)
  UInt64 _absPos;
  UInt64 _length;        // virtual Length
  UInt64 _absLimit;
  
  CObjectVector<CVolStream> Streams;
  CRecordVector<UInt64> Sizes;
  
  UInt64 _restrict_Begin;
  UInt64 _restrict_End;
  UInt64 _restrict_Global;

  unsigned NumOpenFiles_AllowedMax;

  // ----- Double Linked List -----

  unsigned NumListItems;
  int Head; // newest
  int Tail; // oldest

  void InitLinkedList()
  {
    Head = -1;
    Tail = -1;
    NumListItems = 0;
  }

  void InsertToLinkedList(unsigned index)
  {
    {
      CVolStream &node = Streams[index];
      node.Next = Head;
      node.Prev = -1;
    }
    if (Head != -1)
      Streams[(unsigned)Head].Prev = (int)index;
    else
    {
      // if (Tail != -1) throw 1;
      Tail = (int)index;
    }
    Head = (int)index;
    NumListItems++;
  }

  void RemoveFromLinkedList(unsigned index)
  {
    CVolStream &s = Streams[index];
    if (s.Next != -1) Streams[(unsigned)s.Next].Prev = s.Prev; else Tail = s.Prev;
    if (s.Prev != -1) Streams[(unsigned)s.Prev].Next = s.Next; else Head = s.Next;
    s.Next = -1; // optional
    s.Prev = -1; // optional
    NumListItems--;
  }

  /*
  void Delete_LastStream_Records()
  {
    if (Streams.Back().Stream)
      RemoveFromLinkedList(Streams.Size() - 1);
    Streams.DeleteBack();
  }
  */

  UInt64 GetVolSize_for_Stream(unsigned i) const
  {
    const unsigned last = Sizes.Size() - 1;
    return Sizes[i < last ? i : last];
  }
  UInt64 GetGlobalOffset_for_NewStream() const
  {
    return Streams.Size() == 0 ? 0:
        Streams.Back().Start +
        GetVolSize_for_Stream(Streams.Size() - 1);
  }
  unsigned GetStreamIndex_for_Offset(UInt64 offset, UInt64 &relOffset) const;
  bool IsRestricted(const CVolStream &s) const;
  bool IsRestricted_Empty(const CVolStream &s) const
  {
    // (s) must be stream that has (VolSize == 0).
    // we treat empty stream as restricted, if next byte is restricted.
    if (s.Start < _restrict_Global)
      return true;
    return
         (_restrict_Begin != _restrict_End)
      && (_restrict_Begin <= s.Start)
      && (_restrict_Begin == s.Start || _restrict_End > s.Start);
  }
  // bool IsRestricted_for_Close(unsigned index) const;
  FString GetFilePath(unsigned index);
  
  HRESULT CloseStream(unsigned index);
  HRESULT CloseStream_and_DeleteFile(unsigned index);
  HRESULT CloseStream_and_FinalRename(unsigned index);
  
  HRESULT PrepareToOpenNew();
  HRESULT CreateNewStream(UInt64 newSize);
  HRESULT CreateStreams_If_Required(unsigned streamIndex);
  HRESULT ReOpenStream(unsigned streamIndex);
  HRESULT OptReOpen_and_SetSize(unsigned index, UInt64 size);

  HRESULT Normalize_finalMode(bool finalMode);
public:
  FString Prefix;
  CFiTime MTime;
  bool MTime_Defined;
  bool FinalVol_WasReopen;
  bool NeedDelete;

  CMultiOutStream() {}
  ~CMultiOutStream();
  void Init(const CRecordVector<UInt64> &sizes);
  bool SetMTime_Final(const CFiTime &mTime);
  UInt64 GetSize() const { return _length; }
  /* it makes final flushing, closes open files and renames to final name if required
     but it still keeps Streams array of all closed files.
     So we still can delete all files later, if required */
  HRESULT FinalFlush_and_CloseFiles(unsigned &numTotalVolumesRes);
  // Destruct object without exceptions
  HRESULT Destruct();
};

#endif