Add test for Sid.__repr__.
[bbaumbach/samba-autobuild/.git] / source4 / lib / appweb / ejs-2.0 / mpr / mprGenFile.c
1 /**
2  *      @file   mprGenFile.c
3  *      @brief  Generic File services
4  *      @overview 
5  *      @remarks 
6  *              See OS/mprFile.c for the per O/S portions
7  */
8
9 /******************************************************************************/
10 /*
11  *      @copy   default
12  *      
13  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
14  *      
15  *      This software is distributed under commercial and open source licenses.
16  *      You may use the GPL open source license described below or you may acquire 
17  *      a commercial license from Mbedthis Software. You agree to be fully bound 
18  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
19  *      this software for full details.
20  *      
21  *      This software is open source; you can redistribute it and/or modify it 
22  *      under the terms of the GNU General Public License as published by the 
23  *      Free Software Foundation; either version 2 of the License, or (at your 
24  *      option) any later version. See the GNU General Public License for more 
25  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
26  *      
27  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
28  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
29  *      
30  *      This GPL license does NOT permit incorporating this software into 
31  *      proprietary programs. If you are unable to comply with the GPL, you must
32  *      acquire a commercial license to use this software. Commercial licenses 
33  *      for this software and support services are available from Mbedthis 
34  *      Software at http://www.mbedthis.com 
35  *      
36  *      @end
37  */
38
39 /********************************** Includes **********************************/
40
41 #include        "mpr.h"
42
43 /****************************** Forward Declarations **************************/
44 #if !BREW
45
46 static int closeDestructor(void *data);
47
48 /************************************ Code ************************************/
49
50 int mprStartFileServices(MprCtx ctx)
51 {
52         MprApp          *app;
53
54         app = mprGetApp(ctx);
55         app->console = mprAllocTypeZeroed(ctx, MprFile);
56         app->error = mprAllocTypeZeroed(ctx, MprFile);
57
58         /*
59          *      We assume that STDOUT is 1 and STDERR is 2
60          */
61         app->console->fd = 1;
62         app->error->fd = 2;
63
64         return 0;
65 }
66
67 /******************************************************************************/
68
69 void mprStopFileServices(MprCtx ctx)
70 {
71         MprApp          *app;
72
73         app = mprGetApp(ctx);
74
75         mprFree(app->console);
76         app->console = 0;
77         mprFree(app->error);
78         app->error = 0;
79 }
80
81 /******************************************************************************/
82
83 MprFile *mprOpen(MprCtx ctx, const char *path, int omode, int perms)
84 {
85         MprFile         *file;
86         
87         mprAssert(path && *path);
88
89         file = mprAllocTypeZeroed(ctx, MprFile);
90         
91         file->fd = open(path, omode, perms);
92         if (file->fd < 0) {
93                 mprFree(file);
94                 return 0;
95         }
96
97         mprSetDestructor(file, closeDestructor);
98         return file;
99 }
100
101 /******************************************************************************/
102
103 static int closeDestructor(void *data)
104 {
105         MprFile *file = (MprFile*) data;
106
107         mprAssert(file);
108         
109         mprClose(file);
110         return 0;
111 }
112
113 /******************************************************************************/
114
115 void mprClose(MprFile *file)
116 {
117         mprAssert(file);
118
119         if (file < 0) {
120                 return;
121         }
122
123         mprAssert(file->fd >= 0);
124         close(file->fd);
125
126         mprSetDestructor(file, 0);
127         mprFree(file);
128 }
129  
130 /******************************************************************************/
131
132 int mprRead(MprFile *file, void *buf, uint size)
133 {
134         mprAssert(file);
135
136         if (file == 0) {
137                 return MPR_ERR_BAD_HANDLE;
138         }
139
140         return read(file->fd, buf, size);
141 }
142
143 /******************************************************************************/
144
145 int mprWrite(MprFile *file, const void *buf, uint count)
146 {
147         mprAssert(file);
148
149         if (file == 0) {
150                 return MPR_ERR_BAD_HANDLE;
151         }
152
153         return write(file->fd, buf, count);
154 }
155
156 /******************************************************************************/
157
158 int mprSeek(MprFile *file, int seekType, long distance)
159 {
160         mprAssert(file);
161
162         if (file == 0) {
163                 return MPR_ERR_BAD_HANDLE;
164         }
165
166         return lseek(file->fd, seekType, distance);
167 }
168
169 /******************************************************************************/
170
171 int mprDelete(MprCtx ctx, const char *path)
172 {
173         return unlink(path);
174 }
175
176 /******************************************************************************/
177
178 int mprDeleteDir(MprCtx ctx, const char *path)
179 {
180         return rmdir(path);
181 }
182  
183 #endif /* !BREW */
184 /******************************************************************************/
185
186 char *mprGets(MprFile *file, char *buf, uint size)
187 {
188         MprBuf  *bp;
189         int             count, len, c;
190
191         mprAssert(file);
192
193         if (file == 0) {
194                 return 0;
195         }
196
197         if (file->buf == 0) {
198                 file->buf = mprCreateBuf(file, MPR_DEFAULT_ALLOC, MPR_MAX_STRING);
199         }
200         bp = file->buf;
201
202         /*
203          *      Must leave room for null
204          */
205         count = 0;
206         while (--size > 0) {
207                 if (mprGetBufLength(bp) == 0) {
208                         mprFlushBuf(bp);
209                         len = mprRead(file, mprGetBufEnd(bp), 
210                                 mprGetBufLinearSpace(bp));
211                         if (len <= 0) {
212                                 return 0;
213                         }
214                         mprAdjustBufEnd(bp, len);
215                         mprAddNullToBuf(bp);
216                 }
217                 if ((c = mprGetCharFromBuf(bp)) == '\n') {
218                         buf[count] = '\0';
219                         return buf;
220                 }
221                 buf[count++] = c;
222         }
223         buf[count] = '\0';
224         return buf;
225 }
226
227 /******************************************************************************/
228
229 int mprPuts(MprFile *file, const char *writeBuf, uint count)
230 {
231         MprBuf  *bp;
232         char    *buf;
233         int             total, bytes, len;
234
235         mprAssert(file);
236
237         /*
238          *      Buffer output and flush when full.
239          */
240         if (file->buf == 0) {
241                 file->buf = mprCreateBuf(file, MPR_BUFSIZE, 0);
242                 if (file->buf == 0) {
243                         return MPR_ERR_CANT_ALLOCATE;
244                 }
245         }
246         bp = file->buf;
247
248         if (mprGetBufLength(bp) > 0 && mprGetBufSpace(bp) < (int) count) {
249                 len = mprGetBufLength(bp);
250                 if (mprWrite(file, mprGetBufStart(bp), len) != len) {
251                         return MPR_ERR_CANT_WRITE;
252                 }
253                 mprFlushBuf(bp);
254         }
255
256         total = 0;
257         buf = (char*) writeBuf;
258
259         while (count > 0) {
260                 bytes = mprPutBlockToBuf(bp, buf, count);
261                 if (bytes <= 0) {
262                         return MPR_ERR_CANT_ALLOCATE;
263                 }
264                 count -= bytes;
265                 buf += bytes;
266                 total += bytes;
267                 mprAddNullToBuf(bp);
268
269                 if (count > 0) {
270                         len = mprGetBufLength(bp);
271                         if (mprWrite(file, mprGetBufStart(bp), len) != len) {
272                                 return MPR_ERR_CANT_WRITE;
273                         }
274                         mprFlushBuf(bp);
275                 }
276         }
277         return total;
278 }
279
280 /******************************************************************************/
281
282 int mprMakeTempFileName(MprCtx ctx, char *buf, int bufsize, const char *tempDir)
283 {
284         MprFile         *file;
285         MprTime         now;
286         char            *dir;
287         int             seed, i;
288
289         if (tempDir == 0) {
290 #if WIN
291                 char    *cp;
292                 dir = mprStrdup(ctx, getenv("TEMP"));
293                 for (cp = dir; *cp; cp++) {
294                         if (*cp == '\\') {
295                                 *cp = '/';
296                         }
297                 }
298 #else
299                 dir = mprStrdup(ctx, "/tmp");
300 #endif
301         } else {
302                 dir = mprStrdup(ctx, tempDir);
303         }
304
305         mprGetTime(ctx, &now);
306         seed = now.msec % 64000;
307         file = 0;
308
309         for (i = 0; i < 128; i++) {
310                 mprSprintf(buf, bufsize, "%s/MPR_%d_%d.tmp", dir, getpid(), seed++);
311                 file = mprOpen(ctx, buf, O_CREAT | O_EXCL | O_BINARY, 0664);
312                 if (file) {
313                         break;
314                 }
315         }
316
317         if (file == 0) {
318                 return MPR_ERR_CANT_CREATE;
319         }
320
321         mprClose(file);
322         mprFree(dir);
323
324         return 0;
325 }
326
327 /******************************************************************************/
328 /*
329  * Local variables:
330  * tab-width: 4
331  * c-basic-offset: 4
332  * End:
333  * vim:tw=78
334  * vim600: sw=4 ts=4 fdm=marker
335  * vim<600: sw=4 ts=4
336  */