Permalink
Browse files

Implement FSFolder_absolutify in core

  • Loading branch information...
1 parent 296f3a9 commit 976db78939edbc02b7c6b281c94a41d5b2c52aca @nwellnhof nwellnhof committed Dec 4, 2012
@@ -1,25 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-#include "Lucy/Util/ToolSet.h"
-#include "Lucy/Store/FSFolder.h"
-
-CharBuf*
-FSFolder_absolutify(const CharBuf *path) {
- THROW(LUCY_ERR, "TODO");
- UNREACHABLE_RETURN(lucy_CharBuf*);
-}
-
View
@@ -62,6 +62,14 @@ S_create_dir(const CharBuf *path);
static bool
S_is_local_entry(const CharBuf *path);
+// Return true if the supplied path is absolute.
+static bool
+S_is_absolute(const CharBuf *path);
+
+// Transform a possibly relative path into an absolute path.
+static CharBuf*
+S_absolutify(const CharBuf *path);
+
// Create a hard link.
static bool
S_hard_link(CharBuf *from_path, CharBuf *to_path);
@@ -74,7 +82,7 @@ FSFolder_new(const CharBuf *path) {
FSFolder*
FSFolder_init(FSFolder *self, const CharBuf *path) {
- CharBuf *abs_path = FSFolder_absolutify(path);
+ CharBuf *abs_path = S_absolutify(path);
Folder_init((Folder*)self, abs_path);
DECREF(abs_path);
return self;
@@ -297,6 +305,35 @@ S_is_local_entry(const CharBuf *path) {
#include <windows.h>
static bool
+S_is_absolute(const CharBuf *path) {
+ uint32_t code_point = CharBuf_Code_Point_At(path, 0);
+
+ if (isalpha(code_point)) {
+ code_point = CharBuf_Code_Point_At(path, 1);
+ if (code_point != ':') { return false; }
+ code_point = CharBuf_Code_Point_At(path, 2);
+ }
+
+ return code_point == '\\' || code_point == '/';
+}
+
+static CharBuf*
+S_absolutify(const CharBuf *path) {
+ if (S_is_absolute(path)) { return Lucy_CB_Clone(path); }
+
+ DWORD cwd_len = GetCurrentDirectory(0, NULL);
+ char *cwd = (char*)MALLOCATE(cwd_len);
+ DWORD res = GetCurrentDirectory(cwd_len, cwd);
+ if (res == 0 || res > cwd_len) {
+ THROW(ERR, "GetCurrentDirectory failed");
+ }
+ CharBuf *abs_path = lucy_CB_newf("%s\\%o", cwd, path);
+ FREEMEM(cwd);
+
+ return abs_path;
+}
+
+static bool
S_hard_link(CharBuf *from_path, CharBuf *to_path) {
char *from8 = (char*)CB_Get_Ptr8(from_path);
char *to8 = (char*)CB_Get_Ptr8(to_path);
@@ -316,6 +353,23 @@ S_hard_link(CharBuf *from_path, CharBuf *to_path) {
#elif (defined(CHY_HAS_UNISTD_H))
static bool
+S_is_absolute(const CharBuf *path) {
+ return Lucy_CB_Starts_With_Str(path, DIR_SEP, 1);
+}
+
+static CharBuf*
+S_absolutify(const CharBuf *path) {
+ if (S_is_absolute(path)) { return Lucy_CB_Clone(path); }
+
+ char *cwd = getcwd(NULL, 0);
+ if (!cwd) { THROW(ERR, "getcwd failed"); }
+ CharBuf *abs_path = lucy_CB_newf("%s%s%o", cwd, DIR_SEP, path);
+ free(cwd);
+
+ return abs_path;
+}
+
+static bool
S_hard_link(CharBuf *from_path, CharBuf *to_path) {
char *from8 = (char*)CB_Get_Ptr8(from_path);
char *to8 = (char*)CB_Get_Ptr8(to_path);
@@ -77,11 +77,6 @@ public class Lucy::Store::FSFolder inherits Lucy::Store::Folder {
public bool
Hard_Link(FSFolder *self, const CharBuf *from, const CharBuf *to);
-
- /** Transform a relative path into an abolute path.
- */
- inert incremented CharBuf*
- absolutify(const CharBuf *path);
}
@@ -1,25 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-#include "Lucy/Util/ToolSet.h"
-#include "Lucy/Store/FSFolder.h"
-
-CharBuf*
-FSFolder_absolutify(const CharBuf *path) {
- THROW(LUCY_ERR, "TODO");
- UNREACHABLE_RETURN(lucy_CharBuf*);
-}
-
View
@@ -431,14 +431,6 @@ sub error {$Clownfish::Err::error}
}
{
- package Lucy::Store::FSFolder;
- our $VERSION = '0.003000';
- $VERSION = eval $VERSION;
- use File::Spec::Functions qw( rel2abs );
- sub _absolutify {rel2abs(shift)}
-}
-
-{
package Lucy::Store::RAMFileHandle;
our $VERSION = '0.003000';
$VERSION = eval $VERSION;
@@ -1,38 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-#include "XSBind.h"
-#include "Lucy/Store/FSFolder.h"
-
-cfish_CharBuf*
-lucy_FSFolder_absolutify(const cfish_CharBuf *path) {
- dSP;
- ENTER;
- SAVETMPS;
- EXTEND(SP, 2);
- PUSHMARK(SP);
- mPUSHs(XSBind_cb_to_sv(path));
- PUTBACK;
- call_pv("Lucy::Store::FSFolder::_absolutify", G_SCALAR);
- SPAGAIN;
- cfish_CharBuf *absolutified
- = (cfish_CharBuf*)XSBind_perl_to_cfish(POPs);
- PUTBACK;
- FREETMPS;
- LEAVE;
- return absolutified;
-}
-
@@ -1,25 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-
-#include "Lucy/Util/ToolSet.h"
-#include "Lucy/Store/FSFolder.h"
-
-CharBuf*
-FSFolder_absolutify(const CharBuf *path) {
- THROW(LUCY_ERR, "TODO");
- UNREACHABLE_RETURN(lucy_CharBuf*);
-}
-

0 comments on commit 976db78

Please sign in to comment.