Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Changing Remote File Permissions - Part 2

Page 1  Page 2  Page 3

How do we define the permissions?

Before we add the form to modify the permissions, we need to first establish how we want to define the permissions. Recall from Figure 1 that the remote listing displayed the file permissions as a string in a 9-bit pattern, such as -rwxr-xr-x. I purposely did not include the most significant bit, which applies to directories, pipes, etc., (file types) because we won't be using this bit in this article.

Unix permissions specify what the owner can do, what the owner group can do, and what everybody else can do with the file. For each of these relationships we need three bits to define the permissions, the first to denote read (r) access, the second to denote write (w) access and the third to denote execute (x) access. Since we have three relationships, owner, group, and all, we need a triplet for each, resulting in nine bits.

Since we are using the chmod command, we need to know the syntax. It is,

chmod [options] mode file(s)

For the purposes of this article, we will not use the [options] (symbolic mode) portion of the command.

Every mode has a corresponding code number represented as three digits (Technically, it is four octal numbers, but we will not address the first octal). You may have seen the chmod command used like this, chmod 755 someFile. This simply means that each one of the three digits corresponds to one of the three triplets. For each triplet, a value is assigned for each bit. These values are, 4 for read, 2 for write, and 1 for execute. a dash (-) means no value, or zero (0). To enable a bit you add the value in that position and to clear a bit you add zero (0). Lets use an example to see how this works.

In Figure 1, the Welcome file attributes are displayed as -rw-r--r--. To calculate the appropriate mode, simply add the values for the bit positions, like so:

owner: rw-  =  4 + 2 + 0  =  6
group: r--  =  4 + 0 + 0  =  4
other: r--  =  4 + 0 + 0  =  4


The resulting permissions for the Welcome file are, read+write (6) for user, read (4) for group, and read (4) for other, or mode 644. This is the format we will use when creating a user interface to modify remote file permissions.


Adding our permission form

You will need to add a new Windows Form called FormPermissions.cs to the example project. On this form we'll add a checkbox control for each bit position for each user, creating a grid layout. We use a checkbox so that we can set and clear the appropriate values giving us a visual indication of the permissions. I've also included a Permission Mode textbox to display the mode number calculation results of our permission selections.

Figure 2 shows the resulting layout for our permission form.

The remote file list view allows us to select more than one file. We want this feature to apply to setting our permissions as well. The problem occurs when one file has a set of permissions that are not the same as another file. To indicate this, each checkbox should have the three-state property set to true, so that we can indicate which bits are different for each file. To see an example of how this will work, use Windows Explorer to view the properties of two files with different read-only properties. Note that when you override the displayed value, say clearing the read-only attribute, that change is applied to both files. This is how our permission form will function.

Now we need a means to display our permission form. Since we are changing permissions only for remote files, it makes sense to add this function to the popup menu for the remote file list view. To do so, add the Permissions... entry to the MenuRemote context menu on FormMain.cs and name it MenuRemote_Permissions.

Figure 3 shows the permission entry to the popup menu.

Now we need to add the code to the remote menu popup event to enable or disable the permissions option. The permissions option should be enabled when one or more items in the remote file list view are selected, otherwise it should be disabled. The following highlighted code shows the necessary entries.

private void MenuRemote_Popup(object sender, System.EventArgs e) {
   ICollection rc = new ListView.SelectedListViewItemCollection(listViewRemote);
   if (rc.Count == 0) {
      MenuRemote_Download.Enabled=false;
      MenuRemote_MakeDir.Enabled=true;
      MenuRemote_Rename.Enabled=false;
      MenuRemote_Delete.Enabled=false;
MenuRemote_Permissions.Enabled=false;
MenuRemote_Refresh.Enabled=true; }else if (rc.Count > 1) { MenuRemote_Download.Enabled=true; MenuRemote_MakeDir.Enabled=false; MenuRemote_Rename.Enabled=false; MenuRemote_Delete.Enabled=true;
MenuRemote_Permissions.Enabled=true;
MenuRemote_Refresh.Enabled=true; }else{ MenuRemote_Download.Enabled=true; MenuRemote_MakeDir.Enabled=false; MenuRemote_Rename.Enabled=true; MenuRemote_Delete.Enabled=true;
MenuRemote_Permissions.Enabled=true;
MenuRemote_Refresh.Enabled=true; } }


Once the permissions option is defined, we can now add the code to show our permissions form in the event handler.

private void MenuRemote_Permissions_Click(object sender, System.EventArgs e) {
   string newPermissions = "";
   SortedList remoteList = new SortedList();
   ICollection cp = new ListView.SelectedListViewItemCollection(listViewRemote);
   IEnumerator ep = cp.GetEnumerator();
   FormPermissions frmPerms = new FormPermissions();

   while ( ep.MoveNext() ) {
      ListViewItem epFile = (ListViewItem) ep.Current;
      remoteList.Add( epFile.Text, epFile.SubItems[1].Text.Substring(1) );
   }
   // pass the collection to the permission form
   if ((newPermissions = frmPerms.ShowModal(remoteList)) != "") {
      try {
         ep.Reset();
         while ( ep.MoveNext() ) {
            ListViewItem epFile = (ListViewItem) ep.Current;
           myFtp.IssueCommand("SITE chmod " + newPermissions + " " + epFile.Text);
         }
      } catch (Exception ex) {
         MessageBox.Show(this, ex.ToString(),"Permissions Error");
      }
   }
}


The event handler will take the return value (permission mode) from our permissions form and apply that value to each of the selected files.

The final step is to add the code to our permissions form.


Page 1  Page 2  Page 3