Tuesday, December 25, 2012

Change File Permission via ftpd

方案一,上傳檔案才chmod修改檔案權限
In Windows command line:
ftp> chmod
Invalid command.
Do it by "quote" command:
ftp> quote
Command line to send site chmod 666 filename.txt
200 CHMOD command successful.
缺點:
FTP裡下chmod指令無法使用萬用字元(*, wildcard),也無法使用 -R flag。
好不容易知道怎麼透過FTP下chmod了... 不實用

Using Apache Commons Net 3.2 API in java:
   1:  //using public int sendCommand(String command, String args)
   2:  //實在不知道那些字要放在command, 哪些字要放在args, try了好幾次
   3:  //TEST#1:
   4:  FTPClient.sendCommand("quote", "site chmod 666 *");
   5:  //command result=500 'QUOTE site chmod 666 *': command not understood.
   6:   
   7:  //TEST#2:
   8:  FTPClient.sendCommand("quote site chmod", "666 *");
   9:  //command result=500 'QUOTE site chmod 666 *': command not understood.
.....試了N百個組合都不認得, go die
還有error message回傳的指令都寫大寫也很奇怪.. unix大小寫有差啊,我要小寫!!

最後讀了public int sendCommand(int command, String args)方法才知道他根本就沒提供quote這個指令OTZ||| 從FTPCommand看起來,"SITE"是最有可能會動的指令:
   1:  //TEST#3:
   2:  FTPClient.sendCommand("site", "chmod 666 *");
   3:  //command result=550 *: A file or directory in the path name does not exist.
........ 透過程式來做也一樣,*不能用,再度遭受打擊OTZ|||
   1:  //TEST#4:
   2:  FTPClient.sendCommand("site", "chmod 666 filename.txt");
   3:  //command result=200 CHMOD command successful.
一次只能改一個檔案也太讓人不爽了。所以先上傳檔案才修改檔案權限的方案透過FTP不work。

方案二,登入FTP後先下umask指令來控制檔案權限
In Windows command line:
ftp> quote
Command line to send site umask 000
200 UMASK set to 000 (was 007)

Using Apache Commons Net 3.2 API in java:
   1:  //TEST#5:
   2:  FTPClient.sendCommand("site", "umask 002");
   3:  //command result=200 UMASK set to 002 (was 007)
特別提醒透過下umask方法目前測起來只對FTP有用,SFTP應該無效
又是另一項作業..

Ref:

  1. [API] FTP.sendCommand(String, String)
  2. [API] FTPCommand class
Wednesday, January 04, 2012

Using Editable JComboBox with KeyEvent

//The following code wont’t work
JComboBox jcb1 = new JComboBox();
jcb1.setEditable(true);
jcb1.addKeyListener(aKeyEventObject);

//Correct: KeyEvent should be added to JComboBox.getEditor().getEditorComponent()
JComboBox jcb2 = new JComboBox();
jcb2.setEditable(true);
jcb2.getEditor().getEditorComponent().addListerer(aKeyEventObject);

Calling to JComboBox.getEditor().getEditorComponent() will return a JTextField that is used for editing the comboBox’s text.

And if at the same moment you want to get the filling text:

//The following code won’t work(return null)
String s = jcb2.getSelectedItem().toString();

//Correct:
String s = ((JTextComponent)jcb.getEditor().getEditorComponent()).getText();

Thursday, June 30, 2011

Transfer text file between Windows and Linux

最近遇到的問題,普通的.txt檔案在windows上跟在linux上看起來沒什麼差別,但是事實上如果用HEX mode或是用Notepad++顯示所有字元,就會注意到差別! 換行字元的差別!!

ASCII字元表裡的10( LF , line feed), 13(CR, carriage return)都有換行的作用,看到一篇文章裡有稍微解說:
Mac OS X開始是用LF,較舊的版本是用CR
Unix系統下,是用LF 
愛搞怪的窗戶是用CRLF...

當我透過FTP把檔案從windows丟到linux上之後,每行資料最後的換行符號都從CRLF變成LF,整個檔案的長度就變了... 影響了程式執行的結果。於是就查了要怎麼避免這個行為,還滿簡單的,不過不知道就沒轍。

只要把FTP的傳輸模式改為binary mode即可。用command line做FTP的可以參考reference 2號,用UI的話,我是用fileZilla,在選單"傳輸>傳輸型態”裡可以選擇。就這樣,再傳一次,果然,換行符號就不會因不同系統而改變了。

ref:

  1. How to Transfer Text Files Between Linux, Macintosh, and Microsoft Windows Operating Systems
  2. [中正大學電算中心] 第五章 檔案傳輸協定-FTP
