#include "pathlocks.hh" #include "util.hh" #include #include #include #include #include namespace nix { int openLockFile(const Path & path, bool create) { AutoCloseFD fd; fd = open(path.c_str(), O_RDWR | (create ? O_CREAT : 0), 0600); if (fd == -1 && (create || errno != ENOENT)) throw SysError(format("opening lock file `%1%'") % path); closeOnExec(fd); return fd.borrow(); } void deleteLockFile(const Path & path, int fd) { /* Get rid of the lock file. Have to be careful not to introduce races. Write a (meaningless) token to the file to indicate to other processes waiting on this lock that the lock is stale (deleted). */ unlink(path.c_str()); writeFull(fd, "d"); /* Note that the result of unlink() is ignored; removing the lock file is an optimisation, not a necessity. */ } bool lockFile(int fd, LockType lockType, bool wait) { struct flock lock; if (lockType == ltRead) lock.l_type = F_RDLCK; else if (lockType == ltWrite) lock.l_type = F_WRLCK; else if (lockType == ltNone) lock.l_type = F_UNLCK; else abort(); lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; /* entire file */ if (wait) { while (fcntl(fd, F_SETLKW, &lock) != 0) { checkInterrupt(); if (errno != EINTR) throw SysError(format("acquiring/releasing lock")); } } else { while (fcntl(fd, F_SETLK, &lock) != 0) { checkInterrupt(); if (errno == EACCES || errno == EAGAIN) return false; if (errno != EINTR) throw SysError(format("acquiring/releasing lock")); } } return true; } /* This enables us to check whether are not already holding a lock on a file ourselves. POSIX locks (fcntl) suck in this respect: if we close a descriptor, the previous lock w;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2018, 2019, 2022 Ludovic Courtès <ludo@gnu.org> ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. (define-module (test-status) #:use-module (guix status) #:use-module (srfi srfi-1) #:use-module (srfi srfi-64) #:use-module (srfi srfi-71) #:use-module (rnrs bytevectors) #:use-module (rnrs io ports) #:use-module (ice-9 match)) (test-begin "status") (test-equal "compute-status, no-op" (build-status) (let ((port get-status (build-event-output-port compute-status))) (display "foo\nbar\n\baz\n" port) (get-status))) (test-equal "compute-status, builds + substitutes" (list (build-status (building (list (build "foo.drv" "x86_64-linux"))) (downloading (list (download "bar" "http://example.org/bar" #:size 500 #:start 'now)))) (build-status (building (list (build "foo.drv" "x86_64-linux"))) (downloading (list (download "bar" "http://example.org/bar" #:size 500 #:transferred 42 #:start 'now)))) (build-status (builds-completed (list (build "foo.drv" "x86_64-linux"))) (downloads-completed (list (download "bar" "http://example.org/bar" #:size 500 #:transferred 500 #:start 'now #:end 'now))))) (let ((port get-status (build-event-output-port (lambda (event status) (compute-status event status #:current-time (const 'now)))))) (display "@ build-started foo.drv - x86_64-linux \n" port) (display "@ substituter-started bar\n" port) (display "@ download-started bar http://example.org/bar 500\n" port) (display "various\nthings\nget\nwritten\n" port) (let ((first (get-status))) (display "@ download-progress bar http://example.org/bar 500 42\n" port) (let ((second (get-status))) (display "@ download-progress bar http://example.org/bar 500 84\n" port) (display "@ build-succeeded foo.drv\n" port) (display "@ download-succeeded bar http://example.org/bar 500\n" port) (display &q