]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
simple_buffer: Allow buffer size to change
[apt.git] / apt-pkg / contrib / fileutl.cc
index 0c5d76290ed5d9c26803cb3d7f9b0a5889f61ddd..54af2536921fe1102cf4b834941d713ec43e30c4 100644 (file)
@@ -920,16 +920,32 @@ bool ChangeOwnerAndPermissionOfFile(char const * const requester, char const * c
                                                                        /*}}}*/
 
 struct APT_HIDDEN simple_buffer {                                                      /*{{{*/
-   static constexpr size_t buffersize_max = 4096;
+   size_t buffersize_max = 0;
    unsigned long long bufferstart = 0;
    unsigned long long bufferend = 0;
-   char buffer[buffersize_max];
+   char *buffer = nullptr;
+
+   simple_buffer() {
+      reset(4096);
+   }
+   ~simple_buffer() {
+      delete buffer;
+   }
 
    const char *get() const { return buffer + bufferstart; }
    char *get() { return buffer + bufferstart; }
    bool empty() const { return bufferend <= bufferstart; }
    bool full() const { return bufferend == buffersize_max; }
    unsigned long long size() const { return bufferend-bufferstart; }
+   void reset(size_t size)
+   {
+      if (size > buffersize_max) {
+        delete[] buffer;
+        buffersize_max = size;
+        buffer = new char[size];
+      }
+      reset();
+   }
    void reset() { bufferend = bufferstart = 0; }
    ssize_t read(void *to, unsigned long long requested_size) APT_MUSTCHECK
    {
@@ -1218,16 +1234,16 @@ public:
    }
    virtual bool InternalFlush() override
    {
-      size_t written = 0;
-      char *data = writebuffer.get();
-      auto size = writebuffer.size();
-
-      while (written < size) {
-        auto written_this_time = wrapped->InternalWrite(data + written, size - written);
-        if (written_this_time < 0)
+      while (writebuffer.empty() == false) {
+        auto written = wrapped->InternalWrite(writebuffer.get(),
+                                              writebuffer.size());
+        // Ignore interrupted syscalls
+        if (written < 0 && errno == EINTR)
+           continue;
+        if (written < 0)
            return false;
 
-        written += written_this_time;
+        writebuffer.bufferstart += written;
       }
 
       writebuffer.reset();