Thursday, 19 November 2015

Symbolic Link

Hi Friends,

Today I am back with very small thing but I was not aware and faced some problem with that. So I am here with the thought that this might help someone. 

Did you heard about “Symbolic Link”?

I did not heard about it earlier but found a requirement in one of the project as per suggestion from my coordinator. Then I started exploring it and found that in Windows environment it is just like a shortcut which points to another location. 

But later i found that there is some differences in Symbolic link and shortcuts. Shortcuts need a specific application to make it working which is supported by windows like operating system. But in case of Linux/Unix based system this is not working (not declaring this but found such in initial phase). 

Shortcut is just a file which keep information of target file or directory with additional information like icon etc. But very important fact is that Shortcut is a file and not behaves as a directory to navigate to targeting location. Here symbolic link comes into existence in my requirement.

Definition:- In computing, a symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects path name resolution.

So in my words, Symbolic link is a bridge between two directories (or directory to file) which are linked through a symbolic link file. This symbolic link file provides a door to navigate directly to the targeting location and behaves like an actual directory/sub directory which can be used/referenced in path while saving or accessing file.

Now point is how to create symbolic link, so below are the steps to create symbolic link.

Windows Environment :- In windows environment, symbolic link creation is easy through command prompt.
Step 1 :- Open command prompt with run as administrator option.
Step 2:- Use mklink command to create symbolic link as given below 

MKLINK [[/D] | [/H] | [/J]] Link Target

/D      Creates a directory symbolic link.  Default is a file
symbolic link.
/H      Creates a hard link instead of a symbolic link.
/J      Creates a Directory Junction.
Link    specifies the new symbolic link name.
Target  specifies the path (relative or absolute) that the new link
refers to.

For ex. Suppose we need to create a symbolic link named sftp from the root directory to the \Users\User1\data directory, then type:

mklink /D \sftp \Users\User1\data

This will create a symbolic link “sftp” at root which will navigate user to “\Users\User1\data” directory when opened.

2. Linux environment :- In Linux/Unix based environment “ln” command is used to create link. With “-s” parameter it creates a symbolic/soft link. Below is “ln” command to create a symbolic link.

ln -s [source_dir] [Link_Name]

For ex. Suppose we need to create a soft link named as “sftp” at root which should point to “/var/opt/data”. Then:

ln -s /var/opt/data /sftp  

This will create a soft link “sftp”which will point to “/var/opt/data”

To remove existing symbolic link navigate to the directory where symbolic link exists and then execute below command.

rm [Link Name]

For Ex. If “sftp” symbolic link exist already then we can remove it through below command

rm sftp

Now below are few important points while creating or removing links.
1. Create specific type of link as per need (i.e Soft or Hard)

2. By removing soft link it just removes soft link file and actual linked file or directory is safe.

3. By removing a link which is hard link and created with /h or -h option then it actually represents target file or directory and removing such link may remove actual linked file or directory as well. So be careful with it.

4. If you have created a soft link “sftp”at “/tmp/sftp” then you can use it to create new file or directory at targeting location. For ex. If sftp pointing to “/var/opt/data”then you can try to create a directory at “/tmp/sftp/dev-data”which will create “dev-data” directory at targeting location that means at “/var/opt/data/dev-data”

5. Similarly you can access a file from targeting location using this soft link like “/tmp/sftp/test.html”where “test.html” is placed at linked location (i.e. “/var/opt/data/test.html”)

I hope it will give you some idea about symlink/symbolic link or soft link and about their use and you can start thinking where you can gain an advantage from this.

So keep exploring and share your thoughts and findings.

References

https://kb.iu.edu/d/abbe

Monday, 1 December 2014

Basic Git Operations With GIT GUI for Windows User



Recently I started working on a project which is using GIT as its repository for version controlling. Previously I worked with SVN, CVS but not yet got a chance to work with GIT and was really excited to work with it. But later on I came to know that most of the operations done in GIT are done using commands which most of the window users not found very convenient and so I am.

