File: mount_z.c

package info (click to toggle)
jazip 0.34-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 452 kB
  • ctags: 154
  • sloc: ansic: 2,632; perl: 343; makefile: 91; sh: 32
file content (198 lines) | stat: -rw-r--r-- 5,143 bytes parent folder | download | duplicates (3)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * jaZip for Linux  (c) 1996  Jarrod A. Smith
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#include <mntent.h>
/* Use sys/mount.h instead of now deprecated method of using kernel header
   linux/fs.h - Matthew Wilcox <willy@debian.org> */
#include <sys/mount.h>
#include <sys/vfs.h>
#include <X11/forms.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "jazip.h"

/* Copy the definition of struct partition from the kernel header instead
   of including kernel header linux/genhd.h
   Daniel Schepler <schepler@math.berkeley.edu> */
struct partition {
	unsigned char boot_ind;
	unsigned char head;
	unsigned char sector;
	unsigned char cyl;
	unsigned char sys_ind;
	unsigned char end_head;
	unsigned char end_sector;
	unsigned char end_cyl;
	unsigned int start_sect;
	unsigned int nr_sects;
} __attribute__((packed));

#define MAXIMUM_PARTS   60
#define SECTOR_SIZE  512
#define offset(b, n) ((struct partition *)((b) + 0x1be + \
		(n) * sizeof(struct partition)))

char  buffer[SECTOR_SIZE];    /* first four partitions */
char  *buffers[MAXIMUM_PARTS] = {buffer, buffer, buffer, buffer};
uint offsets[MAXIMUM_PARTS] = {0, 0, 0, 0};

/* partitions */
struct   partition *part_table[MAXIMUM_PARTS] = {offset(buffer, 0), offset(buffer, 1),
	offset(buffer, 2), offset(buffer, 3)};

struct systypes {
	unsigned char index;
	char *name;
	char *fstype;
	} sys_types[] = {
		{0, "Empty", "unknown"},
		{1, "DOS 12-bit FAT", "msdos"},
		{4, "DOS 16-bit <32M", "vfat"},
		{5, "Extended", "unknown"},
		{6, "DOS 16-bit >=32M", "vfat"},
		{0xb, "Win95 FAT32", "fat32"},
		{0x81, "Linux/MINIX", "ext"},
		{0x82, "Linux swap", "swap"},
		{0x83, "Linux native", "ext2"},
  };

char *partition_type(unsigned char type)
{
	int high = sizeof(sys_types) / sizeof(struct systypes),
		low = 0, mid;
	uint tmp;

	while (high >= low) {
		mid = (high + low) >> 1;
		if ((tmp = sys_types[mid].index) == type)
			return sys_types[mid].fstype;
		else if (tmp < type)
			low = mid + 1;
		else high = mid - 1;
	}
	return NULL;
}

int z_mount( char *dev )
{
	FILE		*jazipconf, *mtab;
	struct	mntent	*mp;
	struct	stat		s;
	struct	statfs	fs;
	struct	partition *p;
	int		mntopt=0, tmp, i;
	int		fd, partitions = 4;
	char		*type, perm_stat[32], pname[10];

	if (is_mounted( dev )) {
		sprintf (mesg, "%s is already mounted as %s on %s.", mnt_fsname, mnt_type, mnt_dir);
		return (0);
	}

/*Read the partition table from the disk and find a filesystem to mount*/

	seteuid(0);

	if ((fd = open (dev, O_RDONLY)) < 0){
		sprintf (mesg, "Couldn't open %s.  Is there a disk in the drive?", dev);
		seteuid(getuid());
		return (0);
	}

	if (SECTOR_SIZE != read(fd, buffer, SECTOR_SIZE)){
		sprintf (mesg, "Error reading the partition table.");
		close (fd);
		seteuid(getuid());
		return (0);
	} else close (fd);

	seteuid(getuid());

/* Get the correct line from IOMEGA_FSTAB */
	if (!(jazipconf = setmntent (IOMEGA_FSTAB, "r"))) {
		sprintf (mesg, "Unable to read %s: %s.", dev, strerror (errno));
		return (0);
	}
	for(i=1; i<=conf; i++) mp = getmntent (jazipconf);
	endmntent (jazipconf);

/* Get the first partition only */
	for (i=0; i<partitions; i++)
		if ((p = part_table[i])->sys_ind)
			break;
	sprintf( mp->mnt_fsname, "%7s%-1d", dev, i+1 );
	mp->mnt_type = partition_type( p->sys_ind );
	strcpy( mp->mnt_opts, "defaults");
	mp->mnt_freq = 0;
	mp->mnt_passno = 0;

/* Check to make sure the mount point exists. */
	if (tmp=stat (mp->mnt_dir, &s)) {
		sprintf (mesg, "Couldn't find mount point %s specified in %s.",
			mp->mnt_dir, IOMEGA_FSTAB);
		return( 0 );
	}

	if (dostatus (dev) != 0) {
		strcpy (perm_stat, "write-protected ");
		mntopt=MS_NOSUID|MS_RDONLY;
	} else {
		strcpy (perm_stat, "");
		mntopt=MS_NOSUID&~MS_RDONLY;
	}

	if (!lock_mtab()) {
		sprintf (mesg, "Unable to create lock-file.  Mount denied.");
		return (0);
	}

	seteuid(0);
  	if (!mount (mp->mnt_fsname,mp->mnt_dir,mp->mnt_type,0xc0ed0000|mntopt,"" )) {
  		sprintf (mesg, "Mounted %s%s disk on %s", perm_stat, mp->mnt_type, mp->mnt_dir);
		if (!(mtab = setmntent(MOUNTED, "a"))) {
			printf ("ERROR! Couldn't write entry to %s.", MOUNTED);
			endmntent (mtab);
			unlink (LOCK_FILE);
			exit (-1);
		} else {
			addmntent (mtab, mp);
			endmntent (mtab);
			unlink (LOCK_FILE);
			seteuid(getuid());
			return (1);
		}
	} else {
			sprintf (mesg, "Error trying to mount %s : %s", dev, strerror (errno));
			unlink (LOCK_FILE);
			seteuid(getuid());
			return (0);
	}
	seteuid(getuid());
}

int lock_mtab()
{
	int fd;

	seteuid(0);
	if ((fd = open (LOCK_FILE, O_WRONLY | O_CREAT | O_EXCL, 0744)) < 0){
		seteuid(getuid());
 		return 0;
	}else{
		close (fd);
		seteuid(getuid());
		return (1);
	}
}