Skip to content

Commit

Permalink
Make batch mode create output files atomically.
Browse files Browse the repository at this point in the history
  • Loading branch information
jblache committed Jan 17, 2009
1 parent 97903e0 commit a42b571
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2009-01-17 Julien Blache <[email protected]>
* doc/descriptions/hp.desc: add SCSI identifiers for the ScanJet
IIc, from Daniel Golle (sane-devel, 20061105).
* frontend/scanimage.c: make batch mode create output files
atomically. Patch by Simon Matter <[email protected]>.

2009-01-16 Chris Bagwell <cbagwell-guest at users.alioth.debian.org>
* doc/Makefile.in, frontend/Makefile.in, include/Makefile.in,
Expand Down
39 changes: 33 additions & 6 deletions frontend/scanimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Uses the SANE library.
Copyright (C) 1996, 1997, 1998 Andreas Beck and David Mosberger
Copyright (C) 1999 - 2008 by the SANE Project -- See AUTHORS and ChangeLog
Copyright (C) 1999 - 2009 by the SANE Project -- See AUTHORS and ChangeLog
for details.
For questions and comments contact the sane-devel mailinglist (see
Expand Down Expand Up @@ -2161,8 +2161,13 @@ List of available devices:", prog_name);
do
{
char path[PATH_MAX];
char part_path[PATH_MAX];
if (batch) /* format is NULL unless batch mode */
sprintf (path, format, n); /* love --(C++) */
{
sprintf (path, format, n); /* love --(C++) */
strcpy (part_path, path);
strcat (part_path, ".part");
}


if (batch)
Expand Down Expand Up @@ -2199,9 +2204,10 @@ List of available devices:", prog_name);
break;
}

if (batch && NULL == freopen (path, "w", stdout))
/* write to .part file while scanning is in progress */
if (batch && NULL == freopen (part_path, "w", stdout))
{
fprintf (stderr, "cannot open %s\n", path);
fprintf (stderr, "cannot open %s\n", part_path);
sane_cancel (device);
return SANE_STATUS_ACCESS_DENIED;
}
Expand All @@ -2216,15 +2222,36 @@ List of available devices:", prog_name);
switch (status)
{
case SANE_STATUS_GOOD:
break;
case SANE_STATUS_EOF:
status = SANE_STATUS_GOOD;
if (batch)
{
/* close output file by redirecting, do not close
stdout here! */
if (NULL == freopen ("/dev/null", "w", stdout))
{
fprintf (stderr, "cannot open /dev/null\n");
sane_cancel (device);
return SANE_STATUS_ACCESS_DENIED;
}
else
{
/* let the fully scanned file show up */
if (rename (part_path, path))
{
fprintf (stderr, "cannot rename %s to %s\n",
part_path, path);
sane_cancel (device);
return SANE_STATUS_ACCESS_DENIED;
}
}
}
break;
default:
if (batch)
{
fclose (stdout);
unlink (path);
unlink (part_path);
}
break;
} /* switch */
Expand Down

0 comments on commit a42b571

Please sign in to comment.