Anyways, I found a window based utility called GIT GUI for basic operation with GIT repository like download contents from GIT repository, Get latest version, commit your changes to repository etc. I faced some problem in using it with available help for basic operation and concluded something to make its use simple. 

So Today, I am sharing steps in very simple way for basic operation with GIT GUI These steps might be really helpful to you guys if you are just starting working with GIT.

To access GIT repository we needs following mandatory things.
1. GIT GUI tool - Click Here to get it now
2. GIT based repository root location
3. Credentials to access GIT repository

Assuming you have already downloaded and installed GIT - GUI on your windows machine. Now we are going to discuss following basic operations.

1. Clone Repository - This operation creates a clone of actual repository (Actual repository resides on remote server) on your local machine. This clone on your local machine will be your working copy. Below are the steps to create clone of a repository.
For Ex - Assume we have GIT repository as below to work with

Step 1 - Right click in a directory where you want to download contents from GIT repository and select Git GUI here from context menu.





Step 2 - Click on Clone Existing Repository link



Step 3 - Enter remote repository location in source 
Enter path of directory in target to create local copy of repository on your machine and click on Clone” button. 



It will download content of repository in specified path on your local machine. Now you are ready to work with content code or anything.  

2. Pull latest changes - Pull operation is used to get latest changes from remote repository to your local repository or you can say that pull command is used to sync your local repository with remote repository. Steps are given below.

Step 1 - Right click directory where you created your local repository using Clone operation and select Git GUI here from context menu. GIT GUI will be open and pointing to your local repository as below. This window has various option to perform various operation with GIT repository but we are just going to cover most basic operations that are needed at beginner level. 



Step 2 - Select Remote --> Fetch from --> origin
This will fetch (pull) all latest changes from remote repository to your local repository from head/root level, that means all latest changes committed in repository will be in your local repository as well now.



Note:- There is also option to pull specif changes as well if you don’t want to refresh complete copy for any reason.

3. Commit Changes to local repository : - Once we have created our local repository and taken latest changes than we are ready to add or modify files in repository. Assuming you have made some changes in main.txt file as available in your local repository. Below are the steps to scan your modified files and commit them in your local repository.

Step 1 - Select your local repository folder and right click and select Git GUI here. This will open GIT GUI utility.



Step 2 - All modified files will appear in left upper box (titled with Unstaged changes”). You can Rescan for modified files if GUI was not already open and not showing your modified files. Tool will show you contents of selected filed in right upper box and displays your modification in content with green (for addition) and red (for deletion).

Step 3 - Select specific file which you want to commit in repository and click on file icon in left of file name from “Unstaged changes” box. This will move your file to lower left box (titled with  “Staged Changes (Will Commit)” )


Step 4 - Enter commit message in right lower box for this specific commit and click on “Commit” button. This will commit your changes to local repository.



Note:- There is option available to add specific line to commit from specific file through selecting file in unstaged box and content will appear in right upper box. Now select specific line and right click, context menu option will appear to stage changes and unstage changes. All staged changes will commit to your local repository.


4. Push changes to remote repository:- Now after commit what else? Here comes the twist in case of GIT repository. As we have created a clone of repository and that is on our local machine only. So this local repository is maintaining version only for our changes to not to lost your changes. But these changes are still not uploaded to remote repository. 
There is Push operation which is used to push your committed changes to remote.   repository. Below are the steps to push changes to remote location. 

Step 1:- Click on “Push” button which will open “Push” window and titled with “Push Branches”


Step 2:- Select master in source branches and enter your remote GIT repository location (As used earlier during cloning of repository) in Arbitrary Location text box. Various options available but not select any check box for now and click on “Push” button. Screen shown above.

Step 3:- Provide the credentials (Username/Password) for repository and click on OK button.



Success screen will appear if your changes successfully pushed to remote repository.



Important points

1. Keep backup of your changes initially for few days until you got confident with the tool.
2. Always get latest changes from remote repository before pushing your changes to it
3. Avoid committing complete branch until you are sure to commit
4. If there is any conflict in file while you taking latest changes resolve them first and commit changes to you local repository and then push your repository changes to remote repository.

