From owner-freebsd-hackers Thu Nov 4 14: 7:24 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from gw-nl4.philips.com (gw-nl4.philips.com [192.68.44.36]) by hub.freebsd.org (Postfix) with ESMTP id 6D39F155E5 for ; Thu, 4 Nov 1999 14:07:17 -0800 (PST) (envelope-from jbackus@plex.nl) Received: from smtprelay-nl1.philips.com (localhost.philips.com [127.0.0.1]) by gw-nl4.philips.com with ESMTP id XAA21769 for ; Thu, 4 Nov 1999 23:04:45 +0100 (MET) (envelope-from jbackus@plex.nl) Received: from smtprelay-eur1.philips.com(130.139.36.3) by gw-nl4.philips.com via mwrap (4.0a) id xma021765; Thu, 4 Nov 99 23:04:45 +0100 Received: from hal.mpn.cp.philips.com (hal.mpn.cp.philips.com [130.139.64.195]) by smtprelay-nl1.philips.com (8.9.3/8.8.5-1.2.2m-19990317) with SMTP id XAA15976 for ; Thu, 4 Nov 1999 23:04:45 +0100 (MET) Received: (qmail 86551 invoked from network); 4 Nov 1999 22:05:05 -0000 Received: from nld113-30.ods.origin-it.com (HELO jos.bugworks.com) (172.16.122.89) by hal.mpn.cp.philips.com with SMTP; 4 Nov 1999 22:05:05 -0000 Received: (qmail 85972 invoked by uid 1000); 4 Nov 1999 22:05:30 -0000 Date: Thu, 4 Nov 1999 23:05:30 +0100 From: Jos Backus To: freebsd-hackers@freebsd.org Subject: ftpd feature: lock file being stored Message-ID: <19991104230530.B84284@jos.bugworks.com> Reply-To: Jos Backus Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG This patch adds a ``-x'' flag to ftpd, which instructs ftpd to obtain an exclusive lock on files it commits to disk as a result of a store operation. This way it becomes easy to tell whether a download has finished, in case the file needs to be copied someplace else (as in my case). I used open()/fdopen() instead of fopen()/flock() to avoid the obvious race. I didn't see any easier ways to accomplish this, but if someone else does, please let me know. And if this is deemed worthy of a PR, I will send one in. Thanks. For testing, I used the following script which shows newly downloaded files: #!/bin/sh while : do ls | while read file do lockf -k -s -t 0 $file ls -l $file done sleep 10 done The patch: --- ftpd.c.orig Mon Sep 20 19:45:09 1999 +++ ftpd.c Thu Nov 4 22:49:49 1999 @@ -132,6 +132,7 @@ int restricted_data_ports = 1; int paranoid = 1; /* be extra careful about security */ int anon_only = 0; /* Only anonymous ftp allowed */ +int use_locking = 0; /* Attempt to lock exclusively while storing */ int guest; int dochroot; int stats; @@ -281,7 +282,7 @@ bind_address.s_addr = htonl(INADDR_ANY); - while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:")) != -1) { + while ((ch = getopt(argc, argv, "AdlDSURt:T:u:xva:p:")) != -1) { switch (ch) { case 'D': daemon_mode++; @@ -343,6 +344,10 @@ anon_only = 1; break; + case 'x': + use_locking = 1; + break; + case 'v': debug = 1; break; @@ -1338,7 +1343,34 @@ if (restart_point) mode = "r+"; - fout = fopen(name, mode); + if (use_locking) { + int fdout; + int flags; + mode_t create_mode = S_IRUSR | S_IWUSR + | S_IRGRP | S_IWGRP + | S_IROTH | S_IWOTH; + + switch (*mode) { + case 'a': + flags = O_CREAT | O_WRONLY | O_APPEND; + case 'w': + flags = O_CREAT | O_WRONLY | O_TRUNC; + default: /* "r+" */ + flags = O_RDWR; + } + + flags |= O_EXLOCK; + if (flags & O_CREAT) + fdout = open(name, flags, create_mode); + else + fdout = open(name, flags); + if (fdout < 0) + fout = NULL; + else + fout = fdopen(fdout, mode); + } else { + fout = fopen(name, mode); + } closefunc = fclose; if (fout == NULL) { perror_reply(553, name); -- Jos Backus _/ _/_/_/ "Modularity is not a hack." _/ _/ _/ -- D. J. Bernstein _/ _/_/_/ _/ _/ _/ _/ jbackus@plex.nl _/_/ _/_/_/ use Std::Disclaimer; To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message