changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: webcookies: avoid rereading and rewriting the cookie jar file all the time, dont return deleted cookies on search

changeset 4212: 879ef1aac380
parent 4211: baf2e50c3353
child 4213: 58775c177bda
author: cinap_lenrek@felloff.net
date: Fri, 16 Jan 2015 04:04:55 +0100
files: sys/src/cmd/webcookies.c
description: webcookies: avoid rereading and rewriting the cookie jar file all the time, dont return deleted cookies on search

- rewrite when jar->dirty != 0 (caller modified the in memory jar)
- reread when the jar->qid != stat(jar->file)->qid (on disk file changed)
- ignore deleted cookies in cookiesearch()
     1.1--- a/sys/src/cmd/webcookies.c
     1.2+++ b/sys/src/cmd/webcookies.c
     1.3@@ -349,24 +349,30 @@ expirejar(Jar *jar, int exiting)
     1.4 int
     1.5 syncjar(Jar *jar)
     1.6 {
     1.7-	int i, fd;
     1.8+	int i, fd, doread, dowrite;
     1.9 	char *line;
    1.10+	Biobuf *b;
    1.11 	Dir *d;
    1.12-	Biobuf *b;
    1.13 	Qid q;
    1.14 
    1.15 	if(jar->file==nil)
    1.16 		return 0;
    1.17 
    1.18-	memset(&q, 0, sizeof q);
    1.19-	if((d = dirstat(jar->file)) != nil){
    1.20-		q = d->qid;
    1.21-		if(d->qid.path != jar->qid.path || d->qid.vers != jar->qid.vers)
    1.22-			jar->dirty = 1;
    1.23+	doread = 0;
    1.24+	dowrite = jar->dirty;
    1.25+
    1.26+	q = jar->qid;
    1.27+	if((d = dirstat(jar->file)) == nil)
    1.28+		dowrite = 1;
    1.29+	else {
    1.30+		if(q.path != d->qid.path || q.vers != d->qid.vers){
    1.31+			q = d->qid;
    1.32+			doread = 1;
    1.33+		}
    1.34 		free(d);
    1.35 	}
    1.36 
    1.37-	if(jar->dirty == 0)
    1.38+	if(!doread && !dowrite)
    1.39 		return 0;
    1.40 
    1.41 	fd = -1;
    1.42@@ -384,52 +390,59 @@ syncjar(Jar *jar)
    1.43 		return -1;
    1.44 	}
    1.45 
    1.46-	for(i=0; i<jar->nc; i++)	/* mark is cleared by addcookie */
    1.47-		jar->c[i].mark = jar->c[i].ondisk;
    1.48+	if(doread){
    1.49+		for(i=0; i<jar->nc; i++)	/* mark is cleared by addcookie */
    1.50+			jar->c[i].mark = jar->c[i].ondisk;
    1.51 
    1.52-	if((b = Bopen(jar->file, OREAD)) == nil){
    1.53-		if(debug)
    1.54-			fprint(2, "Bopen %s: %r", jar->file);
    1.55-		werrstr("cannot read cookie file %s: %r", jar->file);
    1.56-		close(fd);
    1.57-		return -1;
    1.58+		if((b = Bopen(jar->file, OREAD)) == nil){
    1.59+			if(debug)
    1.60+				fprint(2, "Bopen %s: %r", jar->file);
    1.61+			werrstr("cannot read cookie file %s: %r", jar->file);
    1.62+			close(fd);
    1.63+			return -1;
    1.64+		}
    1.65+		for(; (line = Brdstr(b, '\n', 1)) != nil; free(line)){
    1.66+			if(*line == '#')
    1.67+				continue;
    1.68+			addtojar(jar, line, 1);
    1.69+		}
    1.70+		Bterm(b);
    1.71+
    1.72+		for(i=0; i<jar->nc; i++)
    1.73+			if(jar->c[i].mark)
    1.74+				delcookie(jar, &jar->c[i]);
    1.75 	}
    1.76-	for(; (line = Brdstr(b, '\n', 1)) != nil; free(line)){
    1.77-		if(*line == '#')
    1.78-			continue;
    1.79-		addtojar(jar, line, 1);
    1.80-	}
    1.81-	Bterm(b);
    1.82-
    1.83-	for(i=0; i<jar->nc; i++)
    1.84-		if(jar->c[i].mark)
    1.85-			delcookie(jar, &jar->c[i]);
    1.86 
    1.87 	purgejar(jar);
    1.88 
    1.89-	b = Bopen(jar->file, OTRUNC|OWRITE);
    1.90-	if(b == nil){
    1.91-		if(debug)
    1.92-			fprint(2, "Bopen write %s: %r", jar->file);
    1.93-		close(fd);
    1.94-		return -1;
    1.95+	if(dowrite){
    1.96+		b = Bopen(jar->file, OTRUNC|OWRITE);
    1.97+		if(b == nil){
    1.98+			if(debug)
    1.99+				fprint(2, "Bopen write %s: %r", jar->file);
   1.100+			close(fd);
   1.101+			return -1;
   1.102+		}
   1.103+		Bprint(b, "# webcookies cookie jar\n");
   1.104+		Bprint(b, "# comments and non-standard fields will be lost\n");
   1.105+		for(i=0; i<jar->nc; i++){
   1.106+			if(jar->c[i].expire == ~0)
   1.107+				continue;
   1.108+			Bprint(b, "%K\n", &jar->c[i]);
   1.109+			jar->c[i].ondisk = 1;
   1.110+		}
   1.111+		Bflush(b);
   1.112+		if((d = dirfstat(Bfildes(b))) != nil){
   1.113+			q = d->qid;
   1.114+			free(d);
   1.115+		}
   1.116+		Bterm(b);
   1.117 	}
   1.118-	Bprint(b, "# webcookies cookie jar\n");
   1.119-	Bprint(b, "# comments and non-standard fields will be lost\n");
   1.120-	for(i=0; i<jar->nc; i++){
   1.121-		if(jar->c[i].expire == ~0)
   1.122-			continue;
   1.123-		Bprint(b, "%K\n", &jar->c[i]);
   1.124-		jar->c[i].ondisk = 1;
   1.125-	}
   1.126-	Bterm(b);
   1.127 
   1.128+	jar->qid = q;
   1.129 	jar->dirty = 0;
   1.130+
   1.131 	close(fd);
   1.132-	if((d = dirstat(jar->file)) != nil){
   1.133-		jar->qid = d->qid;
   1.134-		free(d);
   1.135-	}
   1.136 	return 0;
   1.137 }
   1.138 
   1.139@@ -451,7 +464,7 @@ readjar(char *file)
   1.140 	p[1] = '.';
   1.141 	jar->lockfile = lock;
   1.142 	jar->file = file;
   1.143-	jar->dirty = 1;
   1.144+	jar->dirty = 0;
   1.145 
   1.146 	if(syncjar(jar) < 0){
   1.147 		free(jar->file);
   1.148@@ -544,13 +557,16 @@ cookiesearch(Jar *jar, char *dom, char *
   1.149 {
   1.150 	int i;
   1.151 	Jar *j;
   1.152+	Cookie *c;
   1.153 	uint now;
   1.154 
   1.155 	now = time(0);
   1.156 	j = newjar();
   1.157-	for(i=0; i<jar->nc; i++)
   1.158-		if((issecure || !jar->c[i].secure) && iscookiematch(&jar->c[i], dom, path, now))
   1.159-			addcookie(j, &jar->c[i]);
   1.160+	for(i=0; i<jar->nc; i++){
   1.161+		c = &jar->c[i];
   1.162+		if(!c->deleted && (issecure || !c->secure) && iscookiematch(c, dom, path, now))
   1.163+			addcookie(j, c);
   1.164+	}
   1.165 	if(j->nc == 0){
   1.166 		closejar(j);
   1.167 		werrstr("no cookies found");