Now you are good to go with very basic operations to play with GIT repository. There is lots of other options available in GIT GUI utility that you can explore further.

A good article for further detailed operations with GIT GUI  is here to explore more. Till than keep exploring.

Wednesday, 11 June 2014

SFTP upload with .Net Application


Today I was working on a utility which need to upload data on FTP. I have implemented code to upload on FTP using FTP web request. But end of the day when I finally deployed the code, I received the details of SFTP account with host, username, password and port. When I configured these details in utility the utility has failed to upload file on FTP.

Then I started researching reason of failure and found that .Net don’t support secured FTP i. e. SFTP. After searching over net I found that we need to use third party library to upload files on SFTP while using with .Net. There are many third party libraries available for SFTP, two of them are given below that could be used for this purpose.

         1.      Rebex SFTP
         2.      SharpSSH

SharpSSH is open source library and published with BSD License. I have decided to use this one for SFTP functionality implementation.

SharpSSH is a pure .NET implementation of the SSH2 client protocol suite. It provides an API for communication with SSH servers and could be integrated into any .NET application.

I am just going to discuss quick integration of this library to implement SFTP functionality in your application. You can download source code or DLLs for SharpSSH here.

Sharp SSH is actually using some java library and commands to support SFTP which is covered in a wrapper to work with .Net.

You will get following three DLLs in download
          1.      Tamir.SharpSSH.dll
          2.      Org.Mentalis.Security.dll
          3.      DiffieHellman.dll

You need to add reference to Tamir.SharpSSH while two other DLLs should also be referenced or added in bin. Org.Mentalis.Security dll is used for encryption. Now let’s come directly to the small piece of code which is doing SFTP here.

//Include namespace for Tamir.SharpSSH and System.IO as below.
using Tamir.SharpSsh;
using System.IO;

//Collect SFTP account details and root directory to upload file in.
string m_FTPServerAddress = "x.x.x.x"; // this is host IP or host domain name
string m_FTPUser = "username"; // this is username as needed to access SFTP
string m_FTPPassword = "Password"; // this is password as needed to access SFTP
string m_FTPPort = "Port"; //this is port number which is configured in SFTP for communucation.

string m_RemoteFTPFolder = "/RootFolderName/"; // this is root folder name as configured in SFTP.

string filePathAndName = "File path"; //This is cmplete file name with physical path.

//Create object for SFTP class as in Tamir.SharpSSH dll.
Sftp scp = new Sftp(m_FTPServerAddress, m_FTPUser, m_FTPPassword);

//Connect to SFTP using port as provided.
scp.Connect(m_FTPPort.ToIntSafe());

//Put/upload file on SFTP from source location to destination location in SFTP root directory.
scp.Put(filePathAndName, m_RemoteFTPFolder + Path.GetFileName(filePathAndName));

//Close connection from SFTP after uploading.
scp.Close();

Now refer above code and notice three magical lines which are doing actual job here as below.
1.      Following line of code creates an object of a SFTP class as in Tamir.SharpSSH using available ftp server address which is host ip/name, SFTP user name and SFTP password.

Sftp scp = new Sftp(m_FTPServerAddress, m_FTPUser, m_FTPPassword);

2.      Following line connects and creates a communication channel between application and SFTP host using specific post as provided in “m_FTPPort” variable.

scp.Connect(m_FTPPort.ToIntSafe());

3.      Following line of code uploads file from source path to destination path on Sftp root.

//scp.Put(source_file, destination_file);
scp.Put(filePathAndName, m_RemoteFTPFolder + Path.GetFileName(filePathAndName));

4.      Following line of code finally closes connection and communication channel from SFTP host.

scp.Close();

The code as explained above is tested and working properly. While I was trying to implement this code I came with an exception with no information in it. But after researching and trying I come to know that I was giving just file name in destination file as specified in point 3. While root folder of SFTP was needed as prefixed with file name to successfully upload file on SFTP server.

I just mentioned above point so that no one other should stuck in such a small problem.
Now enjoy SFTP upload just like normal FTP upload.