]> git.proxmox.com Git - libgit2.git/blobdiff - src/fileops.h
Hide ".git" directory on Windows upon creation of a non bare repository
[libgit2.git] / src / fileops.h
index 56846420552d85e7d5e8a19109594ce4e9792b7d..229f6c4d75ea95ed01633f4b766e3369cb027264 100644 (file)
@@ -6,40 +6,45 @@
 #ifndef INCLUDE_fileops_h__
 #define INCLUDE_fileops_h__
 
-/** Force 64 bit off_t size on POSIX. */
-#define _FILE_OFFSET_BITS 64
-
 #include "common.h"
 #include "map.h"
 #include "dir.h"
-#include <sys/stat.h>
 #include <fcntl.h>
 #include <time.h>
 
 #ifdef GIT_WIN32
-static inline int link(const char *old, const char *new)
+#define GIT_PLATFORM_PATH_SEP '\\'
+#else
+#define GIT_PLATFORM_PATH_SEP '/'
+#endif
+
+#define S_IFGITLINK 0160000
+#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
+
+#ifdef GIT_WIN32
+GIT_INLINE(int) link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new))
 {
+       GIT_UNUSED_ARG(old)
+       GIT_UNUSED_ARG(new)
        errno = ENOSYS;
        return -1;
 }
 
-static inline int fsync(int fd)
-{
-       return 0;
-}
-
-static inline int git__mkdir(const char *path, int mode)
+GIT_INLINE(int) git__mkdir(const char *path, int GIT_UNUSED(mode))
 {
+       GIT_UNUSED_ARG(mode)
        return mkdir(path);
 }
 
 extern int git__unlink(const char *path);
 extern int git__mkstemp(char *template);
+extern int git__fsync(int fd);
 
 # ifndef GIT__WIN32_NO_HIDE_FILEOPS
 #  define unlink(p) git__unlink(p)
 #  define mkstemp(t) git__mkstemp(t)
-#  define mkdir(p,m) git__mkdir(p,m)
+#  define mkdir(p,m) git__mkdir(p, m)
+#  define fsync(fd) git__fsync(fd)
 # endif
 #endif  /* GIT_WIN32 */
 
@@ -51,7 +56,6 @@ extern int git__mkstemp(char *template);
 #define GITFO_BUF_INIT {NULL, 0}
 
 typedef int git_file;
-typedef struct gitfo_cache gitfo_cache;
 
 typedef struct {  /* file io buffer  */
        void *data;  /* data bytes   */
@@ -61,15 +65,46 @@ typedef struct {  /* file io buffer  */
 extern int gitfo_exists(const char *path);
 extern int gitfo_open(const char *path, int flags);
 extern int gitfo_creat(const char *path, int mode);
+extern int gitfo_creat_force(const char *path, int mode);
+extern int gitfo_creat_locked(const char *path, int mode);
+extern int gitfo_creat_locked_force(const char *path, int mode);
+extern int gitfo_mktemp(char *path_out, const char *filename);
+extern int gitfo_isdir(const char *path);
+extern int gitfo_isfile(const char *path);
+extern int gitfo_mkdir_recurs(const char *path, int mode);
+extern int gitfo_mkdir_2file(const char *path);
 #define gitfo_close(fd) close(fd)
 
 extern int gitfo_read(git_file fd, void *buf, size_t cnt);
 extern int gitfo_write(git_file fd, void *buf, size_t cnt);
-extern off_t gitfo_size(git_file fd);
+#define gitfo_lseek(f,n,w) lseek(f, n, w)
+extern git_off_t gitfo_size(git_file fd);
 
 extern int gitfo_read_file(gitfo_buf *obj, const char *path);
 extern void gitfo_free_buf(gitfo_buf *obj);
 
+/* Move (rename) a file; this operation is atomic */
+extern int gitfo_mv(const char *from, const char *to);
+
+/* Move a file (forced); this will create the destination
+ * path if it doesn't exist */
+extern int gitfo_mv_force(const char *from, const char *to);
+
+#define gitfo_stat(p,b) stat(p, b)
+#define gitfo_fstat(f,b) fstat(f, b)
+
+#ifdef GIT_WIN32
+#  define gitfo_lstat(p,b) gitfo_lstat__w32(p,b)
+#  define gitfo_readlink(a, b, c) gitfo_readlink__w32(a, b, c)
+
+   extern int gitfo_lstat__w32(const char *file_name, struct stat *buf);
+   extern int gitfo_readlink__w32(const char *link, char *target, size_t target_len);
+   extern int gitfo_hide_directory__w32(const char *path);
+#else
+#  define gitfo_lstat(p,b) lstat(p,b)
+#  define gitfo_readlink(a, b, c) readlink(a, b, c)
+#endif
+
 #define gitfo_unlink(p) unlink(p)
 #define gitfo_rmdir(p) rmdir(p)
 #define gitfo_chdir(p) chdir(p)
@@ -97,7 +132,7 @@ extern void gitfo_free_buf(gitfo_buf *obj);
 extern int gitfo_map_ro(
        git_map *out,
        git_file fd,
-       off_t begin,
+       git_off_t begin,
        size_t len);
 
 /**
@@ -123,9 +158,61 @@ extern int gitfo_dirent(
        int (*fn)(void *, char *),
        void *state);
 
-extern gitfo_cache *gitfo_enable_caching(git_file fd, size_t cache_size);
-extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);
-extern int gitfo_flush_cached(gitfo_cache *ioc);
-extern int gitfo_close_cached(gitfo_cache *ioc);
+extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
+               const char *name2, int len2, int isdir2);
+
+extern int gitfo_getcwd(char *buffer_out, size_t size);
+
+/**
+ * Clean up a provided absolute or relative directory path.
+ * 
+ * This prettification relies on basic operations such as coalescing 
+ * multiple forward slashes into a single slash, removing '.' and 
+ * './' current directory segments, and removing parent directory 
+ * whenever '..' is encountered.
+ *
+ * If not empty, the returned path ends with a forward slash.
+ *
+ * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/".
+ *
+ * This only performs a string based analysis of the path.
+ * No checks are done to make sure the path actually makes sense from 
+ * the file system perspective.
+ *
+ * @param buffer_out buffer to populate with the normalized path.
+ * @param size buffer size.
+ * @param path directory path to clean up.
+ * @return
+ * - GIT_SUCCESS on success;
+ * - GIT_ERROR when the input path is invalid or escapes the current directory.
+ */
+int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path);
+
+/**
+ * Clean up a provided absolute or relative file path.
+ * 
+ * This prettification relies on basic operations such as coalescing 
+ * multiple forward slashes into a single slash, removing '.' and 
+ * './' current directory segments, and removing parent directory 
+ * whenever '..' is encountered.
+ *
+ * For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
+ *
+ * This only performs a string based analysis of the path.
+ * No checks are done to make sure the path actually makes sense from 
+ * the file system perspective.
+ *
+ * @param buffer_out buffer to populate with the normalized path.
+ * @param size buffer size.
+ * @param path file path to clean up.
+ * @return
+ * - GIT_SUCCESS on success;
+ * - GIT_ERROR when the input path is invalid or escapes the current directory.
+ */
+int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path);
+
+void gitfo_posixify_path(char *path);
+
+int gitfo_retrieve_path_root_offset(const char *path);
 
 #endif /* INCLUDE_fileops_h__ */