Lines Matching refs:st

161 status_rd(struct status *st)
167 sr = status_rdraw(st, &line);
172 st->error = STATUS_ERR_PARSE;
179 status_rdraw(struct status *st, char **linep)
184 if (st->rd == NULL || st->eof)
186 line = stream_getln(st->rd, NULL);
188 if (stream_eof(st->rd)) {
189 if (st->depth != 0) {
190 st->error = STATUS_ERR_TRUNC;
193 st->eof = 1;
196 st->error = STATUS_ERR_READ;
197 st->suberror = errno;
200 st->linenum++;
204 st->error = STATUS_ERR_PARSE;
214 st->depth++;
224 if (st->depth <= 0) {
225 st->error = STATUS_ERR_BOGUS_DIRUP;
228 st->depth--;
237 st->error = STATUS_ERR_BAD_TYPE;
238 st->suberror = cmd[0];
243 if (st->previous != NULL &&
244 statusrec_cmp(st->previous, &sr) >= 0) {
245 st->error = STATUS_ERR_UNSORTED;
250 if (st->previous == NULL) {
251 st->previous = &st->buf;
253 statusrec_fini(st->previous);
254 statusrec_init(st->previous);
256 st->previous->sr_type = sr.sr_type;
257 st->previous->sr_file = sr.sr_file;
259 return (st->previous);
263 status_wr(struct status *st, struct statusrec *sr)
270 pc = st->pc;
284 error = proto_printf(st->wr, "D %s\n", name);
291 error = proto_printf(st->wr, "U %s %f\n", name, fa);
303 error = proto_printf(st->wr, "C %s %s %s %f %s %s %f\n",
308 error = proto_printf(st->wr, "c %s %s %s %f\n", sr->sr_file,
312 error = proto_printf(st->wr, "V %s %f\n", sr->sr_file,
316 error = proto_printf(st->wr, "v %s %f\n", sr->sr_file,
324 st->error = STATUS_ERR_WRITE;
325 st->suberror = errno;
330 status_wrraw(struct status *st, struct statusrec *sr, char *line)
336 if (st->wr == NULL)
346 pathcomp_put(st->pc, PC_DIRDOWN, sr->sr_file);
348 pathcomp_put(st->pc, PC_DIRUP, sr->sr_file);
350 pathcomp_put(st->pc, PC_FILE, sr->sr_file);
352 ret = pathcomp_get(st->pc, &type, &name);
359 ret = pathcomp_get(st->pc, &type, &name);
386 error = proto_printf(st->wr, "%c %S\n", cmd, sr->sr_file);
388 error = proto_printf(st->wr, "%c %s %S\n", cmd, sr->sr_file,
391 st->error = STATUS_ERR_WRITE;
392 st->suberror = errno;
430 struct status *st;
432 st = xmalloc(sizeof(struct status));
433 st->path = path;
434 st->error = 0;
435 st->suberror = 0;
436 st->tempfile = NULL;
437 st->scantime = scantime;
438 st->rd = file;
439 st->wr = NULL;
440 st->previous = NULL;
441 st->current = NULL;
442 st->dirty = 0;
443 st->eof = 0;
444 st->linenum = 0;
445 st->depth = 0;
446 st->pc = pathcomp_new();
447 statusrec_init(&st->buf);
448 return (st);
452 status_free(struct status *st)
455 if (st->previous != NULL)
456 statusrec_fini(st->previous);
457 if (st->rd != NULL)
458 stream_close(st->rd);
459 if (st->wr != NULL)
460 stream_close(st->wr);
461 if (st->tempfile != NULL)
462 free(st->tempfile);
463 free(st->path);
464 pathcomp_free(st->pc);
465 free(st);
471 struct status *st;
499 st = status_new(path, scantime, file);
500 st->linenum = 1;
501 return (st);
507 struct status *st;
509 st = status_new(path, -1, NULL);
510 st->eof = 1;
511 return (st);
523 struct status *st;
538 st = status_fromnull(path);
540 st = status_fromrd(path, file);
541 if (st == NULL) {
553 st->tempfile = tempname(destpath);
558 status_free(st);
562 st->wr = stream_open_file(st->tempfile,
564 if (st->wr == NULL) {
566 st->tempfile, strerror(errno));
567 status_free(st);
573 rv = fattr_install(fa, st->tempfile, NULL);
578 st->tempfile, strerror(errno));
579 status_free(st);
582 if (scantime != st->scantime)
583 st->dirty = 1;
584 error = proto_printf(st->wr, "F %d %t\n", STATUS_VERSION,
587 st->error = STATUS_ERR_WRITE;
588 st->suberror = errno;
589 *errmsg = status_errmsg(st);
590 status_free(st);
594 return (st);
605 status_get(struct status *st, char *name, int isdirup, int deleteto,
613 if (st->eof)
616 if (st->error)
620 sr = status_rd(st);
622 if (st->error)
630 if (st->current != NULL) {
631 sr = st->current;
632 st->current = NULL;
634 sr = status_rd(st);
636 if (st->error)
650 if (st->wr != NULL && !deleteto) {
651 error = status_wr(st, sr);
657 sr = status_rdraw(st, &line);
659 if (st->error)
666 if (st->wr != NULL && !deleteto) {
667 error = status_wrraw(st, sr, line);
674 st->error = STATUS_ERR_PARSE;
678 st->current = sr;
691 status_put(struct status *st, struct statusrec *sr)
696 ret = status_get(st, sr->sr_file, sr->sr_type == SR_DIRUP, 0, &old);
708 ret = status_get(st, sr->sr_file, 1, 1, &old);
714 st->current = NULL;
716 st->dirty = 1;
717 error = status_wr(st, sr);
727 status_delete(struct status *st, char *name, int isdirup)
732 ret = status_get(st, name, isdirup, 0, &sr);
736 st->current = NULL;
737 st->dirty = 1;
746 status_eof(struct status *st)
749 return (st->eof);
758 status_errmsg(struct status *st)
762 if (!st->error)
764 switch (st->error) {
767 st->path, strerror(st->suberror));
771 st->tempfile, strerror(st->suberror));
775 "Could not parse status record", st->path, st->linenum);
779 "File is not sorted properly", st->path, st->linenum);
783 "File is truncated", st->path);
787 "\"U\" entry has no matching \"D\"", st->path, st->linenum);
791 "Invalid file type \"%c\"", st->path, st->linenum,
792 st->suberror);
796 st->tempfile, st->path, strerror(st->suberror));
813 status_close(struct status *st, char **errmsg)
819 if (st->wr != NULL) {
820 if (st->dirty) {
821 if (st->current != NULL) {
822 error = status_wr(st, st->current);
824 *errmsg = status_errmsg(st);
827 st->current = NULL;
830 while ((sr = status_rdraw(st, &line)) != NULL) {
831 error = status_wrraw(st, sr, line);
833 *errmsg = status_errmsg(st);
837 if (st->error) {
838 *errmsg = status_errmsg(st);
843 pathcomp_finish(st->pc);
844 while (pathcomp_get(st->pc, &type, &name)) {
846 error = proto_printf(st->wr, "U %s %f\n",
849 st->error = STATUS_ERR_WRITE;
850 st->suberror = errno;
851 *errmsg = status_errmsg(st);
857 error = rename(st->tempfile, st->path);
859 st->error = STATUS_ERR_RENAME;
860 st->suberror = errno;
861 *errmsg = status_errmsg(st);
866 unlink(st->tempfile);
870 status_free(st);
873 status_free(st);