Thursday, June 23, 2011

HTC Wildfire2.2 root and apply new rom

昨天玩了一個下午,試了好多方法,終於把上到官方2.2版本的wildfire刷了新的rom。其中最大的障礙在取得手機的root權限,網路上多篇文章都說unrevoked3已經支援wildfire2.2、教學文章寫的跟真的一樣(ref.#1),但是我實際試了之後,並沒有成功。後來找到一篇滿冷門的文章(ref.#3 Wildfire 2.2 root tutorual),雖然還是使用beta版的工具,但是豁出去給他試了一下,竟然還成了。

前置作業
因為在用了對的方法前,都是偏向使用unrevoked3的方法來取得root權限,也照著其他人的教學(ref.#2)做了一些前置作業,所以事後有點難判斷究竟是不是必要的,不過以下幾點大概是一定要做的:

  1. 電腦上曾安裝HTC Sync,須把它移除。若不曾安裝,也需要先安裝再移除。這主要是為了取得一些HTC Driver。所以在安裝了HTC Sync後,開啟"控制台 > 新增或移除程式",找到"HTC Sync",將他移除(如下圖),保留其餘兩個。
    remove HTC Sync
  2. 手機設定: Setting > Applications > Development > USB debugging (勾選,直到整個刷rom過程結束前,都不必改回來)
  3. 手機設定: Setting > Connect to PC > Default connection type > HTC Syne (勾選)
  4. 手機設定: Setting > Connect to PC > Ask me (取消勾選)
  5. 取得unrevoked USB Driver並安裝,詳細安裝步驟請看ref.#5 Windows HBOOT driver installation instructions
  6. 手機序號: Setting > About phone 內有serial number資訊,須記下
  7. HBOOT Version: 在手機關機的狀態下,按住音量小聲鍵+電源鍵(進入bootloader),會進入如下畫面,便可看見HBOOT版本資訊,須記下
    hboot version
  8. AlphaRevX beta網站下載for windows的軟體,同時他會出現Key generate畫面,這邊需要輸入在上面兩個步驟取得的serinal number與HBOOT version,取得key之後記得記下來
    image

S-OFF using AlphaRevX
此步驟完成了bootloader訊息會由S-ON變為S-OFF,有關S-OFF請見ref.#6 簡易將HTC Desire S-off 教學

  1. 將下載來的alpharevx.exe.zip解壓縮後,執行alpharevx.exe
    image
  2. 此時再將手機接上電腦,會提示你輸入前置作業步驟8所取得的beta application key
  3. 輸入後此程式會繼續作業,直到提示完成。這時候就可以把手機取下,關機。
  4. 按住音量小聲鍵+電源鍵(進入bootloader),檢視S-OFF是否成功,應該會變成如下圖
    image 

Clockwork recovery

  1. HTC Developer center下載Fastboot Tool(window版本請在網頁搜尋"Fastboot binary, Windows",比較好找到載點)
  2. 把下載回來的fastboot.exe放入S-OFF using AlphaRevX第一步驟中解壓縮的資料夾內,為了方便操作,將整個資料夾copy到C槽底下,並改名為"wildfire" (當然若你很熟悉command line的指令,可以不要這麼做)
  3. 這裡下載Wildfire_recovery-clockwork-3.2.0.0-j_r0dd_mod.zip (這個連結我也是從參考的文章內直接使用的,所以無法跟各位保證他的來源,不過我自己也用了就是。xdadevelopers這裡有提供r較新的版本的連結)
  4. 將解壓縮出來的檔案改名為wildfire.img,一起放入上述資料夾(本例是c槽下的wildfire)
  5. 確定你已經做了前置作業步驟5的usb driver安裝後,將手機開至bootloader(音量小聲鍵+電源鍵),接上電腦
  6. 開啟command line,依序輸入這兩個指令
    cd c:/wildfire
    fastboot flash recovery wildfire.img
  7. 完成後,可以將手機拿下,開至bootloader,並進入recovery模式,應該可看見如下選單(bootloader內用音量大小來做選單的上下移動、電源鍵為enter。clockworkMod Recovery的選單一樣用音量大小來做上下移動,但是以按下光學滑鼠當enter,電源鍵是回首頁或上一層… 我一直到後來才發現這件事,還一直以為裝壞了...)
    clockworkmod recovery menu

ROOT
接下來終於要進入root~(重頭戲的1/2)!! ref.#3的教學內提供了兩個方法,我只用其中一個:

  1. 下載GingerBreak-v1.20.apk 放到手機裡
  2. 在手機上用任何一款file manager安裝他,並執行
  3. 接著就是無盡的等待~~ 少說要15分鐘,最後你會看到一個聲稱不知道發生什麼錯誤的訊息,並要求你手動關機再重開,重開後,檢查一下你的程式集,應該就有superUser囉~

Apply new ROM
重頭戲2/2!!! 我個人是使用CyanogenMod7,xda這裡有介紹許多其他的rom,任君挑選。

  1. 選好要安裝的ROM(通常是zip檔,不要解壓縮),下載來並放入SD卡,改名為update.zip
  2. 到market裡下載”ROM Manager” 並安裝 (我只用free版)
  3. 開啟ROM Manager,首先更新Recovery(刷入ClockworkMod Recovery/Flash ClockworkMod Recovery)
  4. 選擇Install ROM from SD Card > 選到步驟1的update.zip
  5. ROM Manager應該會有要求取得root的動作,確定之後,就開始刷rom啦~~ 又是一陣等待
  6. 第一次重開機會需要好一些時間,然後就開開心心的用新系統八~

GOOD LUCK!!!!

後記1號:
CyanogenMod7因為license問題,預設不會包含google的所有服務(就是market, map, mail等的,這怎麼成!!) 所以需要參考ref.#8 Latest Version/Google Apps 取得google App,一樣是zip檔,請放入SD卡,開至bootloader > recovry > install zip from sd card處理(請記得clockworkMod Recovery選單內以光學滑鼠當enter)

後記2號:
裝google map的時候,才剛開始download,馬上就終止了。所以找到論壇上一篇文章說明,可能是download cache太小所導致(詳見ref.#9)。所以有人將download cache的位置改指到一個建在SD內的資料夾下,馬上就可以解決這個問題了!! 請開啟app內的terminal emulator(模擬終端),依序輸入以下指令:

mkdir /mnt/sdcard/market-download-cache
su
cd /cache
mv download download.bak
ln -s /mnt/sdcard/market-download-cache download
ls -ahl

由最後一個指令可看出/cache/download 改指到/mnt/sdcard/market-download-cache了,重新下載安裝google map,成功。

 

ref:

  1. [教程] Android 2.2系统机型一键root教程 (doesn’t work)
  2. 使用unrevoked 3將官方Desire 2.1, 2.2刷入第三方recovery image(含root) (適用desire)
  3. Wildfire 2.2 root tutorial (work!)
  4. AlphaRevX Beta
  5. Windows HBOOT driver installation instructions
  6. 簡易將HTC Desire S-off 教學
  7. CyanogenMod Wiki
  8. CyanogenMod Wiki: Latest Version/Google Apps
  9. [forums] I can't download anything bigger than 5M from the markets
Tuesday, January 18, 2011

Roundcube auto-reply plugin: Vacation

Evnironment:
OS: FreeBSD 8.1-RELEASE-p1
roundcube: 0.4.2
vacation: 1.9.9
apache: 1.3.42
php: 5.3.3_2
  1. 前提: roundcube已正常運作。也就是apache, php等基本配件已經處理好了。
  2. 下載Vacation plugin,並解壓縮至/roundcube/plugins/內
    (/usr/local/www/roundcube/plugins/)
  3. 先讀過README與INSTALL兩個檔案,並決定要用哪種driver讓auto-reply的功能運作(這邊會以SSHFTP為例)。
  4. 啟用plugin: 修改/usr/local/www/roundcube/config/main.inc.php檔
    找到$rcmail_config['plugins'] 這行,並加入'vacation'

    // List of active plugins (in plugins/ directory)
    $rcmail_config['plugins'] = array('other_plugins','vacation');

  5. 修改/vacation/config.ini檔,指定driver

    [default]
    driver = "sshftp"
    subject = "Default subject"
    body = "default.txt"
    server = host_name

  6. 修改/vacation/config.ini檔的擁有者 與 執行權限
    # chown $apache_user config.ini
    # chmod 0400 confing.ini

    到目前為止,2-6點是INSTALL.txt內也有列出的幾項工作。
    照著做完之後,在roundcube設定內,就可以看見"休假"的頁籤了。

  7. 不過你可能會遇到點下"休假"的頁籤後畫面就錯誤的狀況,這時候去檢查/roundcube/logs/errors時,可能會有以下錯誤訊息:
    PHP Fatal error: 
    Call to undefined function ssh2_connect() in …/roundcube/plugins/vacation/lib/sshftp.class.php on line ...
  8. 因為要使用PHP的SSH2系列功能時,系統需要有OpenSSL, libssh2, pecl-ssh2三個套件,可從ports安裝,libssh2與pecl-ssh2的位置分別在:
    libssh2: /usr/ports/security/libssh2
    pecl-ssh2:  /usr/ports/security/pecl-ssh2
    分別進入這兩個資料夾,設定並安裝:
    # make config install clean
    完成安裝後,可用phpinfo();網頁檢視SSH模組是否已支援。
    或是檢視/usr/local/etc/php/extensions.ini檔案內是否有此行:
    extension=ssh2.so
  9. 修改/etc/ssh/sshd_config檔
    必須啟用sshd的password authentication方法(sshftp.class.php內的叫用才會成功),找到PasswordAuthentication關進字並修改:

    # Change to yes to enable built-in password authentication.
    PasswordAuthentication yes

  10. 重新啟動sshd
    # /etc/rc.d/sshd restart
  11. 重新啟動apache
    # cd /usr/local/etc/rc.d
    ./apache restart
  12. try it!!

ref:

  1. [PHP] Secure Shell2
  2. [freeBSD] SSHD Man page
Friday, December 03, 2010

修改SharePoint網站之清單"建立者"欄位

Environment
Server: Win2003 + SharePoint Service 3

需求: 當透過WSS UI的「儲存清單為範本」+「包括內容」選項將清單匯出後,再匯入時,所有清單項目的「建立者」欄位都會變成匯入人的名字。如果資訊人員佛心來的想替使用者改掉... 是可以做的,寫程式囉。

利用Microsoft提供的SharePoint API: Microsoft.SharePoint.dll。這支dll可以在SharePoint server上的\\Computer_Name\Drive$\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI 內找到。

但是,如果你覺得把他複製出來到自己本機開發那就錯了。這麼做應該會遇到「無法載入檔案或組件或其相依性的其中之一」的錯誤,就像這篇SharePoint Forums上的討論,請直接在裝有SharePoint Service的server上做開發.. 如果有測試機器是再好不過的,就可以參考這篇文章(Can we update the values of “Created By”, "Modified By” columns in SharePoint lists?)直接在Visual Studio上參考上述組件做開發。

如果你正好沒有測試環境,而且也無法在production上安裝VisualStudio這麼龐大的怪物的話,就參考這篇文章(Updating the “Created By” and "Modified By” Columns in SharePoint lists using POWERSHELL)。沒錯! 就是用POWERSHELL。這也是我用的方法。

script:

   1:  # Get the SharePoint Assembly 


   2:  # You will need to run this on the SharePoint Server


   3:  [Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") 


   4:   


   5:  # create a new object called $SPSite


   6:  # Make sure you have the last “/” in the url on the site 


   7:  # allow $SPWeb to be the OpenWeb from the site


   8:  $SPSite = New-Object Microsoft.SharePoint.SPSite("http://server-name/")


   9:  $SPWeb = $SPSite.OpenWeb() 


  10:   


  11:  # Get the list ModifyCreatedBy 


  12:  $SPList = $SPWeb.Lists["ListName"] 


  13:   


  14:  # Get the Collection of the List Items


  15:  $SPListItemCollection = $SPList.Items 


  16:   


  17:  # iterate the Collection


  18:  foreach ($ListItem in $SPListItemCollection) 


  19:  {


  20:   


  21:    # The user in this case was UserA with id of 8 from the SPWeb users 


  22:    $SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,8,"UserA") 


  23:   


  24:    $ListItem["Author"] = $SPFieldUserValue


  25:   


  26:    # Note: Editor will be the account that you are running the Powershell under unless you update Editor as well 


  27:    # ListItem["Editor"] = $SPFieldUserValue


  28:   


  29:    $ListItem.Update() 


  30:  } 


  31:   


  32:  $SPWeb.Update() 


  33:   


  34:  # Still TODO Disposes, Error Checking, Change to take Parameters, etc


  35:   



 
上面的script是改了整個指定清單的建立者欄位,如果你還需要先判斷是那些文件,可以參考一下MSDN Library看看SPListItem還有哪些properities可以用。若是powershell語法不熟的話(尤其他又喜歡跟一般語法同.... 特別是比較運算子...),請參考reference#4, #5,或自行google。



嗯.. 大概就以上資訊,即可作業!!



Ref:




  1. [SharePoint Forums] Could not find Microsoft.SharePoint.Library.dll


  2. [MSDN Blog] Can we update the values of “Created By”, "Modified By” columns in SharePoint lists?


  3. [MSDN Blog] Updating the “Created By” and "Modified By” Columns in SharePoint lists using POWERSHELL


  4. [TechNet 技術文件庫] Running Windows PowerShell Scripts


  5. [賴榮樞的軟體資訊誌] scripting_PowerShell tag


  6. [MSDN Library] Microsoft.SharePoint Namespace

Tags