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 2009/03/12 04:25:37 UTC
svn commit: r752757 - in
/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core: Dir.charm Dir.harm
OperSys.charm OperSys.harm
Author: marvin
Date: Thu Mar 12 03:25:37 2009
New Revision: 752757
URL: http://svn.apache.org/viewvc?rev=752757&view=rev
Log:
Add a Charmonizer core module for directory handling. Return a value when
running a local command via OperSys.
Added:
lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.charm
lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.harm
Modified:
lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.charm
lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.harm
Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.charm?rev=752757&view=auto
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.charm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.charm Thu Mar 12 03:25:37 2009
@@ -0,0 +1,163 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "Charmonizer/Core/Dir.h"
+
+#include "Charmonizer/Core/Compiler.h"
+#include "Charmonizer/Core/HeadCheck.h"
+#include "Charmonizer/Core/ModHandler.h"
+#include "Charmonizer/Core/OperSys.h"
+#include "Charmonizer/Core/Util.h"
+
+static chaz_bool_t mkdir_available = false;
+static chaz_bool_t rmdir_available = false;
+static chaz_bool_t initialized = false;
+int chaz_Dir_mkdir_num_args = 0;
+static char mkdir_command[7];
+char *Dir_mkdir_command = mkdir_command;
+
+/* Source code for standard POSIX mkdir */
+static char posix_mkdir_code[] = METAQUOTE
+ #include <%s>
+ int main(int argc, char **argv) {
+ if (argc != 2) { return 1; }
+ if (mkdir(argv[1], 0777) != 0) { return 2; }
+ return 0;
+ }
+METAQUOTE;
+
+/* Source code for Windows _mkdir. */
+static char win_mkdir_code[] = METAQUOTE
+ #include <direct.h>
+ int main(int argc, char **argv) {
+ if (argc != 2) { return 1; }
+ if (_mkdir(argv[1]) != 0) { return 2; }
+ return 0;
+ }
+METAQUOTE;
+
+/* Source code for rmdir. */
+static char rmdir_code[] = METAQUOTE
+ #include <%s>
+ int main(int argc, char **argv) {
+ if (argc != 2) { return 1; }
+ if (rmdir(argv[1]) != 0) { return 2; }
+ return 0;
+ }
+METAQUOTE;
+
+static chaz_bool_t
+try_init_posix_mkdir(char *header)
+{
+ size_t needed = sizeof(posix_mkdir_code) + 30;
+ char *code_buf = malloc(needed);
+
+ /* Attempt compilation. */
+ sprintf(code_buf, posix_mkdir_code, header);
+ mkdir_available = compiler->compile_exe(compiler, "_charm_mkdir.c",
+ "_charm_mkdir", code_buf, strlen(code_buf));
+
+ /* Set vars on success. */
+ if (mkdir_available) {
+ strcpy(mkdir_command, "mkdir");
+ if (strcmp(header, "direct.h") == 0) {
+ Dir_mkdir_num_args = 1;
+ }
+ else {
+ Dir_mkdir_num_args = 2;
+ }
+ }
+
+ free(code_buf);
+ return mkdir_available;
+}
+
+static chaz_bool_t
+try_init_win_mkdir()
+{
+ mkdir_available = compiler->compile_exe(compiler, "_charm_mkdir.c",
+ "_charm_mkdir", win_mkdir_code, strlen(win_mkdir_code));
+ if (mkdir_available) {
+ strcpy(mkdir_command, "_mkdir");
+ Dir_mkdir_num_args = 1;
+ }
+ return mkdir_available;
+}
+
+static void
+init_mkdir()
+{
+ if (verbosity) {
+ printf("Attempting to compile _charm_mkdir utility...\n");
+ }
+ if (try_init_win_mkdir()) { return; }
+ if (try_init_posix_mkdir("sys/stat.h")) { return; }
+ if (try_init_posix_mkdir("direct.h")) { return; }
+}
+
+static chaz_bool_t
+try_init_rmdir(char *header)
+{
+ size_t needed = sizeof(posix_mkdir_code) + 30;
+ char *code_buf = malloc(needed);
+ sprintf(code_buf, rmdir_code, header);
+ rmdir_available = compiler->compile_exe(compiler, "_charm_rmdir.c",
+ "_charm_rmdir", code_buf, strlen(code_buf));
+ free(code_buf);
+ return rmdir_available;
+}
+
+static void
+init_rmdir()
+{
+ if (verbosity) {
+ printf("Attempting to compile _charm_rmdir utility...\n");
+ }
+ if (try_init_rmdir("unistd.h")) { return; }
+ if (try_init_rmdir("dirent.h")) { return; }
+ if (try_init_rmdir("direct.h")) { return; }
+}
+
+/* Compile _charm_mkdir and _charm_rmdir. */
+void
+chaz_Dir_init(void)
+{
+ if (!initialized) {
+ initialized = true;
+ init_mkdir();
+ init_rmdir();
+ }
+}
+
+chaz_bool_t
+chaz_Dir_mkdir(const char *filepath)
+{
+ if (!initialized) { chaz_Dir_init(); }
+ return os->run_local(os, "_charm_mkdir ", filepath, NULL);
+}
+
+chaz_bool_t
+chaz_Dir_rmdir(const char *filepath)
+{
+ if (!initialized) { chaz_Dir_init(); }
+ return os->run_local(os, "_charm_rmdir ", filepath, NULL);
+}
+
+/**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.harm?rev=752757&view=auto
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.harm (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/Dir.harm Thu Mar 12 03:25:37 2009
@@ -0,0 +1,57 @@
+/* Charmonizer/Core/Dir.h - Directory manipulation routines.
+ *
+ * Compile utilities to create and remove directories.
+ */
+
+#ifndef H_CHAZ_DIR
+#define H_CHAZ_DIR
+
+#include "Charmonizer/Core/Defines.h"
+
+/* Compile the utilities which execute the Dir_mkdir and Dir_rmdir functions. */
+void
+chaz_Dir_init(void);
+
+/* Attempt to create a directory. Returns true on success, false on failure.
+ */
+chaz_bool_t
+chaz_Dir_mkdir(const char *filepath);
+
+/* Attempt to remove a directory, which must be empty. Returns true on
+ * success, false on failure.
+ */
+chaz_bool_t
+chaz_Dir_rmdir(const char *filepath);
+
+/* The string command for mkdir. */
+extern char* chaz_Dir_mkdir_command;
+
+/* Indicate whether the mkdir takes 1 or 2 args. */
+extern int chaz_Dir_mkdir_num_args;
+
+#ifdef CHAZ_USE_SHORT_NAMES
+ #define Dir_init chaz_Dir_init
+ #define Dir_mkdir chaz_Dir_mkdir
+ #define Dir_rmdir chaz_Dir_rmdir
+ #define Dir_mkdir_command chaz_Dir_mkdir_command
+ #define Dir_mkdir_num_args chaz_Dir_mkdir_num_args
+#endif
+
+#endif /* H_CHAZ_DIR */
+
+/**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.charm?rev=752757&r1=752756&r2=752757&view=diff
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.charm Thu Mar 12 03:25:37 2009
@@ -19,7 +19,7 @@
static void
remove_obj(OperSys *self, char *name);
-static void
+static int
run_local(OperSys *self, ...);
chaz_OperSys*
@@ -111,7 +111,7 @@
remove(self->buf);
}
-static void
+static int
run_local(OperSys *self, ...)
{
va_list args;
@@ -126,7 +126,7 @@
va_end(args);
/* run the command */
- system(self->buf);
+ return system(self->buf);
}
Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.harm?rev=752757&r1=752756&r2=752757&view=diff
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.harm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core/OperSys.harm Thu Mar 12 03:25:37 2009
@@ -20,7 +20,7 @@
/* Concatenate all arguments in a NULL-terminated list into a single command
* string, prepend the appropriate prefix, and invoke via system().
*/
-typedef void
+typedef int
(*chaz_OS_run_local_t)(chaz_OperSys *self, ...);
/* Destructor.