Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmtar / handle.c
bloba86bbc5d88fa3c99e52fff628bcf33bfe9965804
1 /*
2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
5 **
6 ** handle.c - libtar code for initializing a TAR handle
7 **
8 ** Mark D. Roth <roth@uiuc.edu>
9 ** Campus Information Technologies and Educational Services
10 ** University of Illinois at Urbana-Champaign
13 #include <libtarint/internal.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <errno.h>
19 #ifdef HAVE_UNISTD_H
20 # include <unistd.h>
21 #endif
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 #endif
27 #ifdef HAVE_IO_H
28 #include <io.h>
29 //Yogi: hack. this should work on windows where there is no O_ACCMODE defined
30 #ifndef O_ACCMODE
31 # define O_ACCMODE 0x0003
32 #endif
33 #endif
35 const char libtar_version[] = PACKAGE_VERSION;
37 struct libtar_fd_file
39 int fd;
42 static struct libtar_fd_file libtar_fd_file_pointer;
44 static int libtar_open(void* call_data, const char* pathname, int flags, mode_t mode)
46 struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
47 lf->fd = open(pathname, flags, mode);
48 return lf->fd;
51 static int libtar_close(void* call_data)
53 struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
54 return close(lf->fd);
56 static ssize_t libtar_read(void* call_data, void* buf, size_t count)
58 struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
59 return (ssize_t)read(lf->fd, buf, (unsigned int)count);
61 static ssize_t libtar_write(void* call_data, const void* buf, size_t count)
63 struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
64 return (ssize_t) write(lf->fd, buf, (unsigned int)count);
67 static tartype_t default_type = { libtar_open, libtar_close, libtar_read, libtar_write, &libtar_fd_file_pointer };
69 static int
70 tar_init(TAR **t, char *pathname, tartype_t *type,
71 int oflags, int mode, int options)
73 (void)mode;
74 if ((oflags & O_ACCMODE) == O_RDWR)
76 errno = EINVAL;
77 return -1;
80 *t = (TAR *)calloc(1, sizeof(TAR));
81 if (*t == NULL)
82 return -1;
84 (*t)->pathname = pathname;
85 (*t)->options = options;
86 (*t)->type = (type ? type : &default_type);
87 (*t)->oflags = oflags;
89 if ((oflags & O_ACCMODE) == O_RDONLY)
90 (*t)->h = libtar_hash_new(256,
91 (libtar_hashfunc_t)path_hashfunc);
92 else
93 (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash);
94 if ((*t)->h == NULL)
96 free(*t);
97 return -1;
100 return 0;
104 /* open a new tarfile handle */
106 tar_open(TAR **t, char *pathname, tartype_t *type,
107 int oflags, int mode, int options)
109 int res;
110 if (tar_init(t, pathname, type, oflags, mode, options) == -1)
111 return -1;
113 if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT))
114 oflags |= O_EXCL;
116 #ifdef O_BINARY
117 oflags |= O_BINARY;
118 #endif
120 res = (*((*t)->type->openfunc))((*t)->type->call_data, pathname, oflags, mode);
121 if (res == -1)
123 free(*t);
124 return -1;
127 return 0;
132 tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
133 int oflags, int mode, int options)
135 if (tar_init(t, pathname, type, oflags, mode, options) == -1)
136 return -1;
138 if ( !type )
140 struct libtar_fd_file* lf = (struct libtar_fd_file*)((*t)->type->call_data);
141 lf->fd = fd;
143 return 0;
146 /* close tarfile handle */
148 tar_close(TAR *t)
150 int i;
152 i = (*(t->type->closefunc))(t->type->call_data);
154 if (t->h != NULL)
155 libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
156 ? free
157 : (libtar_freefunc_t)tar_dev_free));
158 free(t);
160 return i;