{"thread":{"id":"21496","subject":"[PATCH] git_odb_open ckeck for valid path to database","startedAt":"2009-11-06T12:32:09Z","lastAt":"2009-12-03T18:54:57Z","messageCount":5,"participants":["Esben Mose Hansen","Ramsay Jones"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"126976","messageId":"1257510729-6803-1-git-send-email-kde@mosehansen.dk","threadId":"21496","inReplyTo":null,"subject":"[PATCH] git_odb_open ckeck for valid path to database","fromName":"Esben Mose Hansen","fromEmail":"kde@mosehansen.dk","sentAt":"2009-11-06T12:32:09Z","receivedAt":"2009-11-06T12:32:09Z","isPatch":true,"sender":{"key":"kde@mosehansen.dk","avatar":null},"body":"---\n src/errors.c                |    1 +\n src/git/common.h            |    3 +++\n src/odb.c                   |   13 +++++++++++++\n tests/t0204-opendb_errors.c |   38 ++++++++++++++++++++++++++++++++++++++\n 4 files changed, 55 insertions(+), 0 deletions(-)\n create mode 100644 tests/t0204-opendb_errors.c\n\ndiff --git a/src/errors.c b/src/errors.c\nindex f348997..074c01c 100644\n--- a/src/errors.c\n+++ b/src/errors.c\n@@ -36,6 +36,7 @@ static struct {\n \t{ GIT_ENOTOID, \"Not a git oid\" },\n \t{ GIT_ENOTFOUND, \"Object does not exist in the scope searched\" },\n \t{ GIT_ENOMEM, \"Not enough space\" },\n+\t{ GIT_ENOTDIR, \"Not a directory\" }\n };\n \n const char *git_strerror(int num)\ndiff --git a/src/git/common.h b/src/git/common.h\nindex c470e0e..96f2971 100644\n--- a/src/git/common.h\n+++ b/src/git/common.h\n@@ -65,6 +65,9 @@\n /** Consult the OS error information. */\n #define GIT_EOSERR (GIT_ERROR - 4)\n \n+/** The path is not a directory */\n+#define GIT_ENOTDIR (GIT_ERROR - 5)\n+\n GIT_BEGIN_DECL\n \n /** A revision traversal pool. */\ndiff --git a/src/odb.c b/src/odb.c\nindex 6d646a4..89d6d03 100644\n--- a/src/odb.c\n+++ b/src/odb.c\n@@ -30,6 +30,10 @@\n #include \"hash.h\"\n #include \"odb.h\"\n \n+#include <sys/types.h>\n+#include <sys/stat.h>\n+#include <unistd.h>\n+\n #define GIT_PACK_NAME_MAX (5 + 40 + 1)\n struct git_pack {\n \tgit_odb *db;\n@@ -1023,6 +1027,15 @@ int git_odb_open(git_odb **out, const char *objects_dir)\n \t\tfree(db);\n \t\treturn GIT_ERROR;\n \t}\n+\tstruct stat objects_dir_stat;\n+\tif (stat(db->objects_dir, &objects_dir_stat)) {\n+\t\tfree(db);\n+\t\treturn GIT_EOSERR;\n+\t}\n+\tif (!S_ISDIR(objects_dir_stat.st_mode)) {\n+\t\tfree(db);\n+\t\treturn GIT_ENOTDIR;\n+\t}\n \n \tgitlck_init(&db->lock);\n \ndiff --git a/tests/t0204-opendb_errors.c b/tests/t0204-opendb_errors.c\nnew file mode 100644\nindex 0000000..e9b52c9\n--- /dev/null\n+++ b/tests/t0204-opendb_errors.c\n@@ -0,0 +1,38 @@\n+#include \"test_lib.h\"\n+#include \"test_helpers.h\"\n+#include <git/odb.h>\n+#include \"fileops.h\"\n+\n+static char *odb_dir = \"test-objects\";\n+\n+static unsigned char one_bytes[] = {\n+    0x31, 0x78, 0x9c, 0xe3, 0x02, 0x00, 0x00, 0x0b,\n+    0x00, 0x0b,\n+};\n+\n+static unsigned char one_data[] = {\n+    0x0a,\n+};\n+\n+static object_data one = {\n+    one_bytes,\n+    sizeof(one_bytes),\n+    \"8b137891791fe96927ad78e64b0aad7bded08bdc\",\n+    \"blob\",\n+    \"test-objects/8b\",\n+    \"test-objects/8b/137891791fe96927ad78e64b0aad7bded08bdc\",\n+    one_data,\n+    sizeof(one_data),\n+};\n+\n+BEGIN_TEST(opendb_errors)\n+    git_odb *db;\n+    int error = git_odb_open(&db, odb_dir);\n+    must_be_true(error == GIT_EOSERR);\n+    must_be_true(errno == ENOENT);\n+    must_pass(write_object_files(odb_dir, &one));\n+    error = git_odb_open(&db, one.file);\n+    must_be_true(error == GIT_ENOTDIR);\n+\n+    must_pass(remove_object_files(odb_dir, &one));\n+END_TEST\n-- \n1.6.3.3\n"},{"id":"127188","messageId":"4AF86A7F.1010500@ramsay1.demon.co.uk","threadId":"21496","inReplyTo":"1257510729-6803-1-git-send-email-kde@mosehansen.dk","subject":"Re: [PATCH] git_odb_open ckeck for valid path to database","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsay1.demon.co.uk","sentAt":"2009-11-09T19:16:15Z","receivedAt":"2009-11-09T19:16:15Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"Esben Mose Hansen wrote:\n> diff --git a/src/odb.c b/src/odb.c\n> index 6d646a4..89d6d03 100644\n> --- a/src/odb.c\n> +++ b/src/odb.c\n> @@ -30,6 +30,10 @@\n>  #include \"hash.h\"\n>  #include \"odb.h\"\n>  \n> +#include <sys/types.h>\n> +#include <sys/stat.h>\n> +#include <unistd.h>\n> +\n\nNone of these #includes should be necessary (they are not for me).\nAlso, \"#include <unistd.h>\" breaks on windows ;-)\n\n>  #define GIT_PACK_NAME_MAX (5 + 40 + 1)\n>  struct git_pack {\n>  \tgit_odb *db;\n> @@ -1023,6 +1027,15 @@ int git_odb_open(git_odb **out, const char *objects_dir)\n>  \t\tfree(db);\n>  \t\treturn GIT_ERROR;\n>  \t}\n> +\tstruct stat objects_dir_stat;\n\nThis should be declared at the head of the function (or generally at the start of\na block/scope); ie we don't use this C99 facility. This breaks MSVC, which does\nnot understand C99.\n\n> +\tif (stat(db->objects_dir, &objects_dir_stat)) {\n> +\t\tfree(db);\n> +\t\treturn GIT_EOSERR;\n> +\t}\n> +\tif (!S_ISDIR(objects_dir_stat.st_mode)) {\n\nThis breaks on MSVC, since the MS headers do not define S_ISDIR. It's not difficult\nto add; it's on my TODO list. (Andreas, I can add this later if you like)\n\nHowever, I suspect it may be useful to add an gitfo_is_directory() function, or\nsomething like it. Maybe. Andreas?\n\nThank you for the patch.\n\n(I think Andreas would prefer to see libgit2 somewhere in the subject, perhaps\nlike [LIBGIT2 PATCH], otherwise he may miss you email)\n\nATB,\nRamsay Jones\n"},{"id":"127276","messageId":"200911102207.04135.kde@mosehansen.dk","threadId":"21496","inReplyTo":"4AF86A7F.1010500@ramsay1.demon.co.uk","subject":"Re: [LIBGIT2 PATCH] git_odb_open ckeck for valid path to database","fromName":"Esben Mose Hansen","fromEmail":"kde@mosehansen.dk","sentAt":"2009-11-10T21:07:04Z","receivedAt":"2009-11-10T21:07:04Z","isPatch":true,"sender":{"key":"kde@mosehansen.dk","avatar":null},"body":"On Monday 09 November 2009 20:16:15 Ramsay Jones wrote:\n\nI have made 2 new patchsets: One \n0001-git_odb_open-check-for-valid-path-to-database.patch\n\nwhich is simply the patch before with fixups, and another series\n\n0001-Add-gitfo_is_diretory.patch\n0002-git_odb_open-ckeck-for-valid-path-to-database-using-.patch\n\nwhich adds a gitfo_is_directory (0001) and then uses this new function from \ngit_odb_open (0002).\n\nI hope I have done this git/email stuff correctly.\n\n> This should be declared at the head of the function (or generally at the\n>  start of a block/scope); ie we don't use this C99 facility. This breaks\n>  MSVC, which does not understand C99.\n\nC89 (?) is a tough monster, I see :)\n\n> \n> > +\tif (stat(db->objects_dir, &objects_dir_stat)) {\n> > +\t\tfree(db);\n> > +\t\treturn GIT_EOSERR;\n> > +\t}\n> > +\tif (!S_ISDIR(objects_dir_stat.st_mode)) {\n> \n> This breaks on MSVC, since the MS headers do not define S_ISDIR. It's not\n>  difficult to add; it's on my TODO list. (Andreas, I can add this later if\n>  you like)\n\nI have not fixed this. \n\n\n> (I think Andreas would prefer to see libgit2 somewhere in the subject,\n>  perhaps like [LIBGIT2 PATCH], otherwise he may miss you email\n\nOK.\n\nThanks for looking at my code.\n\n-- \nKind regards, Esben\n\n\nFrom bb29cbb2fc42ce46bd8a53ad02d291aa833846fa Mon Sep 17 00:00:00 2001\nFrom: Esben Mose Hansen <kde@mosehansen.dk>\nDate: Fri, 6 Nov 2009 10:32:16 +0100\nSubject: [PATCH] git_odb_open check for valid path to database\n\n---\n src/errors.c                |    1 +\n src/git/common.h            |    3 +++\n src/odb.c                   |    9 +++++++++\n tests/t0204-opendb_errors.c |   38 ++++++++++++++++++++++++++++++++++++++\n 4 files changed, 51 insertions(+), 0 deletions(-)\n create mode 100644 tests/t0204-opendb_errors.c\n\ndiff --git a/src/errors.c b/src/errors.c\nindex f348997..074c01c 100644\n--- a/src/errors.c\n+++ b/src/errors.c\n@@ -36,6 +36,7 @@ static struct {\n \t{ GIT_ENOTOID, \"Not a git oid\" },\n \t{ GIT_ENOTFOUND, \"Object does not exist in the scope searched\" },\n \t{ GIT_ENOMEM, \"Not enough space\" },\n+\t{ GIT_ENOTDIR, \"Not a directory\" }\n };\n \n const char *git_strerror(int num)\ndiff --git a/src/git/common.h b/src/git/common.h\nindex c470e0e..96f2971 100644\n--- a/src/git/common.h\n+++ b/src/git/common.h\n@@ -65,6 +65,9 @@\n /** Consult the OS error information. */\n #define GIT_EOSERR (GIT_ERROR - 4)\n \n+/** The path is not a directory */\n+#define GIT_ENOTDIR (GIT_ERROR - 5)\n+\n GIT_BEGIN_DECL\n \n /** A revision traversal pool. */\ndiff --git a/src/odb.c b/src/odb.c\nindex 6d646a4..a588c1b 100644\n--- a/src/odb.c\n+++ b/src/odb.c\n@@ -1014,6 +1014,7 @@ int git_odb_exists(git_odb *db, const git_oid *id)\n \n int git_odb_open(git_odb **out, const char *objects_dir)\n {\n+\tstruct stat objects_dir_stat;\n \tgit_odb *db = git__calloc(1, sizeof(*db));\n \tif (!db)\n \t\treturn GIT_ERROR;\n@@ -1023,6 +1024,14 @@ int git_odb_open(git_odb **out, const char *objects_dir)\n \t\tfree(db);\n \t\treturn GIT_ERROR;\n \t}\n+\tif (stat(db->objects_dir, &objects_dir_stat)) {\n+\t\tfree(db);\n+\t\treturn GIT_EOSERR;\n+\t}\n+\tif (!S_ISDIR(objects_dir_stat.st_mode)) {\n+\t\tfree(db);\n+\t\treturn GIT_ENOTDIR;\n+\t}\n \n \tgitlck_init(&db->lock);\n \ndiff --git a/tests/t0204-opendb_errors.c b/tests/t0204-opendb_errors.c\nnew file mode 100644\nindex 0000000..e9b52c9\n--- /dev/null\n+++ b/tests/t0204-opendb_errors.c\n@@ -0,0 +1,38 @@\n+#include \"test_lib.h\"\n+#include \"test_helpers.h\"\n+#include <git/odb.h>\n+#include \"fileops.h\"\n+\n+static char *odb_dir = \"test-objects\";\n+\n+static unsigned char one_bytes[] = {\n+    0x31, 0x78, 0x9c, 0xe3, 0x02, 0x00, 0x00, 0x0b,\n+    0x00, 0x0b,\n+};\n+\n+static unsigned char one_data[] = {\n+    0x0a,\n+};\n+\n+static object_data one = {\n+    one_bytes,\n+    sizeof(one_bytes),\n+    \"8b137891791fe96927ad78e64b0aad7bded08bdc\",\n+    \"blob\",\n+    \"test-objects/8b\",\n+    \"test-objects/8b/137891791fe96927ad78e64b0aad7bded08bdc\",\n+    one_data,\n+    sizeof(one_data),\n+};\n+\n+BEGIN_TEST(opendb_errors)\n+    git_odb *db;\n+    int error = git_odb_open(&db, odb_dir);\n+    must_be_true(error == GIT_EOSERR);\n+    must_be_true(errno == ENOENT);\n+    must_pass(write_object_files(odb_dir, &one));\n+    error = git_odb_open(&db, one.file);\n+    must_be_true(error == GIT_ENOTDIR);\n+\n+    must_pass(remove_object_files(odb_dir, &one));\n+END_TEST\n-- \n1.6.3.3\n\n\n\nFrom d1eb250c11a35e30aaaf0652a24daa27b6b90b8f Mon Sep 17 00:00:00 2001\nFrom: Esben Mose Hansen <kde@mosehansen.dk>\nDate: Tue, 10 Nov 2009 21:58:04 +0100\nSubject: [PATCH 1/2] Add gitfo_is_diretory\n\n---\n src/errors.c     |    1 +\n src/fileops.c    |   11 +++++++++++\n src/fileops.h    |    5 +++++\n src/git/common.h |    3 +++\n 4 files changed, 20 insertions(+), 0 deletions(-)\n\ndiff --git a/src/errors.c b/src/errors.c\nindex f348997..074c01c 100644\n--- a/src/errors.c\n+++ b/src/errors.c\n@@ -36,6 +36,7 @@ static struct {\n \t{ GIT_ENOTOID, \"Not a git oid\" },\n \t{ GIT_ENOTFOUND, \"Object does not exist in the scope searched\" },\n \t{ GIT_ENOMEM, \"Not enough space\" },\n+\t{ GIT_ENOTDIR, \"Not a directory\" }\n };\n \n const char *git_strerror(int num)\ndiff --git a/src/fileops.c b/src/fileops.c\nindex 5de89cb..47a8681 100644\n--- a/src/fileops.c\n+++ b/src/fileops.c\n@@ -274,3 +274,14 @@ int gitfo_dirent(\n \tclosedir(dir);\n \treturn GIT_SUCCESS;\n }\n+\n+int gitfo_is_directory(const char* path) {\n+\tstruct stat path_stat;\n+\tif (stat(path, &path_stat)) {\n+\t\treturn GIT_EOSERR;\n+\t}\n+\tif (!S_ISDIR(path_stat.st_mode)) {\n+\t\treturn GIT_ENOTDIR;\n+\t}\n+\treturn GIT_SUCCESS;\n+}\ndiff --git a/src/fileops.h b/src/fileops.h\nindex 02e4e5b..73eb72c 100644\n--- a/src/fileops.h\n+++ b/src/fileops.h\n@@ -126,4 +126,9 @@ extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);\n extern int gitfo_flush_cached(gitfo_cache *ioc);\n extern int gitfo_close_cached(gitfo_cache *ioc);\n \n+/**\n+ * Check if path is a directory\n+ */\n+extern int gitfo_is_directory(const char* path);\n+\n #endif /* INCLUDE_fileops_h__ */\ndiff --git a/src/git/common.h b/src/git/common.h\nindex c470e0e..96f2971 100644\n--- a/src/git/common.h\n+++ b/src/git/common.h\n@@ -65,6 +65,9 @@\n /** Consult the OS error information. */\n #define GIT_EOSERR (GIT_ERROR - 4)\n \n+/** The path is not a directory */\n+#define GIT_ENOTDIR (GIT_ERROR - 5)\n+\n GIT_BEGIN_DECL\n \n /** A revision traversal pool. */\n-- \n1.6.3.3\n\n\n\nFrom 4db79e006b722291d645f05a2340cd7d26a3d777 Mon Sep 17 00:00:00 2001\nFrom: Esben Mose Hansen <kde@mosehansen.dk>\nDate: Tue, 10 Nov 2009 21:58:36 +0100\nSubject: [PATCH 2/2] git_odb_open ckeck for valid path to database using gitfo_is_directory\n\n---\n src/odb.c |    5 +++++\n 1 files changed, 5 insertions(+), 0 deletions(-)\n\ndiff --git a/src/odb.c b/src/odb.c\nindex 6d646a4..709fe58 100644\n--- a/src/odb.c\n+++ b/src/odb.c\n@@ -1014,6 +1014,7 @@ int git_odb_exists(git_odb *db, const git_oid *id)\n \n int git_odb_open(git_odb **out, const char *objects_dir)\n {\n+\tint status;\n \tgit_odb *db = git__calloc(1, sizeof(*db));\n \tif (!db)\n \t\treturn GIT_ERROR;\n@@ -1023,6 +1024,10 @@ int git_odb_open(git_odb **out, const char *objects_dir)\n \t\tfree(db);\n \t\treturn GIT_ERROR;\n \t}\n+\tif ((status = gitfo_is_directory(db->objects_dir))) {\n+\t\tfree(db);\n+\t\treturn status;\n+\t}\n \n \tgitlck_init(&db->lock);\n \n-- \n1.6.3.3\n\n"},{"id":"128756","messageId":"200911301037.53512.kde@mosehansen.dk","threadId":"21496","inReplyTo":"200911102207.04135.kde@mosehansen.dk","subject":"Re: [LIBGIT2 PATCH] git_odb_open ckeck for valid path to database","fromName":"Esben Mose Hansen","fromEmail":"kde@mosehansen.dk","sentAt":"2009-11-30T09:37:53Z","receivedAt":"2009-11-30T09:37:53Z","isPatch":true,"sender":{"key":"kde@mosehansen.dk","avatar":null},"body":"On Tuesday 10 November 2009 22:07:04 Esben Mose Hansen wrote:\n> On Monday 09 November 2009 20:16:15 Ramsay Jones wrote:\n> \n> I have made 2 new patchsets: One\n\nDid these get lost in transit? Or am I going about it in the wrong way? :)\n\nThank you for your time\n\n-- \nKind regards, Esben\n"},{"id":"129131","messageId":"4B180981.1050702@ramsay1.demon.co.uk","threadId":"21496","inReplyTo":"200911301037.53512.kde@mosehansen.dk","subject":"Re: [LIBGIT2 PATCH] git_odb_open ckeck for valid path to database","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsay1.demon.co.uk","sentAt":"2009-12-03T18:54:57Z","receivedAt":"2009-12-03T18:54:57Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"Esben Mose Hansen wrote:\n> On Tuesday 10 November 2009 22:07:04 Esben Mose Hansen wrote:\n>> On Monday 09 November 2009 20:16:15 Ramsay Jones wrote:\n>>\n>> I have made 2 new patchsets: One\n> \n> Did these get lost in transit? Or am I going about it in the wrong way? :)\n\nOh, sorry, I didn't notice that this was sent to me! *ahem*\n(I thought/expected them to be addressed to Andreas)\n\nMaybe you could resend the first patch, inline rather than as an attachment,\naddressed to Andreas (ae@op5.com) so that he can comment on them (or commit\nthem).\n\nATB,\nRamsay Jones\n"}]}