1 /* mappedfile - interface to a mmaped, lockable, writable file 2 * 3 * Copyright (c) 1994-2011 Carnegie Mellon University. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The name "Carnegie Mellon University" must not be used to 18 * endorse or promote products derived from this software without 19 * prior written permission. For permission or any legal 20 * details, please contact 21 * Carnegie Mellon University 22 * Center for Technology Transfer and Enterprise Creation 23 * 4615 Forbes Avenue 24 * Suite 302 25 * Pittsburgh, PA 15213 26 * (412) 268-7393, fax: (412) 268-7395 27 * innovation@andrew.cmu.edu 28 * 29 * 4. Redistributions of any form whatsoever must retain the following 30 * acknowledgment: 31 * "This product includes software developed by Computing Services 32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)." 33 * 34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO 35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE 37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 41 */ 42 43 #ifndef _MAPPEDFILE_H 44 #define _MAPPEDFILE_H 45 46 // includes 47 #include "util.h" 48 #include <sys/types.h> 49 #include <sys/uio.h> 50 51 struct mappedfile { 52 char *fname; 53 54 /* obviously you will need 64 bit size_t for 64 bit files... */ 55 struct buf map_buf; 56 size_t map_size; 57 58 /* the file itself */ 59 int fd; 60 61 /* tracking */ 62 int lock_status; 63 int dirty; 64 int was_resized; 65 int is_rw; 66 67 struct timeval starttime; 68 }; 69 70 #define MAPPEDFILE_CREATE (1<<0) 71 #define MAPPEDFILE_RW (1<<1) 72 73 #define MF_UNLOCKED 0 74 #define MF_READLOCKED 1 75 #define MF_WRITELOCKED 2 76 77 extern int mappedfile_open(struct mappedfile **mfp, 78 const char *fname, int flags); 79 extern int mappedfile_close(struct mappedfile **mfp); 80 81 extern int mappedfile_readlock(struct mappedfile *mf); 82 extern int mappedfile_writelock(struct mappedfile *mf); 83 extern int mappedfile_unlock(struct mappedfile *mf); 84 85 extern int mappedfile_commit(struct mappedfile *mf); 86 extern ssize_t mappedfile_pwrite(struct mappedfile *mf, 87 const void *base, size_t len, 88 off_t offset); 89 extern ssize_t mappedfile_pwritebuf(struct mappedfile *mf, 90 const struct buf *buf, 91 off_t offset); 92 extern ssize_t mappedfile_pwritev(struct mappedfile *mf, 93 const struct iovec *iov, int nio, 94 off_t offset); 95 extern int mappedfile_truncate(struct mappedfile *mf, off_t offset); 96 97 extern int mappedfile_rename(struct mappedfile *mf, const char *newname); 98 99 #define mappedfile_islocked(mf) ((mf)->lock_status != MF_UNLOCKED) 100 #define mappedfile_isreadlocked(mf) ((mf)->lock_status == MF_READLOCKED) 101 #define mappedfile_iswritelocked(mf) ((mf)->lock_status == MF_WRITELOCKED) 102 #define mappedfile_iswritable(mf) (!!(mf)->is_rw) 103 #define mappedfile_base(mf) ((const char *)((mf)->map_buf.s)) 104 #define mappedfile_size(mf) ((mf)->map_size) 105 #define mappedfile_buf(mf) ((const struct buf *)(&((mf)->map_buf))) 106 #define mappedfile_fname(mf) ((const char *)((mf)->fname)) 107 108 109 #endif /* _MAPPEDFILE_H */ 110