summaryrefslogtreecommitdiff
path: root/lib/star.c
blob: e43ddf09bbbb07fff5999f393291e69c2b2f3ec2 (plain)
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
#include "tarfn.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
#include <errno.h>
#include <string.h>
#include <time.h>

static int
Read(void * userData, char * buffer, int length)
{
	/*
	 * If the status of the read function is < 0, it will be returned to
	 * the caller of TarExtractor().
	 */
	return read((int)userData, buffer, length);
}

static int
IOError(TarInfo * i)
{
	int	error = errno;	/* fflush() could cause errno to change */
	fflush(stdout);
	fprintf(stderr, "%s: %s\n", i->Name, strerror(error));

	/*
	 * The status returned by a coroutine of TarExtractor(), if it
	 * is non-zero, will be returned to the caller of TarExtractor().
	 */
	return -2;
}

static int
ExtractFile(TarInfo * i)
{
	/*
	 * If you don't want to extract the file, you must advance the tape
	 * by the file size rounded up to the next 512-byte boundary and
	 * return 0.
	 */

	int	fd = open(i->Name, O_CREAT|O_TRUNC|O_WRONLY, i->Mode & ~S_IFMT);
	char	buffer[512];
	size_t	size = i->Size;
	struct utimbuf t;
		
	if ( fd < 0 )
		return IOError(i);

	printf("File: %s\n", i->Name);

	while ( size > 0 ) {
		size_t	writeSize = size >= 512 ? 512 : size;

		if ( Read(i->UserData, buffer, 512) != 512 )
			return -1;	/* Something wrong with archive */
		if ( write(fd, buffer, writeSize) != writeSize )
			return IOError(i);	/* Write failure. */

		size -= writeSize;
	}
	/* fchown() and fchmod() are cheaper than chown() and chmod(). */
	fchown(fd, i->UserID, i->GroupID);
	fchmod(fd, i->Mode & ~S_IFMT);
	close(fd);
	t.actime = time(0);
	t.modtime = i->ModTime;
	utime(i->Name, &t);
	return 0;
}

static int
SetModes(TarInfo * i)
{
	struct utimbuf t;
	chown(i->Name, i->UserID, i->GroupID);
	chmod(i->Name, i->Mode & ~S_IFMT);
	t.actime = time(0);
	t.modtime = i->ModTime;
	utime(i->Name, &t);
        return 0;
}

static int
MakeDirectory(TarInfo * i)
{
	printf("Directory: %s\n", i->Name);
	if ( mkdir(i->Name, i->Mode & ~S_IFMT) != 0 ) {
		if ( errno == EEXIST ) {
			struct stat s;
			if ( stat(i->Name, &s) != 0 || !(s.st_mode & S_IFDIR) )
				return IOError(i);
		}
		else
			return IOError(i);
	}
	SetModes(i);
        return 0;
}

static int
MakeHardLink(TarInfo * i)
{
	printf("Hard Link: %s\n", i->Name);

	if ( link(i->LinkName, i->Name) != 0 )
		return IOError(i);
	SetModes(i);
        return 0;
}

static int
MakeSymbolicLink(TarInfo * i)
{
	printf("Symbolic Link: %s\n", i->Name);

	if ( symlink(i->LinkName, i->Name) != 0 )
		return -2;
	SetModes(i);
        return 0;
}

static int
MakeSpecialFile(TarInfo * i)
{
	printf("Special File: %s\n", i->Name);

	if ( mknod(i->Name, i->Mode, i->Device) != 0 )
		return -2;
	SetModes(i);
        return 0;
}

static const TarFunctions	functions = {
	Read,
	ExtractFile,
	MakeDirectory,
	MakeHardLink,
	MakeSymbolicLink,
	MakeSpecialFile
};

int
main(int argc, char * * argv)
{
	int	status = TarExtractor((void *)0, &functions);

	if ( status == -1 ) {
		fflush(stdout);
		fprintf(stderr, "Error in archive format.\n");
		return -1;
	}
	else
		return status;
}