00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #ifndef __Persona_h__
00013 #define __Persona_h__
00014
00015 class Persona
00016 {
00017 #ifdef __UNIX__
00018
00019 WX_DECLARE_HASH_MAP(gid_t, wxString, wxIntegerHash, wxIntegerEqual, GroupHash);
00020 #endif // def __UNIX__
00021
00022
00023 public:
00024 Persona()
00025 {
00026 #ifdef __UNIX__
00027 uid = getuid();
00028 gid = getgid();
00029 GetGroups();
00030 #endif // def __UNIX__
00031 sHome = wxGetHomeDir();
00032 };
00033
00034
00035
00036
00037 public:
00038 wxString GetHome()
00039 {
00040 return sHome;
00041 };
00042
00043 bool CanExecute(const wxFileName& filPath)
00044 {
00045 wxString sPath = filPath.GetFullPath();
00046 #ifdef __UNIX__
00047 struct stat st;
00048 lstat(sPath.fn_str(), &st);
00049 #elif defined __WXMSW__
00050 struct _stat st;
00051 MSW_STAT(sPath.fn_str(), &st);
00052 #endif // def __UNIX__
00053 return CanExecute(st);
00054 };
00055
00056 #ifdef __UNIX__
00057 bool CanExecute(const struct stat st)
00058 {
00059 if(st.st_mode & S_IXOTH)
00060 return true;
00061
00062 bool bIsInGroup = IsInGroup(st.st_gid);
00063 if(bIsInGroup && (st.st_mode & S_IXGRP))
00064 return true;
00065
00066 if(uid == st.st_uid && (st.st_mode & S_IXUSR))
00067 return true;
00068
00069 return false;
00070 };
00071 #elif defined __WXMSW__
00072 bool CanExecute(const struct _stat st)
00073 {
00074 return (st.st_mode & _S_IEXEC)? true : false;
00075 }
00076 #endif // def __UNIX__
00077
00078 #ifdef __UNIX__
00079 bool IsInGroup(gid_t gid)
00080 {
00081 GroupHash::iterator it = gph.find(gid);
00082 if(it != gph.end())
00083 return true;
00084 return false;
00085 };
00086 #endif // def __UNIX__
00087
00088 private:
00089 #ifdef __UNIX__
00090
00091
00092
00093
00094
00095
00096 bool GetGroups(void)
00097 {
00098 int nGroupCount = getgroups(0, NULL);
00099 gid_t * agid = (gid_t *) malloc(nGroupCount * sizeof(gid_t));
00100 int nGetGroupsResult = getgroups(nGroupCount, agid);
00101 if(nGetGroupsResult < 0)
00102 {
00103 free(agid);
00104 return false;
00105 }
00106
00107 for(int i = 0; i< nGroupCount; i++)
00108 {
00109 gid_t gid = agid[i];
00110 gph[gid] = wxT("GROUP");
00111 }
00112
00113 free(agid);
00114 return true;
00115 }
00116 #endif // def __UNIX__
00117
00118
00119 private:
00120 #ifdef __UNIX__
00121 uid_t uid;
00122 gid_t gid;
00123 GroupHash gph;
00124 #endif // def __UNIX__
00125 wxString sHome;
00126 };
00127
00128 #endif // ndef __Persona_h__