00001 //----------------------------------------------------------- 00002 // Copyright Christian Arnault LAL-Orsay CNRS 00003 // 00004 // See the complete license in cmt_license.txt "http://www.cecill.info". 00005 //----------------------------------------------------------- 00006 00007 #include "cmt_std.h" 00008 #include "cmt_lock.h" 00009 #include "cmt_system.h" 00010 #include "cmt_error.h" 00011 #include "cmt_symbol.h" 00012 00013 //---------------------------------------------------------- 00014 CmtLock::status CmtLock::lock () 00015 { 00016 status s = check (); 00017 00018 switch (s) 00019 { 00020 case locked_by_user: 00021 cout << "Package already locked by you" << endl; 00022 return (s); 00023 case locked_by_another_user: 00024 CmtError::set (CmtError::cannot_lock, "lock> Package already locked by another user"); 00025 return (s); 00026 case not_locked: 00027 break; 00028 } 00029 00030 cmt_string text = "locked by "; 00031 text += CmtSystem::user (); 00032 text += " date "; 00033 text += CmtSystem::now (); 00034 00035 if (!text.write ("lock.cmt")) 00036 { 00037 CmtError::set (CmtError::cannot_write_lock, "lock>"); 00038 return (still_unlocked); 00039 } 00040 00041 Symbol* lock_command = Symbol::find ("lock_command"); 00042 if (lock_command != 0) 00043 { 00044 cmt_string command = lock_command->build_macro_value (); 00045 00046 if (command != "") 00047 { 00048 if (CmtSystem::execute (command) != 0) 00049 { 00050 CmtError::set (CmtError::cannot_run_lock_command, "lock>"); 00051 return (still_unlocked); 00052 } 00053 } 00054 } 00055 00056 cout << "Package now locked" << endl; 00057 00058 return (locked_by_user); 00059 } 00060 00061 //---------------------------------------------------------- 00062 CmtLock::status CmtLock::unlock () 00063 { 00064 status s = check (); 00065 00066 switch (s) 00067 { 00068 case locked_by_user: 00069 break; 00070 case locked_by_another_user: 00071 CmtError::set (CmtError::cannot_unlock, "unlock> Package locked by another user"); 00072 return (s); 00073 case not_locked: 00074 cout << "The package was not locked" << endl; 00075 return (s); 00076 } 00077 00078 Symbol* unlock_command = Symbol::find ("unlock_command"); 00079 if (unlock_command != 0) 00080 { 00081 cmt_string command = unlock_command->build_macro_value (); 00082 00083 if (command != "") 00084 { 00085 if (CmtSystem::execute (command) != 0) 00086 { 00087 CmtError::set (CmtError::cannot_run_unlock_command, "unlock>"); 00088 return (still_locked); 00089 } 00090 } 00091 } 00092 00093 if (!CmtSystem::remove_file ("lock.cmt")) 00094 { 00095 CmtError::set (CmtError::cannot_remove_lock, "unlock>"); 00096 return (still_locked); 00097 } 00098 00099 cout << "Package now unlocked" << endl; 00100 00101 return (not_locked); 00102 } 00103 00104 //---------------------------------------------------------- 00105 CmtLock::status CmtLock::check () 00106 { 00107 cmt_string me = CmtSystem::user (); 00108 cmt_string text; 00109 00110 if (text.read ("lock.cmt")) 00111 { 00112 CmtSystem::cmt_string_vector words; 00113 00114 CmtSystem::split (text, " ", words); 00115 00116 if (words.size () >= 3) 00117 { 00118 if (words[2] == me) 00119 { 00120 return (locked_by_user); 00121 } 00122 else 00123 { 00124 return (locked_by_another_user); 00125 } 00126 } 00127 } 00128 00129 return (not_locked); 00130 }