Debian Codegraph @

libhylafax/Sequence.c++

raw
1 /* $Id: */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * Copyright (c) 2003 iFax Solutions, Inc.
6 *
7 * HylaFAX is a trademark of Silicon Graphics
8 *
9 * Permission to use, copy, modify, distribute, and sell this software and
10 * its documentation for any purpose is hereby granted without fee, provided
11 * that (i) the above copyright notices and this permission notice appear in
12 * all copies of the software and related documentation, and (ii) the names of
13 * Sam Leffler and Silicon Graphics may not be used in any advertising or
14 * publicity relating to the software without the specific, prior written
15 * permission of Sam Leffler and Silicon Graphics.
16 *
17 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
19 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
22 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
23 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
25 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 * OF THIS SOFTWARE.
27 */
28
29 #include "config.h"
30 #include "Str.h"
31 #include "Sys.h"
32 #include "SystemLog.h"
33 #include "Sequence.h"
34
35 #include <sys/file.h>
36 #include <errno.h>
37
38 #include "NLS.h"
39
40
41 const fxStr Sequence::format("%09u");
42
43 u_long Sequence::getNext(const char* name, fxStr& emsg)
44 {
45 struct stat sb;
46 int fd;
47 int rtn = lstat(name, &sb);
48 if (rtn != 0 && errno == ENOENT) {
49 fd = Sys::open(name, O_CREAT | O_RDWR | O_EXCL, 0600);
50 } else if (rtn == 0 && S_ISREG(sb.st_mode)) {
51 fd = Sys::open(name, O_RDWR, 0600);
52 struct stat sb2;
53 if (fd < 0 || fstat(fd, &sb2)) {
54 //XXX some kind of error opening file
55 fd = -1;
56 } else if (sb.st_ino != sb2.st_ino || sb.st_dev != sb2.st_dev) {
57 //XXX something wrong with file
58 fd = -1;
59 }
60 } else {
61 //XXX some kind of error opening file
62 fd = -1;
63 }
64 if (fd < 0) {
65 emsg = fxStr::format(NLS::TEXT("Unable to open sequence number file %s; %s."),
66 name, strerror(errno));
67 logError("%s: open: %s", name, strerror(errno));
68 return ((u_long) -1);
69 }
70 flock(fd, LOCK_EX);
71 u_long seqnum = 1;
72 char line[1024];
73 int n = read(fd, line, sizeof (line));
74 line[n < 0 ? 0 : n] = '\0';
75 if (n > 0) {
76 seqnum = atol(line);
77 }
78 if (seqnum < 1 || seqnum >= MAXSEQNUM) {
79 logWarning(NLS::TEXT("%s: Invalid sequence number \"%s\", resetting to 1"),
80 name, line);
81 seqnum = 1;
82 }
83 fxStr line2 = fxStr::format("%u", NEXTSEQNUM(seqnum));
84 lseek(fd, 0, SEEK_SET);
85 int len = line2.length();
86 if (Sys::write(fd, (const char*)line2, len) != len ||
87 ftruncate(fd, len)) {
88 emsg = fxStr::format(
89 NLS::TEXT("Unable update sequence number file %s; write failed."), name);
90 logError("%s: Problem updating sequence number file", name);
91 return ((u_long) -1);
92 }
93 Sys::close(fd); // NB: implicit unlock
94 return (seqnum);
95 }