2 * To understand this code, see Rock Ridge Interchange Protocol
3 * standard 1.12 and System Use Sharing Protocol version 1.12
4 * (search for rrip112.ps and susp112.ps on the web).
6 * Even better, go read something else.
15 static long mode(Direc*, int);
16 static long nlink(Direc*);
17 static ulong suspdirflags(Direc*, int);
18 static ulong CputsuspCE(Cdimg *cd, ulong offset);
19 static int CputsuspER(Cdimg*, int);
20 static int CputsuspRR(Cdimg*, int, int);
21 static int CputsuspSP(Cdimg*, int);
22 /*static int CputsuspST(Cdimg*, int); */
23 static int Cputrripname(Cdimg*, char*, int, char*, int);
24 static int CputrripSL(Cdimg*, int, int, char*, int);
25 static int CputrripPX(Cdimg*, Direc*, int, int);
26 static int CputrripTF(Cdimg*, Direc*, int, int);
29 * Patch the length field in a CE record.
32 setcelen(Cdimg *cd, ulong woffset, ulong len)
43 * Rock Ridge data is put into little blockettes, which can be
44 * at most 256 bytes including a one-byte length. Some number
45 * of blockettes get packed together into a normal 2048-byte block.
46 * Blockettes cannot cross block boundaries.
48 * A Cbuf is a blockette buffer. Len contains
49 * the length of the buffer written so far, and we can
52 * We only have one active Cbuf at a time; cdimg.rrcontin is the byte
53 * offset of the beginning of that Cbuf.
55 * The blockette can be at most 255 bytes. The last 28
56 * will be (in the worst case) a CE record pointing at
57 * a new blockette. If we do write 255 bytes though,
58 * we'll try to pad it out to be even, and overflow.
59 * So the maximum is 254-28.
61 * Ceoffset contains the offset to be used with setcelen
62 * to patch the CE pointing at the Cbuf once we know how
65 typedef struct Cbuf Cbuf;
67 int len; /* written so far, of 254-28 */
74 return (254-28) - cp->len;
78 ensurespace(Cdimg *cd, int n, Cbuf *co, Cbuf *cn, int dowrite)
82 if(co->len+n <= 254-28) {
88 assert(co->len <= 254);
96 * the current blockette is full; update cd->rrcontin and then
97 * write a CE record to finish it. Unfortunately we need to
98 * figure out which block will be next before we write the CE.
100 end = Cwoffset(cd)+28;
103 * if we're in a continuation blockette, update rrcontin.
104 * also, write our length into the field of the CE record
107 if(cd->rrcontin+co->len == end) {
108 assert(cd->rrcontin != 0);
110 cd->rrcontin += co->len;
111 setcelen(cd, co->ceoffset, co->len);
116 * if the current continuation block can't fit another
117 * blockette, then start a new continuation block.
118 * rrcontin = 0 (mod Blocksize) means we just finished
119 * one, not that we've just started one.
121 if(cd->rrcontin%Blocksize == 0
122 || cd->rrcontin/Blocksize != (cd->rrcontin+256)/Blocksize) {
123 cd->rrcontin = cd->nextblock*Blocksize;
127 cn->ceoffset = CputsuspCE(cd, cd->rrcontin);
129 assert(Cwoffset(cd) == end);
132 Cwseek(cd, cd->rrcontin);
133 assert(cd->rrcontin != 0);
139 * Put down the name, but we might need to break it
140 * into chunks so that each chunk fits in 254-28-5 bytes.
143 * The new Plan 9 format uses strings of this form too,
144 * since they're already there.
147 Cputstring(Cdimg *cd, Cbuf *cp, Cbuf *cn, char *nm, char *p, int flags, int dowrite)
152 for(; p[0] != '\0'; p = q) {
153 cp = ensurespace(cd, 5+1, cp, cn, dowrite);
155 free = freespace(cp);
156 assert(5+1 <= free && free < 256);
158 strncpy(buf, p, free-5);
163 ensurespace(cd, 5+strlen(p), cp, nil, dowrite); /* nil: better not use this. */
164 Cputrripname(cd, nm, flags | (q[0] ? NMcontinue : 0), p, dowrite);
170 * Write a Rock Ridge SUSP set of records for a directory entry.
173 Cputsysuse(Cdimg *cd, Direc *d, int dot, int dowrite, int initlen)
175 char buf[256], buf0[256], *nextpath, *p, *path, *q;
176 int flags, free, m, what;
181 assert((initlen&1) == 0);
190 assert(dowrite==0 || Cwoffset(cd) == o+co.len-initlen);
193 if (dot == DTrootdot) {
194 m = CputsuspSP(cd, 0);
195 cp = ensurespace(cd, m, cp, &cn, dowrite);
196 CputsuspSP(cd, dowrite);
198 m = CputsuspER(cd, 0);
199 cp = ensurespace(cd, m, cp, &cn, dowrite);
200 CputsuspER(cd, dowrite);
204 * In a perfect world, we'd be able to omit the NM
205 * entries when our name was all lowercase and conformant,
206 * but OpenBSD insists on uppercasing (really, not lowercasing)
209 what = RR_PX | RR_TF | RR_NM;
210 if(d != nil && (d->mode & CHLINK))
213 m = CputsuspRR(cd, what, 0);
214 cp = ensurespace(cd, m, cp, &cn, dowrite);
215 CputsuspRR(cd, what, dowrite);
218 m = CputrripPX(cd, d, dot, 0);
219 cp = ensurespace(cd, m, cp, &cn, dowrite);
220 CputrripPX(cd, d, dot, dowrite);
226 else if(dot == DTdotdot)
231 flags = suspdirflags(d, dot);
232 assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);
233 cp = Cputstring(cd, cp, &cn, "NM", p, flags, dowrite);
237 * Put down the symbolic link. This is even more of a crock.
238 * Not only are the individual elements potentially split,
239 * but the whole path itself can be split across SL blocks.
240 * To keep the code simple as possible (really), we write
241 * only one element per SL block, wasting 6 bytes per element.
244 for(path=d->symlink; path[0] != '\0'; path=nextpath) {
245 /* break off one component */
246 if((nextpath = strchr(path, '/')) == nil)
247 nextpath = path+strlen(path);
248 strncpy(buf0, path, nextpath-path);
249 buf0[nextpath-path] = '\0';
250 if(nextpath[0] == '/')
254 /* write the name, perhaps broken into pieces */
255 if(strcmp(p, "") == 0)
257 else if(strcmp(p, ".") == 0)
259 else if(strcmp(p, "..") == 0)
264 /* the do-while handles the empty string properly */
266 /* must have room for at least 1 byte of name */
267 cp = ensurespace(cd, 7+1, cp, &cn, dowrite);
269 free = freespace(cp);
270 assert(7+1 <= free && free < 256);
272 strncpy(buf, p, free-7);
277 /* nil: better not need to expand */
278 assert(7+strlen(p) <= free);
279 ensurespace(cd, 7+strlen(p), cp, nil, dowrite);
280 CputrripSL(cd, nextpath[0], flags | (q[0] ? NMcontinue : 0), p, dowrite);
282 } while(p[0] != '\0');
286 assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);
289 m = CputrripTF(cd, d, TFcreation|TFmodify|TFaccess|TFattributes, 0);
290 cp = ensurespace(cd, m, cp, &cn, dowrite);
291 CputrripTF(cd, d, TFcreation|TFmodify|TFaccess|TFattributes, dowrite);
293 assert(dowrite==0 || cp != &co || Cwoffset(cd) == o+co.len-initlen);
295 if(cp == &cn && dowrite) {
296 /* seek out of continuation, but mark our place */
297 cd->rrcontin = Cwoffset(cd);
298 setcelen(cd, cn.ceoffset, cn.len);
299 Cwseek(cd, o+co.len-initlen);
309 if(Cwoffset(cd) != o+co.len-initlen)
310 fprint(2, "offset %lud o+co.len-initlen %lud\n", Cwoffset(cd), o+co.len-initlen);
311 assert(Cwoffset(cd) == o+co.len-initlen);
313 assert(Cwoffset(cd) == o);
315 assert(co.len <= 255);
316 return co.len - initlen;
319 static char SUSPrrip[10] = "RRIP_1991A";
320 static char SUSPdesc[84] = "RRIP <more garbage here>";
321 static char SUSPsrc[135] = "RRIP <more garbage here>";
324 CputsuspCE(Cdimg *cd, ulong offset)
328 chat("writing SUSP CE record pointing to %ld, %ld\n", offset/Blocksize, offset%Blocksize);
334 Cputn(cd, offset/Blocksize, 4);
335 Cputn(cd, offset%Blocksize, 4);
338 assert(Cwoffset(cd) == o+28);
344 CputsuspER(Cdimg *cd, int dowrite)
349 chat("writing SUSP ER record\n");
350 Cputc(cd, 'E'); /* ER field marker */
352 Cputc(cd, 26); /* Length */
353 Cputc(cd, 1); /* Version */
354 Cputc(cd, 10); /* LEN_ID */
355 Cputc(cd, 4); /* LEN_DESC */
356 Cputc(cd, 4); /* LEN_SRC */
357 Cputc(cd, 1); /* EXT_VER */
358 Cputs(cd, SUSPrrip, 10); /* EXT_ID */
359 Cputs(cd, SUSPdesc, 4); /* EXT_DESC */
360 Cputs(cd, SUSPsrc, 4); /* EXT_SRC */
366 CputsuspRR(Cdimg *cd, int what, int dowrite)
371 Cputc(cd, 'R'); /* RR field marker */
373 Cputc(cd, 5); /* Length */
374 Cputc(cd, 1); /* Version number */
375 Cputc(cd, what); /* Flags */
381 CputsuspSP(Cdimg *cd, int dowrite)
386 chat("writing SUSP SP record\n");
387 Cputc(cd, 'S'); /* SP field marker */
389 Cputc(cd, 7); /* Length */
390 Cputc(cd, 1); /* Version */
391 Cputc(cd, 0xBE); /* Magic */
401 CputsuspST(Cdimg *cd, int dowrite)
406 Cputc(cd, 'S'); /* ST field marker */
408 Cputc(cd, 4); /* Length */
409 Cputc(cd, 1); /* Version */
416 suspdirflags(Direc *d, int dot)
442 Cputrripname(Cdimg *cd, char *nm, int flags, char *name, int dowrite)
448 Cputc(cd, nm[0]); /* NM field marker */
450 Cputc(cd, l+5); /* Length */
451 Cputc(cd, 1); /* Version */
452 Cputc(cd, flags); /* Flags */
453 Cputs(cd, name, l); /* Alternate name */
459 CputrripSL(Cdimg *cd, int contin, int flags, char *name, int dowrite)
469 Cputc(cd, contin ? 1 : 0);
478 CputrripPX(Cdimg *cd, Direc *d, int dot, int dowrite)
483 Cputc(cd, 'P'); /* PX field marker */
485 Cputc(cd, 36); /* Length */
486 Cputc(cd, 1); /* Version */
488 Cputn(cd, mode(d, dot), 4); /* POSIX File mode */
489 Cputn(cd, nlink(d), 4); /* POSIX st_nlink */
490 Cputn(cd, d?d->uidno:0, 4); /* POSIX st_uid */
491 Cputn(cd, d?d->gidno:0, 4); /* POSIX st_gid */
498 CputrripTF(Cdimg *cd, Direc *d, int type, int dowrite)
503 assert(!(type & TFlongform));
512 Cputc(cd, 'T'); /* TF field marker */
514 Cputc(cd, 5+7*length); /* Length */
515 Cputc(cd, 1); /* Version */
516 Cputc(cd, type); /* Flags (types) */
518 if (type & TFcreation)
519 Cputdate(cd, d?d->ctime:0);
521 Cputdate(cd, d?d->mtime:0);
523 Cputdate(cd, d?d->atime:0);
524 if (type & TFattributes)
525 Cputdate(cd, d?d->ctime:0);
527 /* if (type & TFbackup) */
528 /* Cputdate(cd, 0); */
529 /* if (type & TFexpiration) */
530 /* Cputdate(cd, 0); */
531 /* if (type & TFeffective) */
532 /* Cputdate(cd, 0); */
538 #define NONPXMODES (DMDIR & DMAPPEND & DMEXCL & DMMOUNT)
539 #define POSIXMODEMASK (0177777)
541 #define S_IFMT (0170000)
544 #define S_IFDIR (0040000)
547 #define S_IFREG (0100000)
550 #define S_IFLNK (0120000)
553 #define ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
555 #define S_ISDIR(mode) ISTYPE(mode, S_IFDIR)
558 #define S_ISREG(mode) ISTYPE(mode, S_IREG)
561 #define S_ISLNK(mode) ISTYPE(mode, S_ILNK)
566 mode(Direc *d, int dot)
573 if ((dot != DTroot) && (dot != DTrootdot)) {
574 mode = (d->mode & ~(NONPXMODES));
577 else if (d->mode & CHLINK)
582 mode = S_IFDIR | (0755);
584 mode &= POSIXMODEMASK;
586 /* Botch: not all POSIX types supported yet */
587 assert(mode & (S_IFDIR|S_IFREG));
589 chat("writing PX record mode field %ulo with dot %d and name \"%s\"\n", mode, dot, d->name);
595 nlink(Direc *d) /* Trump up the nlink field for POSIX compliance */
604 if (d->mode & DMDIR) /* One for "." and one more for ".." */
607 for(i=0; i<d->nchild; i++)
608 if (d->child[i].mode & DMDIR)