rpm  5.4.14
rpmiob.c
Go to the documentation of this file.
1 
4 #include "system.h"
5 #define _RPMIOB_INTERNAL
6 #include <rpmiotypes.h>
7 #include <rpmio.h>
8 #include "debug.h"
9 
10 /*@unchecked@*/
11 size_t _rpmiob_chunk = 1024;
12 
13 /*@unchecked@*/
15 
16 static void rpmiobFini(void * _iob)
17 {
18  rpmiob iob = (rpmiob) _iob;
19 
20 if (_rpmiob_debug)
21 fprintf(stderr, "--> %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
22  iob->b = _free(iob->b);
23  iob->blen = 0;
24  iob->allocated = 0;
25 }
26 
27 /*@unchecked@*/ /*@only@*/ /*@null@*/
29 
30 static rpmiob rpmiobGetPool(/*@null@*/ rpmioPool pool)
31  /*@globals _rpmiobPool, fileSystem @*/
32  /*@modifies pool, _rpmiobPool, fileSystem @*/
33 {
34  rpmiob iob;
35 
36  if (_rpmiobPool == NULL) {
37  _rpmiobPool = rpmioNewPool("iob", sizeof(*iob), -1, _rpmiob_debug,
38  NULL, NULL, rpmiobFini);
39  pool = _rpmiobPool;
40  }
41  return (rpmiob) rpmioGetPool(pool, sizeof(*iob));
42 }
43 
45 {
46  rpmiob iob = rpmiobGetPool(_rpmiobPool);
47 if (_rpmiob_debug)
48 fprintf(stderr, "--> %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
49  if (len == 0)
50  len = _rpmiob_chunk;
51  iob->allocated = len;
52  iob->blen = 0;
53  iob->b = (rpmuint8_t *) xcalloc(iob->allocated+1, sizeof(*iob->b));
54  return rpmiobLink(iob);
55 }
56 
58 {
59 assert(iob != NULL);
60  iob->b[0] = '\0';
61  iob->blen = 0;
62 if (_rpmiob_debug)
63 fprintf(stderr, "<-- %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
64  return iob;
65 }
66 
68 {
69 assert(iob != NULL);
70  while (iob->blen > 0 && xisspace((int)iob->b[iob->blen-1]))
71  iob->b[--iob->blen] = (rpmuint8_t) '\0';
72 if (_rpmiob_debug)
73 fprintf(stderr, "<-- %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
74  return iob;
75 }
76 
77 rpmiob rpmiobAppend(rpmiob iob, const char * s, size_t nl)
78 {
79  size_t ns = strlen(s);
80  rpmuint8_t * tail;
81 
82  if (nl > 0) ns++;
83 
84 assert(iob != NULL);
85  if ((iob->blen + ns) > iob->allocated) {
86  iob->allocated += ((ns+_rpmiob_chunk-1)/_rpmiob_chunk) * _rpmiob_chunk;
87  iob->b = (rpmuint8_t *) xrealloc(iob->b, iob->allocated+1);
88  }
89 
90  tail = iob->b + iob->blen;
91  tail = (rpmuint8_t *) stpcpy((char *)tail, s);
92  if (nl > 0) {
93  *tail++ = (rpmuint8_t) '\n';
94  *tail = (rpmuint8_t) '\0';
95  }
96  iob->blen += ns;
97 if (_rpmiob_debug)
98 fprintf(stderr, "<-- %s(%p,%p,%u) %p[%u:%u] \"%s\"\n", __FUNCTION__, iob, s, (unsigned)nl, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated, s);
99  return iob;
100 }
101 
103 {
104 assert(iob != NULL);
105 if (_rpmiob_debug)
106 fprintf(stderr, "<-- %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
107 /*@-retalias -retexpose -usereleased @*/
108  return iob->b;
109 /*@=retalias =retexpose =usereleased @*/
110 }
111 
112 char * rpmiobStr(rpmiob iob)
113 {
114 assert(iob != NULL);
115 if (_rpmiob_debug)
116 fprintf(stderr, "<-- %s(%p) %p[%u:%u]\n===============\n%s\n===============\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated, iob->b);
117 /*@-retalias -retexpose -usereleased @*/
118  return (char *) iob->b;
119 /*@=retalias =retexpose =usereleased @*/
120 }
121 
122 size_t rpmiobLen(rpmiob iob)
123 {
124 if (_rpmiob_debug)
125 fprintf(stderr, "<-- %s(%p) %p[%u:%u]\n", __FUNCTION__, iob, iob->b, (unsigned)iob->blen, (unsigned)iob->allocated);
126  return (iob != NULL ? iob->blen : 0);
127 }
128 
129 int rpmiobSlurp(const char * fn, rpmiob * iobp)
130 {
131  static size_t blenmax = (128 * BUFSIZ); /* XXX 1Mb with glibc */
132  rpmuint8_t * b = NULL;
133  size_t blen = 0;
134  struct stat sb;
135  FD_t fd;
136  int rc = 0;
137  int xx;
138 
139  fd = Fopen(fn, "r.ufdio");
140  if (fd == NULL || Ferror(fd)) {
141  rc = 2;
142  goto exit;
143  }
144  sb.st_size = 0;
145  if ((xx = Fstat(fd, &sb)) < 0 || sb.st_size == 0)
146  sb.st_size = blenmax;
147 #if defined(__linux__)
148  /* XXX st->st_size = 0 for /proc files on linux, see stat(2). */
149  /* XXX glibc mmap'd libio no workie for /proc files on linux?!? */
150  if (sb.st_size == 0 && !strncmp(fn, "/proc/", sizeof("/proc/")-1)) {
151  blen = blenmax;
152  b = (rpmuint8_t *) xmalloc(blen+1);
153  b[0] = (rpmuint8_t) '\0';
154 
155  xx = read(Fileno(fd), b, blen);
156  blen = (size_t) (xx >= 0 ? xx : 0);
157  } else
158 #endif
159  {
160  blen = sb.st_size;
161  b = (rpmuint8_t *) xmalloc(blen+1);
162  b[0] = (rpmuint8_t) '\0';
163 
164  blen = Fread(b, sizeof(*b), blen, fd);
165  if (Ferror(fd)) {
166  rc = 1;
167  goto exit;
168  }
169  }
170  if (blen < (size_t)sb.st_size)
171  b = (rpmuint8_t *) xrealloc(b, blen+1);
172  b[blen] = (rpmuint8_t) '\0';
173 
174 exit:
175  if (fd != NULL) (void) Fclose(fd);
176 
177  if (rc == 0 && iobp != NULL) {
178  /* XXX use rpmiobNew() if/when lazy iop->b alloc is implemented. */
179  rpmiob iob = rpmiobGetPool(_rpmiobPool);
180  iob->b = b;
181  iob->blen = blen;
182  iob->allocated = blen;
183  *iobp = iob;
184  } else {
185  if (iobp)
186  *iobp = NULL;
187  b = _free(b);
188  }
189 
190  return rc;
191 }
rpmiob rpmiobRTrim(rpmiob iob)
Trim trailing white space.
Definition: rpmiob.c:67
int xx
Definition: spec.c:744
rpmuint8_t * rpmiobBuf(rpmiob iob)
Return I/O buffer.
Definition: rpmiob.c:102
static void rpmiobFini(void *_iob)
Definition: rpmiob.c:16
size_t rpmiobLen(rpmiob iob)
Return I/O buffer len.
Definition: rpmiob.c:122
int _rpmiob_debug
Definition: rpmiob.c:14
FD_t Fopen(const char *path, const char *_fmode)
fopen(3) clone.
Definition: rpmio.c:2831
int rc
Definition: poptALL.c:670
void * pool
Definition: rpmiotypes.h:46
rpmiob rpmiobAppend(rpmiob iob, const char *s, size_t nl)
Append string to I/O buffer.
Definition: rpmiob.c:77
unsigned char rpmuint8_t
Private int typedefs to avoid C99 portability issues.
Definition: rpmiotypes.h:26
int rpmiobSlurp(const char *fn, rpmiob *iobp)
Definition: rpmiob.c:129
struct rpmiob_s * rpmiob
Definition: rpmiotypes.h:60
int Fstat(FD_t fd, struct stat *st)
fstat(2) clone.
Definition: rpmrpc.c:1441
goto exit
Definition: db3.c:1903
size_t ns
Definition: db3.c:1892
void * xcalloc(size_t nmemb, size_t size)
Definition: rpmmalloc.c:300
assert(key->size==sizeof(hdrNum))
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
fprintf(stderr,"--> %s(%p,%p,%p) sig %p sigp %p\n", __FUNCTION__, dig, t, rsactx, sig, sigp)
static int xisspace(int c)
Definition: rpmiotypes.h:446
The FD_t File Handle data structure.
size_t Fread(void *buf, size_t size, size_t nmemb, FD_t fd)
fread(3) clone.
Definition: rpmio.c:2410
rpmiob rpmiobLink(rpmiob iob)
Reference a I/O buffer instance.
node fd
Definition: rpmfd-py.c:124
int Fclose(FD_t fd)
fclose(3) clone.
Definition: rpmio.c:2532
rpmiob rpmiobNew(size_t len)
Create an I/O buffer.
Definition: rpmiob.c:44
int Ferror(FD_t fd)
ferror(3) clone.
Definition: rpmio.c:2942
char * rpmiobStr(rpmiob iob)
Return I/O buffer (as string).
Definition: rpmiob.c:112
const char * s
Definition: poptALL.c:734
size_t _rpmiob_chunk
Definition: rpmiob.c:11
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
char * stpcpy(char *dest, const char *src)
return NULL
Definition: poptALL.c:613
rpmiob rpmiobEmpty(rpmiob iob)
Empty an I/O buffer.
Definition: rpmiob.c:57
int Fileno(FD_t fd)
fileno(3) clone.
Definition: rpmio.c:2989
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
char * b
Definition: macro.c:746
static rpmiob rpmiobGetPool(rpmioPool pool)
Definition: rpmiob.c:30
#define xmalloc
Definition: system.h:32
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
rpmioPool _rpmiobPool
Definition: rpmiob.c:28
#define xrealloc
Definition: system.h:35
size_t fn
Definition: macro.c:1698
int len
Definition: rpmdb-py.c:119