You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/07/09 16:10:17 UTC
[lucy-commits] svn commit: r1144672 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs
lib/Clownfish/Binding/Core/File.pm lib/Clownfish/Util.pm src/CFCHierarchy.c
src/CFCUtil.c src/CFCUtil.h
Author: marvin
Date: Sat Jul 9 14:10:17 2011
New Revision: 1144672
URL: http://svn.apache.org/viewvc?rev=1144672&view=rev
Log:
Add more dir manip capabilities to CFCUtil.
Add is_dir(), make_dir() and make_path() to CFCUtil.
Modified:
incubator/lucy/trunk/clownfish/lib/Clownfish.xs
incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm
incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm
incubator/lucy/trunk/clownfish/src/CFCHierarchy.c
incubator/lucy/trunk/clownfish/src/CFCUtil.c
incubator/lucy/trunk/clownfish/src/CFCUtil.h
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Sat Jul 9 14:10:17 2011
@@ -1600,6 +1600,26 @@ PPCODE:
char *content = SvPV(content_sv, len);
CFCUtil_write_if_changed(path, content, len);
+int
+is_dir(path)
+ const char *path;
+CODE:
+ RETVAL = CFCUtil_is_dir(path);
+OUTPUT: RETVAL
+
+int
+make_dir(dir)
+ const char *dir;
+CODE:
+ RETVAL = CFCUtil_make_dir(dir);
+OUTPUT: RETVAL
+
+int
+make_path(path)
+ const char *path;
+CODE:
+ RETVAL = CFCUtil_make_path(path);
+OUTPUT: RETVAL
MODULE = Clownfish PACKAGE = Clownfish::Variable
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/File.pm Sat Jul 9 14:10:17 2011
@@ -17,10 +17,9 @@ use strict;
use warnings;
package Clownfish::Binding::Core::File;
-use Clownfish::Util qw( a_isa_b verify_args );
+use Clownfish::Util qw( a_isa_b verify_args make_path is_dir );
use Clownfish::Binding::Core::Class;
use File::Spec::Functions qw( catfile splitpath );
-use File::Path qw( mkpath );
use Scalar::Util qw( blessed );
use Fcntl;
use Carp;
@@ -42,8 +41,8 @@ sub write_h {
# Unlink then open file.
my ( undef, $out_dir, undef ) = splitpath($h_path);
- mkpath $out_dir unless -d $out_dir;
- confess("Can't make dir '$out_dir'") unless -d $out_dir;
+ make_path($out_dir) unless is_dir($out_dir);
+ confess("Can't make dir '$out_dir'") unless is_dir($out_dir);
unlink $h_path;
sysopen( my $fh, $h_path, O_CREAT | O_EXCL | O_WRONLY )
or confess("Can't open '$h_path' for writing");
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Util.pm Sat Jul 9 14:10:17 2011
@@ -31,6 +31,9 @@ our @EXPORT_OK = qw(
a_isa_b
write_if_changed
trim_whitespace
+ is_dir
+ make_dir
+ make_path
);
sub verify_args {
Modified: incubator/lucy/trunk/clownfish/src/CFCHierarchy.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCHierarchy.c?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCHierarchy.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCHierarchy.c Sat Jul 9 14:10:17 2011
@@ -66,20 +66,6 @@ S_fetch_file(CFCHierarchy *self, const c
static int
S_do_propagate_modified(CFCHierarchy *self, CFCClass *klass, int modified);
-// Indicate whether a path is a directory.
-// Note: this has to be defined before including the Perl headers because they
-// redefine stat() in an incompatible way on certain systems (Windows).
-static int
-S_is_dir(const char *path) {
- struct stat stat_buf;
- int stat_check = stat(path, &stat_buf);
- if (stat_check == -1) {
- CFCUtil_die("Stat failed for '%s': %s", path,
- strerror(errno));
- }
- return (stat_buf.st_mode & S_IFDIR) ? true : false;
-}
-
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
@@ -196,7 +182,7 @@ S_find_cfh(char *dir, char **cfh_list, s
cfh_list[num_cfh++] = CFCUtil_strdup(full_path);
cfh_list[num_cfh] = NULL;
}
- else if (S_is_dir(full_path)) {
+ else if (CFCUtil_is_dir(full_path)) {
cfh_list = S_find_cfh(full_path, cfh_list, num_cfh);
num_cfh = 0;
if (cfh_list) {
Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.c?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.c Sat Jul 9 14:10:17 2011
@@ -282,6 +282,61 @@ CFCUtil_warn(const char* format, ...) {
fprintf(stderr, "\n");
}
+// Note: this has to be defined before including the Perl headers because they
+// redefine stat() in an incompatible way on certain systems (Windows).
+int
+CFCUtil_is_dir(const char *path) {
+ struct stat stat_buf;
+ int stat_check = stat(path, &stat_buf);
+ if (stat_check == -1) {
+ return false;
+ }
+ return (stat_buf.st_mode & S_IFDIR) ? true : false;
+}
+
+#ifdef WIN32
+ #define DIR_SEP_CHAR '\\'
+#else
+ #define DIR_SEP_CHAR '/'
+#endif
+
+int
+CFCUtil_make_path(const char *path) {
+ CFCUTIL_NULL_CHECK(path);
+ char *target = CFCUtil_strdup(path);
+ size_t orig_len = strlen(target);
+ size_t len = orig_len;
+ for (size_t i = 0; i <= len; i++) {
+#ifndef WIN32
+ if (target[i] == '\\') {
+ i++;
+ continue;
+ }
+#endif
+ if (target[i] == DIR_SEP_CHAR || i == len) {
+ target[i] = 0; // NULL-terminate.
+ struct stat stat_buf;
+ int stat_check = stat(target, &stat_buf);
+ if (stat_check != -1) {
+ if (!(stat_buf.st_mode & S_IFDIR)) {
+ CFCUtil_die("%s isn't a directory", target);
+ }
+ }
+ else {
+ int success = CFCUtil_make_dir(target);
+ if (!success) {
+ FREEMEM(target);
+ return false;
+ }
+ }
+ target[i] = DIR_SEP_CHAR;
+ }
+ }
+
+ FREEMEM(target);
+ return true;
+}
+
/******************************** WINDOWS **********************************/
#ifdef WIN32
@@ -294,6 +349,11 @@ typedef struct WinDH {
int first_time;
} WinDH;
+void
+CFCUtil_make_dir (const char *dir) {
+ return !mkdir(dir);
+}
+
void*
CFCUtil_opendir(const char *dir) {
size_t dirlen = strlen(dir);
@@ -348,6 +408,12 @@ CFCUtil_closedir(void *dirhandle, const
#include <dirent.h>
+int
+CFCUtil_make_dir (const char *dir) {
+ return !mkdir(dir, 0777);
+}
+
+
void*
CFCUtil_opendir(const char *dir) {
DIR *dirhandle = opendir(dir);
Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.h?rev=1144672&r1=1144671&r2=1144672&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.h Sat Jul 9 14:10:17 2011
@@ -126,6 +126,23 @@ CFCUtil_dirnext(void *dirhandle);
void
CFCUtil_closedir(void *dirhandle, const char *dir);
+/* Returns true if the supplied path is a directory, false otherwise.
+ */
+int
+CFCUtil_is_dir(const char *path);
+
+/* Create the specified directory. Returns true on success, false on failure.
+ */
+int
+CFCUtil_make_dir(const char *dir);
+
+/* Create the specified path including all subdirectories. Returns true on
+ * success, false on failure. Intermediate directories may be left behind on
+ * failure.
+ */
+int
+CFCUtil_make_path(const char *path);
+
/* Print an error message to stderr and exit.
*/
void