about summary refs log tree commit diff
path: root/src/libnix/archive.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-10-22T10·48+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-10-22T10·48+0000
commit4a8948b7a60e751dd809f279f1baa434ea09a4d3 (patch)
tree3c8475fe2280f2e48654f287c46edc580bf4382c /src/libnix/archive.cc
parentc62433751d5fce8c25a802989c50fee993f39c2d (diff)
* Some wrapper classes to ensure that file descriptors / directory
  handles are closed when they go out of scope.

Diffstat (limited to 'src/libnix/archive.cc')
-rw-r--r--src/libnix/archive.cc12
1 files changed, 3 insertions, 9 deletions
diff --git a/src/libnix/archive.cc b/src/libnix/archive.cc
index 9039ad7db43e..ed57df4c9f7b 100644
--- a/src/libnix/archive.cc
+++ b/src/libnix/archive.cc
@@ -51,7 +51,7 @@ static void dump(const string & path, DumpSink & sink);
 
 static void dumpEntries(const Path & path, DumpSink & sink)
 {
-    DIR * dir = opendir(path.c_str());
+    AutoCloseDir dir = opendir(path.c_str());
     if (!dir) throw SysError("opening directory " + path);
 
     vector<string> names;
@@ -77,8 +77,6 @@ static void dumpEntries(const Path & path, DumpSink & sink)
         dump(path + "/" + *it, sink);
         writeString(")", sink);
     }
-    
-    closedir(dir); /* !!! close on exception */
 }
 
 
@@ -88,7 +86,7 @@ static void dumpContents(const Path & path, unsigned int size,
     writeString("contents", sink);
     writeInt(size, sink);
 
-    int fd = open(path.c_str(), O_RDONLY);
+    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
     if (fd == -1) throw SysError(format("opening file `%1%'") % path);
     
     unsigned char buf[65536];
@@ -105,8 +103,6 @@ static void dumpContents(const Path & path, unsigned int size,
         throw SysError("file changed while reading it: " + path);
 
     writePadding(size, sink);
-
-    close(fd); /* !!! close on exception */
 }
 
 
@@ -262,7 +258,7 @@ static void restore(const Path & path, RestoreSource & source)
     if (s != "(") throw badArchive("expected open tag");
 
     enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
-    int fd = -1; /* !!! close on exception */
+    AutoCloseFD fd;
 
     while (1) {
         s = readString(source);
@@ -326,8 +322,6 @@ static void restore(const Path & path, RestoreSource & source)
         }
         
     }
-
-    if (fd != -1) close(fd);
 }