Create a micro abstraction around the POSIX file APIs This way we can start to write IO code to read and write files in the Git object database, but provide a hook to inject native Win32 APIs instead so libgit2 can be ported to run natively on that platform. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
diff --git a/.gitignore b/.gitignore
index 2733c6a..f5e3dd7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
/apidocs
+/include/git/config.h
*.o
*.a
diff --git a/Makefile b/Makefile
index 339a0ed..51fcd3c 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,7 @@ all::
DOXYGEN = doxygen
CFLAGS = -g -O2
+OS = unix
BASIC_CFLAGS := -Iinclude
BASIC_CFLAGS += -DGIT__PRIVATE
@@ -11,11 +12,16 @@ BASIC_CFLAGS += -fvisibility=hidden
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
HDRS = $(wildcard include/git/*.h)
+OBJS += src/os/$(OS).o
+HDRS += include/git/config.h
+HDRS += include/git/os/$(OS).h
+
all:: libgit2.a
clean:
rm -f libgit2.a src/*.o
+ rm -f include/git/config.h
rm -rf apidocs
apidocs:
@@ -25,6 +31,10 @@ apidocs:
.c.o:
$(CC) $(BASIC_CFLAGS) $(CFLAGS) -c $< -o $@
+include/git/config.h: include/git/config.h.in
+ sed 's/@@OS@@/$(OS)/g' $< >$@+
+ mv $@+ $@
+
$(OBJS): $(HDRS)
libgit2.a: $(OBJS)
rm -f libgit2.a
diff --git a/include/git/commit.h b/include/git/commit.h
index 4d8d0fd..88bb8fe 100644
--- a/include/git/commit.h
+++ b/include/git/commit.h
@@ -28,7 +28,6 @@
#include "git/common.h"
#include "git/oid.h"
-#include <time.h>
/**
* @file git/commit.h
diff --git a/include/git/common.h b/include/git/common.h
index 09fe936..45bb348 100644
--- a/include/git/common.h
+++ b/include/git/common.h
@@ -36,6 +36,13 @@
# define GIT_END_DECL /* empty */
#endif
+/** Declare a public function exported for application use. */
+#ifdef __GNUC__
+# define GIT_EXTERN(type) __attribute__((visibility("default"))) type
+#else
+# define GIT_EXTERN(type) type
+#endif
+
/**
* @file git/common.h
* @brief Git common platform definitions
@@ -43,14 +50,6 @@
* @ingroup Git
* @{
*/
-GIT_BEGIN_DECL
-
-/** Declare a public function exported for application use. */
-#ifdef __GNUC__
-# define GIT_EXTERN(type) __attribute__((visibility("default"))) type
-#else
-# define GIT_EXTERN(type) type
-#endif
/** Operation completed successfully. */
#define GIT_SUCCESS 0
@@ -69,6 +68,10 @@ GIT_BEGIN_DECL
/** Input does not exist in the scope searched. */
#define GIT_ENOTFOUND (GIT_ERROR - 2)
+#include "git/config.h"
+
+GIT_BEGIN_DECL
+
/** A revision traversal pool. */
typedef struct git_revp git_revp;
diff --git a/include/git/config.h.in b/include/git/config.h.in
new file mode 100644
index 0000000..44b6a51
--- /dev/null
+++ b/include/git/config.h.in
@@ -0,0 +1,31 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDE_git_config_h__
+#define INCLUDE_git_config_h__
+
+#include "git/os/@@OS@@.h"
+
+#endif
diff --git a/include/git/odb.h b/include/git/odb.h
index abb8a36..55eee3f 100644
--- a/include/git/odb.h
+++ b/include/git/odb.h
@@ -28,7 +28,6 @@
#include "git/common.h"
#include "git/oid.h"
-#include <unistd.h>
/**
* @file git/odb.h
diff --git a/include/git/os/unix.h b/include/git/os/unix.h
new file mode 100644
index 0000000..351cfa4
--- /dev/null
+++ b/include/git/os/unix.h
@@ -0,0 +1,111 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDE_git_os_abstraction_h__
+#define INCLUDE_git_os_abstraction_h__
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <time.h>
+
+/**
+ * @file git/os/unix.h
+ * @brief Portable operating system abstractions
+ * @defgroup git_os_abstraction Portable operating system abstractions
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/** Descriptor to an open file in the filesystem. */
+typedef int git_file;
+
+/**
+ * Open a file by path name.
+ *
+ * Valid flags are:
+ * - O_CREAT: Create the file if it does not yet exist.
+ * - O_RDONLY: Open the file for reading.
+ * - O_WRONLY: Open the file for writing.
+ * - O_RDWR: Open the file for both reading and writing.
+ *
+ * @param path path name of the file to open.
+ * @param flags bitmask of access requested to the file.
+ * @return the opened file descriptor; <0 if the open failed.
+ */
+static inline git_file git_fopen(const char *path, int flags)
+{
+ return open(path, flags);
+}
+
+/**
+ * Close an open file descriptor.
+ * @param fd descriptor to close.
+ * @return 0 on success; <0 if the descriptor close failed.
+ */
+static inline int git_fclose(git_file fd)
+{
+ return close(fd);
+}
+
+/**
+ * Read from an open file descriptor at the current position.
+ *
+ * Less than the number of requested bytes may be read. The
+ * read is automatically restarted if it fails due to a signal
+ * being delivered to the calling thread.
+ *
+ * @param fd open descriptor.
+ * @param buf buffer to store the read data into.
+ * @param cnt number of bytes to transfer.
+ * @return
+ * - On success, actual number of bytes read.
+ * - On EOF, 0.
+ * - On failure, <0.
+ */
+GIT_EXTERN(ssize_t) git_fread(git_file fd, void *buf, size_t cnt);
+
+/**
+ * Write to an open file descriptor at the current position.
+ *
+ * Less than the number of requested bytes may be written. The
+ * write is automatically restarted if it fails due to a signal
+ * being delivered to the calling thread.
+ *
+ * @param fd open descriptor.
+ * @param buf buffer to write data from.
+ * @param cnt number of bytes to transfer.
+ * @return
+ * - On success, actual number of bytes written.
+ * - On EOF, 0.
+ * - On failure, <0.
+ */
+GIT_EXTERN(ssize_t) git_fwrite(git_file fd, void *buf, size_t cnt);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/os/unix.c b/src/os/unix.c
new file mode 100644
index 0000000..d132e97
--- /dev/null
+++ b/src/os/unix.c
@@ -0,0 +1,47 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <errno.h>
+#include "git/common.h"
+
+ssize_t git_fread(git_file fd, void *buf, size_t cnt)
+{
+ for (;;) {
+ ssize_t r = read(fd, buf, cnt);
+ if (r < 0 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ return r;
+ }
+}
+
+ssize_t git_fwrite(git_file fd, void *buf, size_t cnt)
+{
+ for (;;) {
+ ssize_t r = write(fd, buf, cnt);
+ if (r < 0 && (errno == EINTR || errno == EAGAIN))
+ continue;
+ return r;
+ }
+}