qLibc
qcount.c
Go to the documentation of this file.
1 /******************************************************************************
2  * qLibc
3  *
4  * Copyright (c) 2010-2015 Seungyoung Kim.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
28 
29 /**
30  * @file qcount.c Counter file handling APIs.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include "qinternal.h"
44 #include "utilities/qstring.h"
45 #include "utilities/qcount.h"
46 
47 /**
48  * Read counter(integer) from file with advisory file locking.
49  *
50  * @param filepath file path
51  *
52  * @return counter value readed from file. in case of failure, returns 0.
53  *
54  * @code
55  * qcount_save("number.dat", 75);
56  * int count = qcount_read("number.dat");
57  * @endcode
58  *
59  * @code
60  * ---- number.dat ----
61  * 75
62  * --------------------
63  * @endcode
64  */
65 int64_t qcount_read(const char *filepath) {
66  int fd = open(filepath, O_RDONLY, 0);
67  if (fd < 0)
68  return 0;
69 
70  char buf[20 + 1];
71  ssize_t readed = read(fd, buf, (sizeof(buf) - 1));
72  close(fd);
73 
74  int64_t num = 0;
75  if (readed > 0) {
76  buf[readed] = '\0';
77  num = atoll(buf);
78  }
79 
80  return num;
81 }
82 
83 /**
84  * Save counter(integer) to file with advisory file locking.
85  *
86  * @param filepath file path
87  * @param number counter integer value
88  *
89  * @return true if successful, otherwise returns false.
90  *
91  * @code
92  * qcount_save("number.dat", 75);
93  * @endcode
94  */
95 bool qcount_save(const char *filepath, int64_t number) {
96  int fd = open(filepath, O_CREAT | O_WRONLY | O_TRUNC,
97  (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
98  if (fd < 0)
99  return false;
100 
101  char *str = qstrdupf("%"PRId64, number);
102  ssize_t updated = write(fd, str, strlen(str));
103  close(fd);
104 
105  if (updated > 0)
106  return true;
107  return false;
108 }
109 
110 /**
111  * Increases(or decrease) the counter value as much as specified number
112  * with advisory file locking.
113  *
114  * @param filepath file path
115  * @param number how much increase or decrease
116  *
117  * @return updated counter value. in case of failure, returns 0.
118  *
119  * @code
120  * int count;
121  * count = qcount_update("number.dat", -3);
122  * @endcode
123  */
124 int64_t qcount_update(const char *filepath, int64_t number) {
125  int64_t counter = qcount_read(filepath);
126  counter += number;
127  if (qcount_save(filepath, counter) == true) {
128  return counter;
129  }
130  return 0;
131 }
bool qcount_save(const char *filepath, int64_t number)
Save counter(integer) to file with advisory file locking.
Definition: qcount.c:95
int64_t qcount_update(const char *filepath, int64_t number)
Increases(or decrease) the counter value as much as specified number with advisory file locking...
Definition: qcount.c:124
char * qstrdupf(const char *format,...)
Duplicate a formatted string.
Definition: qstring.c:358
int64_t qcount_read(const char *filepath)
Read counter(integer) from file with advisory file locking.
Definition: qcount.c:65