3 * @brief Generic File services
6 * See OS/mprFile.c for the per O/S portions
9 /******************************************************************************/
13 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
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.
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
27 * This program is distributed WITHOUT ANY WARRANTY; without even the
28 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
39 /********************************** Includes **********************************/
43 /****************************** Forward Declarations **************************/
46 static int closeDestructor(void *data);
48 /************************************ Code ************************************/
50 int mprStartFileServices(MprCtx ctx)
55 app->console = mprAllocTypeZeroed(ctx, MprFile);
56 app->error = mprAllocTypeZeroed(ctx, MprFile);
59 * We assume that STDOUT is 1 and STDERR is 2
67 /******************************************************************************/
69 void mprStopFileServices(MprCtx ctx)
75 mprFree(app->console);
81 /******************************************************************************/
83 MprFile *mprOpen(MprCtx ctx, const char *path, int omode, int perms)
87 mprAssert(path && *path);
89 file = mprAllocTypeZeroed(ctx, MprFile);
91 file->fd = open(path, omode, perms);
97 mprSetDestructor(file, closeDestructor);
101 /******************************************************************************/
103 static int closeDestructor(void *data)
105 MprFile *file = (MprFile*) data;
113 /******************************************************************************/
115 void mprClose(MprFile *file)
123 mprAssert(file->fd >= 0);
126 mprSetDestructor(file, 0);
130 /******************************************************************************/
132 int mprRead(MprFile *file, void *buf, uint size)
137 return MPR_ERR_BAD_HANDLE;
140 return read(file->fd, buf, size);
143 /******************************************************************************/
145 int mprWrite(MprFile *file, const void *buf, uint count)
150 return MPR_ERR_BAD_HANDLE;
153 return write(file->fd, buf, count);
156 /******************************************************************************/
158 int mprSeek(MprFile *file, int seekType, long distance)
163 return MPR_ERR_BAD_HANDLE;
166 return lseek(file->fd, seekType, distance);
169 /******************************************************************************/
171 int mprDelete(MprCtx ctx, const char *path)
176 /******************************************************************************/
178 int mprDeleteDir(MprCtx ctx, const char *path)
184 /******************************************************************************/
186 char *mprGets(MprFile *file, char *buf, uint size)
197 if (file->buf == 0) {
198 file->buf = mprCreateBuf(file, MPR_DEFAULT_ALLOC, MPR_MAX_STRING);
203 * Must leave room for null
207 if (mprGetBufLength(bp) == 0) {
209 len = mprRead(file, mprGetBufEnd(bp),
210 mprGetBufLinearSpace(bp));
214 mprAdjustBufEnd(bp, len);
217 if ((c = mprGetCharFromBuf(bp)) == '\n') {
227 /******************************************************************************/
229 int mprPuts(MprFile *file, const char *writeBuf, uint count)
233 int total, bytes, len;
238 * Buffer output and flush when full.
240 if (file->buf == 0) {
241 file->buf = mprCreateBuf(file, MPR_BUFSIZE, 0);
242 if (file->buf == 0) {
243 return MPR_ERR_CANT_ALLOCATE;
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;
257 buf = (char*) writeBuf;
260 bytes = mprPutBlockToBuf(bp, buf, count);
262 return MPR_ERR_CANT_ALLOCATE;
270 len = mprGetBufLength(bp);
271 if (mprWrite(file, mprGetBufStart(bp), len) != len) {
272 return MPR_ERR_CANT_WRITE;
280 /******************************************************************************/
282 int mprMakeTempFileName(MprCtx ctx, char *buf, int bufsize, const char *tempDir)
292 dir = mprStrdup(ctx, getenv("TEMP"));
293 for (cp = dir; *cp; cp++) {
299 dir = mprStrdup(ctx, "/tmp");
302 dir = mprStrdup(ctx, tempDir);
305 mprGetTime(ctx, &now);
306 seed = now.msec % 64000;
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);
318 return MPR_ERR_CANT_CREATE;
327 /******************************************************************************/
334 * vim600: sw=4 ts=4 fdm=marker