1 /* @(#)desktop.c	1.10 09/07/09 joerg, Copyright 1997, 1998, 1999, 2000 James Pearson, Copyright 2000-2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)desktop.c	1.10 09/07/09 joerg, Copyright 1997, 1998, 1999, 2000 James Pearson, Copyright 2000-2009 J. Schilling";
6 #endif
7 /*
8  *      Copyright (c) 1997, 1998, 1999, 2000 James Pearson
9  *	Copyright (c) 2000-2009 J. Schilling
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 /*
27  *	make_desktop: create "Desktop DB" and "Desktop DF" files.
28  *
29  *	These are set up to prevent the Mac "rebuilding the desktop"
30  *	when the CD is inserted ???
31  *
32  *	I don't know if these files should be populated, but I've just
33  *	created these files in their initial states:
34  *
35  *	Desktop DB:	Initial size == volume's clump size
36  *			first block contents found by using od ...
37  *			rest of file seems to be padding
38  *			No resource fork
39  *
40  *	Desktop DF:	Empty
41  *
42  *	If the files already exist, then set correct type/creator/flags
43  *
44  *	James Pearson 11/8/97
45  *	Adapted from mkhfs routines for mkhybrid
46  */
47 
48 #ifdef APPLE_HFS_HYB
49 
50 #include "mkisofs.h"
51 
52 #define	DB	"Desktop DB"
53 #define	DBFC	"DMGR"
54 #define	DBT	"BTFL"
55 
56 #define	DF	"Desktop DF"
57 #define	DFT	"DTFL"
58 
59 /*
60  * from "data.h" - libhfs routines
61  */
62 extern	void d_putw	__PR((unsigned char *, short));
63 extern	void d_putl	__PR((unsigned char *, long));
64 
65 int	make_desktop	__PR((hfsvol *vol, int end));
66 
67 
68 extern	hce_mem *hce;	/* libhfs/mkisofs extras */
69 
70 int
make_desktop(vol,end)71 make_desktop(vol, end)
72 	hfsvol	*vol;
73 	int	end;
74 {
75 	hfsfile		*hfp;			/* Mac file */
76 	hfsdirent	ent;			/* Mac finderinfo */
77 	unsigned long	clps;			/* clump size */
78 	unsigned short	blks;			/* blocks in a clump */
79 	unsigned char	*blk;			/* user data */
80 
81 	/*
82 	 * set up default directory entries - not all these fields are needed,
83 	 * but we'll set them up anyway ...
84 	 * First do a memset because there was a report about randomly
85 	 * changing Desktop DB/DF entries...
86 	 */
87 	memset(&ent, 0, sizeof (hfsdirent));	/* First clear all ... */
88 	ent.u.file.rsize = 0;			/* resource size == 0 */
89 	strcpy(ent.u.file.creator, DBFC);	/* creator */
90 	strcpy(ent.u.file.type, DBT);		/* type */
91 	ent.crdate = ent.mddate = time(0);	/* date is now */
92 	ent.fdflags = HFS_FNDR_ISINVISIBLE;	/* invisible files */
93 
94 	/*
95 	 * clear the DB file
96 	 */
97 	blk = hce->hfs_ce + hce->hfs_ce_size * HFS_BLOCKSZ;
98 	blks = hce->hfs_dt_size;
99 	clps = blks * HFS_BLOCKSZ;
100 
101 	memset(blk, 0, clps);
102 
103 	/*
104 	 * create "Desktop DB" (if it doesn't exist)
105 	 */
106 	if (hfs_create(vol, DB, ent.u.file.type, ent.u.file.creator) == 0) {
107 		/*
108 		 * DB file size from hce_mem info
109 		 * set up "Desktop DB" data - following found by od'ing the
110 		 * "Desktop DB" file
111 		 */
112 		d_putw(blk + 8, 0x100);
113 		d_putw(blk + 10, 0x3);
114 
115 		d_putw(blk + 32, 0x200);
116 		d_putw(blk + 34, 0x25);
117 
118 		d_putl(blk + 36, blks);
119 		d_putl(blk + 40, blks - 1);
120 
121 		d_putl(blk + 46, clps);
122 		d_putw(blk + 50, 0xff);
123 
124 		d_putw(blk + 120, 0x20a);
125 		d_putw(blk + 122, 0x100);
126 
127 		d_putw(blk + 248, 0x8000);
128 
129 		d_putl(blk + 504, 0x1f800f8);
130 		d_putl(blk + 508, 0x78000e);
131 
132 		/* entries for "Desktop DB" */
133 		ent.u.file.dsize = clps;	/* size = clump size */
134 
135 		/* open file */
136 		if ((hfp = hfs_open(vol, DB)) == 0)
137 			perr(hfs_error);
138 
139 		/* "write" file */
140 		write_fork(hfp, clps);
141 
142 		/* set DB file attributes */
143 		if (hfs_fsetattr(hfp, &ent) < 0)
144 			perr(hfs_error);
145 
146 		/* find the real start of the file */
147 		end += hce->hfs_ce_size;
148 
149 		/* close DB file */
150 		if (hfs_close(hfp, end, 0) < 0)
151 			perr(hfs_error);
152 	} else {
153 		/*
154 		 * if it already exists, then make sure it has the correct
155 		 * type/creator and flags
156 		 */
157 		if (hfs_setattr(vol, DB, &ent) < 0)
158 			perr(hfs_error);
159 	}
160 
161 	/* setup "Desktop DF" file as an empty file */
162 	strcpy(ent.u.file.type, DFT);		/* type */
163 	ent.u.file.dsize = 0;			/* empty */
164 
165 	/* create DF file (if it doesn't exist) - no need to open it */
166 	hfs_create(vol, DF, ent.u.file.type, ent.u.file.creator);
167 
168 	/* set DB file attributes */
169 	if (hfs_setattr(vol, DF, &ent) < 0)
170 		perr(hfs_error);
171 
172 	return (0);
173 }
174 
175 #endif	/* APPLE_HFS_HYB */
176