[1] | 1 | |
---|
| 2 | #include "cmt_std.h" |
---|
| 3 | #include "cmt_lock.h" |
---|
| 4 | #include "cmt_system.h" |
---|
| 5 | #include "cmt_error.h" |
---|
| 6 | #include "cmt_symbol.h" |
---|
| 7 | |
---|
| 8 | //---------------------------------------------------------- |
---|
| 9 | CmtLock::status CmtLock::lock () |
---|
| 10 | //---------------------------------------------------------- |
---|
| 11 | { |
---|
| 12 | status s = check (); |
---|
| 13 | |
---|
| 14 | switch (s) |
---|
| 15 | { |
---|
| 16 | case locked_by_user: |
---|
| 17 | cout << "Package already locked by you" << endl; |
---|
| 18 | return (s); |
---|
| 19 | case locked_by_another_user: |
---|
| 20 | CmtError::set (CmtError::cannot_lock, "lock> Package already locked by another user"); |
---|
| 21 | return (s); |
---|
| 22 | case not_locked: |
---|
| 23 | break; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | cmt_string text = "locked by "; |
---|
| 27 | text += CmtSystem::user (); |
---|
| 28 | text += " date "; |
---|
| 29 | text += CmtSystem::now (); |
---|
| 30 | |
---|
| 31 | if (!text.write ("lock.cmt")) |
---|
| 32 | { |
---|
| 33 | CmtError::set (CmtError::cannot_write_lock, "lock>"); |
---|
| 34 | return (still_unlocked); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | Symbol* lock_command = Symbol::find ("lock_command"); |
---|
| 38 | if (lock_command != 0) |
---|
| 39 | { |
---|
| 40 | cmt_string command = lock_command->build_macro_value (); |
---|
| 41 | |
---|
| 42 | if (command != "") |
---|
| 43 | { |
---|
| 44 | if (CmtSystem::execute (command) != 0) |
---|
| 45 | { |
---|
| 46 | CmtError::set (CmtError::cannot_run_lock_command, "lock>"); |
---|
| 47 | return (still_unlocked); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | cout << "Package now locked" << endl; |
---|
| 53 | |
---|
| 54 | return (locked_by_user); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | //---------------------------------------------------------- |
---|
| 58 | CmtLock::status CmtLock::unlock () |
---|
| 59 | //---------------------------------------------------------- |
---|
| 60 | { |
---|
| 61 | status s = check (); |
---|
| 62 | |
---|
| 63 | switch (s) |
---|
| 64 | { |
---|
| 65 | case locked_by_user: |
---|
| 66 | break; |
---|
| 67 | case locked_by_another_user: |
---|
| 68 | CmtError::set (CmtError::cannot_unlock, "unlock> Package locked by another user"); |
---|
| 69 | return (s); |
---|
| 70 | case not_locked: |
---|
| 71 | cout << "The package was not locked" << endl; |
---|
| 72 | return (s); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | Symbol* unlock_command = Symbol::find ("unlock_command"); |
---|
| 76 | if (unlock_command != 0) |
---|
| 77 | { |
---|
| 78 | cmt_string command = unlock_command->build_macro_value (); |
---|
| 79 | |
---|
| 80 | if (command != "") |
---|
| 81 | { |
---|
| 82 | if (CmtSystem::execute (command) != 0) |
---|
| 83 | { |
---|
| 84 | CmtError::set (CmtError::cannot_run_unlock_command, "unlock>"); |
---|
| 85 | return (still_locked); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if (!CmtSystem::remove_file ("lock.cmt")) |
---|
| 91 | { |
---|
| 92 | CmtError::set (CmtError::cannot_remove_lock, "unlock>"); |
---|
| 93 | return (still_locked); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | cout << "Package now unlocked" << endl; |
---|
| 97 | |
---|
| 98 | return (not_locked); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | //---------------------------------------------------------- |
---|
| 102 | CmtLock::status CmtLock::check () |
---|
| 103 | //---------------------------------------------------------- |
---|
| 104 | { |
---|
| 105 | cmt_string me = CmtSystem::user (); |
---|
| 106 | cmt_string text; |
---|
| 107 | |
---|
| 108 | if (text.read ("lock.cmt")) |
---|
| 109 | { |
---|
| 110 | CmtSystem::cmt_string_vector words; |
---|
| 111 | |
---|
| 112 | CmtSystem::split (text, " ", words); |
---|
| 113 | |
---|
| 114 | if (words.size () >= 3) |
---|
| 115 | { |
---|
| 116 | if (words[2] == me) |
---|
| 117 | { |
---|
| 118 | return (locked_by_user); |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | return (locked_by_another_user); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | return (not_locked); |
---|
| 128 | } |
---|