help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] [PATCH] Functions to create symlinks


From: Paolo Bonzini
Subject: [Help-smalltalk] [PATCH] Functions to create symlinks
Date: Mon, 11 Jun 2007 10:45:57 +0200
User-agent: Thunderbird 2.0.0.0 (Macintosh/20070326)

I added two. #symlinkFrom: creates a symlink with a given relative path, #symlinkAs: creates a symlink to a given file computing the relative path automatically.

Paolo
2007-06-11  Paolo Bonzini  <address@hidden>

        * kernel/File.st: Add #pathFrom:to: and symlink creation.
        * kernel/VFS.st: Add symlink creation.

        * libgst/cint.c: Add symlink function.

--- orig/kernel/File.st
+++ mod/kernel/File.st
@@ -151,6 +151,38 @@ fullNameFor: aString
     ^path inject: '/' into: [ :old :each |
        Directory append: each to: old
     ]
+!
+
+pathFrom: srcName to: destName
+    ^self
+       computePathFrom: (File fullNameFor: srcName)
+       to: (File fullNameFor: destName)
+! !
+
+
+!File class methodsFor: 'private'!
+
+computePathFrom: srcName to: destName
+    | src dest path |
+    src := srcName subStrings: Directory pathSeparator.
+    dest := destName subStrings: Directory pathSeparator.
+
+    src := src asOrderedCollection.
+    src removeLast.
+
+    dest := dest asOrderedCollection.
+    dest isEmpty ifTrue: [ dest addLast: '' ].
+
+    [ src isEmpty or: [
+       dest size = 1 or: [
+       src first ~= dest first ] ] ] whileFalse: [
+
+       src removeFirst.
+       dest removeFirst ].
+
+    path := src collect: [ :each | '..' ].
+    path addAllLast: dest.
+    ^path fold: [ :a :b | a, '/', b ]
 ! !
 
 
@@ -178,6 +210,17 @@ touch: fileName
     (File name: fileName) touch
 !
 
+symlink: srcName as: destName
+    "Create a symlink for the srcName file with the given path name"
+    (File name: srcName) symlinkAs: destName
+!
+
+symlink: destName from: srcName
+    "Create a symlink named destName file from the given path (relative to
+     destName)"
+    (VFS.VFSHandler for: destName) symlinkFrom: srcName
+!
+
 remove: fileName
     "Remove the file with the given path name"
     (VFS.VFSHandler for: fileName) remove
@@ -445,6 +488,30 @@ writeStream
     ^self open: FileStream write
 !
 
+symlinkAs: destName
+    "Create destName as a symbolic link of the receiver.  The appropriate
+     relative path is computed automatically."
+    | dest relPath |
+    dest := VFS.VFSHandler for: destName.
+    relPath := File
+       computePathFrom: dest realFileName
+       to: vfsHandler realFileName.
+    dest symlinkFrom: relPath
+!
+
+pathTo: destName
+    "Compute the relative path from the receiver to destName."
+    ^File
+       computePathFrom: vfsHandler realFileName
+       to: (File fullNameFor: destName)
+!
+
+symlinkFrom: srcName
+    "Create the receiver as a symbolic link from srcName (relative to the
+     path of the receiver)."
+    vfsHandler symlinkFrom: srcName
+!
+
 remove
     "Remove the file identified by the receiver"
     ^vfsHandler remove


--- orig/libgst/cint.c
+++ mod/libgst/cint.c
@@ -531,6 +531,7 @@ _gst_init_cfuncs (void)
   _gst_define_cfunc ("rewinddir", rewinddir);
   _gst_define_cfunc ("extractDirentName", extract_dirent_name);
 
+  _gst_define_cfunc ("symlink", symlink);
   _gst_define_cfunc ("unlink", unlink);
   _gst_define_cfunc ("rename", rename);
   _gst_define_cfunc ("rmdir", rmdir);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]