Create a micro abstraction around the POSIX file APIs · nodegit/libgit2@16a6777 · GitHub
Skip to content

Commit 16a6777

Browse files
committed
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 parent 50298f4 commit 16a6777

8 files changed

Lines changed: 211 additions & 10 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions

‎Makefile‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ all::
33
DOXYGEN = doxygen
44

55
CFLAGS = -g -O2
6+
OS = unix
67

78
BASIC_CFLAGS := -Iinclude
89
BASIC_CFLAGS += -DGIT__PRIVATE
@@ -11,11 +12,16 @@ BASIC_CFLAGS += -fvisibility=hidden
1112
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
1213
HDRS = $(wildcard include/git/*.h)
1314

15+
OBJS += src/os/$(OS).o
16+
HDRS += include/git/config.h
17+
HDRS += include/git/os/$(OS).h
18+
1419

1520
all:: libgit2.a
1621

1722
clean:
1823
rm -f libgit2.a src/*.o
24+
rm -f include/git/config.h
1925
rm -rf apidocs
2026

2127
apidocs:
@@ -25,6 +31,10 @@ apidocs:
2531
.c.o:
2632
$(CC) $(BASIC_CFLAGS) $(CFLAGS) -c $< -o $@
2733

34+
include/git/config.h: include/git/config.h.in
35+
sed 's/@@OS@@/$(OS)/g' $< >$@+
36+
mv $@+ $@
37+
2838
$(OBJS): $(HDRS)
2939
libgit2.a: $(OBJS)
3040
rm -f libgit2.a

‎include/git/commit.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
#include "git/common.h"
3030
#include "git/oid.h"
31-
#include <time.h>
3231

3332
/**
3433
* @file git/commit.h

‎include/git/common.h‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,20 @@
3636
# define GIT_END_DECL /* empty */
3737
#endif
3838

39+
/** Declare a public function exported for application use. */
40+
#ifdef __GNUC__
41+
# define GIT_EXTERN(type) __attribute__((visibility("default"))) type
42+
#else
43+
# define GIT_EXTERN(type) type
44+
#endif
45+
3946
/**
4047
* @file git/common.h
4148
* @brief Git common platform definitions
4249
* @defgroup git_common Git common platform definitions
4350
* @ingroup Git
4451
* @{
4552
*/
46-
GIT_BEGIN_DECL
47-
48-
/** Declare a public function exported for application use. */
49-
#ifdef __GNUC__
50-
# define GIT_EXTERN(type) __attribute__((visibility("default"))) type
51-
#else
52-
# define GIT_EXTERN(type) type
53-
#endif
5453

5554
/** Operation completed successfully. */
5655
#define GIT_SUCCESS 0
@@ -69,6 +68,10 @@ GIT_BEGIN_DECL
6968
/** Input does not exist in the scope searched. */
7069
#define GIT_ENOTFOUND (GIT_ERROR - 2)
7170

71+
#include "git/config.h"
72+
73+
GIT_BEGIN_DECL
74+
7275
/** A revision traversal pool. */
7376
typedef struct git_revp git_revp;
7477

‎include/git/config.h.in‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License, version 2,
4+
* as published by the Free Software Foundation.
5+
*
6+
* In addition to the permissions in the GNU General Public License,
7+
* the authors give you unlimited permission to link the compiled
8+
* version of this file into combinations with other programs,
9+
* and to distribute those combinations without any restriction
10+
* coming from the use of this file. (The General Public License
11+
* restrictions do apply in other respects; for example, they cover
12+
* modification of the file, and distribution when not linked into
13+
* a combined executable.)
14+
*
15+
* This file is distributed in the hope that it will be useful, but
16+
* WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; see the file COPYING. If not, write to
22+
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23+
* Boston, MA 02110-1301, USA.
24+
*/
25+
26+
#ifndef INCLUDE_git_config_h__
27+
#define INCLUDE_git_config_h__
28+
29+
#include "git/os/@@OS@@.h"
30+
31+
#endif

‎include/git/odb.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
#include "git/common.h"
3030
#include "git/oid.h"
31-
#include <unistd.h>
3231

3332
/**
3433
* @file git/odb.h

‎include/git/os/unix.h‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* This file is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License, version 2,
4+
* as published by the Free Software Foundation.
5+
*
6+
* In addition to the permissions in the GNU General Public License,
7+
* the authors give you unlimited permission to link the compiled
8+
* version of this file into combinations with other programs,
9+
* and to distribute those combinations without any restriction
10+
* coming from the use of this file. (The General Public License
11+
* restrictions do apply in other respects; for example, they cover
12+
* modification of the file, and distribution when not linked into
13+
* a combined executable.)
14+
*
15+
* This file is distributed in the hope that it will be useful, but
16+
* WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; see the file COPYING. If not, write to
22+
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23+
* Boston, MA 02110-1301, USA.
24+
*/
25+
26+
#ifndef INCLUDE_git_os_abstraction_h__
27+
#define INCLUDE_git_os_abstraction_h__
28+
29+
#include <unistd.h>
30+
#include <sys/stat.h>
31+
#include <fcntl.h>
32+
#include <sys/types.h>
33+
#include <time.h>
34+
35+
/**
36+
* @file git/os/unix.h
37+
* @brief Portable operating system abstractions
38+
* @defgroup git_os_abstraction Portable operating system abstractions
39+
* @ingroup Git
40+
* @{
41+
*/
42+
GIT_BEGIN_DECL
43+
44+
/** Descriptor to an open file in the filesystem. */
45+
typedef int git_file;
46+
47+
/**
48+
* Open a file by path name.
49+
*
50+
* Valid flags are:
51+
* - O_CREAT: Create the file if it does not yet exist.
52+
* - O_RDONLY: Open the file for reading.
53+
* - O_WRONLY: Open the file for writing.
54+
* - O_RDWR: Open the file for both reading and writing.
55+
*
56+
* @param path path name of the file to open.
57+
* @param flags bitmask of access requested to the file.
58+
* @return the opened file descriptor; <0 if the open failed.
59+
*/
60+
static inline git_file git_fopen(const char *path, int flags)
61+
{
62+
return open(path, flags);
63+
}
64+
65+
/**
66+
* Close an open file descriptor.
67+
* @param fd descriptor to close.
68+
* @return 0 on success; <0 if the descriptor close failed.
69+
*/
70+
static inline int git_fclose(git_file fd)
71+
{
72+
return close(fd);
73+
}
74+
75+
/**
76+
* Read from an open file descriptor at the current position.
77+
*
78+
* Less than the number of requested bytes may be read. The
79+
* read is automatically restarted if it fails due to a signal
80+
* being delivered to the calling thread.
81+
*
82+
* @param fd open descriptor.
83+
* @param buf buffer to store the read data into.
84+
* @param cnt number of bytes to transfer.
85+
* @return
86+
* - On success, actual number of bytes read.
87+
* - On EOF, 0.
88+
* - On failure, <0.
89+
*/
90+
GIT_EXTERN(ssize_t) git_fread(git_file fd, void *buf, size_t cnt);
91+
92+
/**
93+
* Write to an open file descriptor at the current position.
94+
*
95+
* Less than the number of requested bytes may be written. The
96+
* write is automatically restarted if it fails due to a signal
97+
* being delivered to the calling thread.
98+
*
99+
* @param fd open descriptor.
100+
* @param buf buffer to write data from.
101+
* @param cnt number of bytes to transfer.
102+
* @return
103+
* - On success, actual number of bytes written.
104+
* - On EOF, 0.
105+
* - On failure, <0.
106+
*/
107+
GIT_EXTERN(ssize_t) git_fwrite(git_file fd, void *buf, size_t cnt);
108+
109+
/** @} */
110+
GIT_END_DECL
111+
#endif

‎src/os/unix.c‎

Lines changed: 47 additions & 0 deletions

0 commit comments

Comments
 (0)