2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
6 ** handle.c - libtar code for initializing a TAR handle
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>
29 //Yogi: hack. this should work on windows where there is no O_ACCMODE defined
31 # define O_ACCMODE 0x0003
35 const char libtar_version
[] = PACKAGE_VERSION
;
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
);
51 static int libtar_close(void* call_data
)
53 struct libtar_fd_file
* lf
= (struct libtar_fd_file
*)call_data
;
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
};
70 tar_init(TAR
**t
, char *pathname
, tartype_t
*type
,
71 int oflags
, int mode
, int options
)
74 if ((oflags
& O_ACCMODE
) == O_RDWR
)
80 *t
= (TAR
*)calloc(1, sizeof(TAR
));
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
);
93 (*t
)->h
= libtar_hash_new(16, (libtar_hashfunc_t
)dev_hash
);
104 /* open a new tarfile handle */
106 tar_open(TAR
**t
, char *pathname
, tartype_t
*type
,
107 int oflags
, int mode
, int options
)
110 if (tar_init(t
, pathname
, type
, oflags
, mode
, options
) == -1)
113 if ((options
& TAR_NOOVERWRITE
) && (oflags
& O_CREAT
))
120 res
= (*((*t
)->type
->openfunc
))((*t
)->type
->call_data
, pathname
, oflags
, mode
);
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)
140 struct libtar_fd_file
* lf
= (struct libtar_fd_file
*)((*t
)->type
->call_data
);
146 /* close tarfile handle */
152 i
= (*(t
->type
->closefunc
))(t
->type
->call_data
);
155 libtar_hash_free(t
->h
, ((t
->oflags
& O_ACCMODE
) == O_RDONLY
157 : (libtar_freefunc_t
)tar_dev_free
));