2020logger = logging .getLogger ('cterasdk.core' )
2121
2222
23+ def _is_permission_denied_message (message ):
24+ msg = (message or '' ).lower ()
25+ return (
26+ 'permission' in msg
27+ or 'denied' in msg
28+ or 'read only' in msg
29+ or 'action is not allowed' in msg
30+ )
31+
32+
33+ def _raise_strict_permission_denied (result , path ):
34+ if result is None :
35+ raise exceptions .io .core .PrivilegeError (path )
36+ if isinstance (result , str ) and not result .strip ():
37+ raise exceptions .io .core .PrivilegeError (path )
38+
39+ msg = getattr (result , 'msg' , None )
40+ rc = getattr (result , 'rc' , None )
41+ if msg and _is_permission_denied_message (msg ):
42+ raise exceptions .io .core .PrivilegeError (path )
43+ if msg in (None , '' ) and rc in (0 , '0' , None ):
44+ raise exceptions .io .core .PrivilegeError (path )
45+
46+
2347def split_file_directory (listdir , receiver , destination ):
2448 """
2549 Split a path into its parent directory and final component.
@@ -120,12 +144,13 @@ def ensure_writeable(resource, directory):
120144
121145class Upload (PortalCommand ):
122146
123- def __init__ (self , function , receiver , listdir , destination , fd , name , size ):
147+ def __init__ (self , function , receiver , listdir , destination , fd , name , size , strict_permission = False ):
124148 super ().__init__ (function , receiver )
125149 self .destination = automatic_resolution (destination , receiver .context )
126150 self ._resolver = PathResolver (listdir , receiver , self .destination , name )
127151 self .size = size
128152 self .fd = fd
153+ self ._strict_permission = strict_permission
129154 self ._resource = None
130155
131156 def get_parameter (self ):
@@ -156,6 +181,8 @@ async def _a_execute(self):
156181
157182 def _handle_response (self , r ):
158183 path = self .destination .relative
184+ if self ._strict_permission :
185+ _raise_strict_permission_denied (r , path )
159186 if r .rc :
160187 error = exceptions .io .core .UploadError (r .msg , path )
161188 logger .error ('Upload failed: %s' , path )
@@ -596,10 +623,11 @@ def _handle_exception(self, e):
596623class CreateDirectory (PortalCommand ):
597624 """Create Directory"""
598625
599- def __init__ (self , function , receiver , path , parents = False ):
626+ def __init__ (self , function , receiver , path , parents = False , strict_permission = False ):
600627 super ().__init__ (function , receiver )
601628 self .path = automatic_resolution (path , receiver .context )
602629 self .parents = parents
630+ self ._strict_permission = strict_permission
603631
604632 def get_parameter (self ):
605633 param = Object ()
@@ -622,7 +650,12 @@ def _execute(self):
622650 if self .parents :
623651 for path in self ._parents_generator ():
624652 try :
625- CreateDirectory (self ._function , self ._receiver , path ).execute ()
653+ CreateDirectory (
654+ self ._function ,
655+ self ._receiver ,
656+ path ,
657+ strict_permission = self ._strict_permission
658+ ).execute ()
626659 except exceptions .io .core .CreateDirectoryError as e :
627660 CreateDirectory ._suppress_file_conflict_error (e )
628661 with self .trace_execution ():
@@ -632,7 +665,12 @@ async def _a_execute(self):
632665 if self .parents :
633666 for path in self ._parents_generator ():
634667 try :
635- await CreateDirectory (self ._function , self ._receiver , path ).a_execute ()
668+ await CreateDirectory (
669+ self ._function ,
670+ self ._receiver ,
671+ path ,
672+ strict_permission = self ._strict_permission
673+ ).a_execute ()
636674 except exceptions .io .core .CreateDirectoryError as e :
637675 CreateDirectory ._suppress_file_conflict_error (e )
638676 with self .trace_execution ():
@@ -645,6 +683,8 @@ def _suppress_file_conflict_error(e):
645683
646684 def _handle_response (self , r ):
647685 path = self .path .relative
686+ if self ._strict_permission :
687+ _raise_strict_permission_denied (r , path )
648688 if r is None or r == 'Ok' :
649689 return path
650690
@@ -903,10 +943,11 @@ def _handle_response(self, r):
903943
904944class TaskCommand (PortalCommand ):
905945
906- def __init__ (self , function , receiver , block ):
946+ def __init__ (self , function , receiver , block , strict_permission = False ):
907947 super ().__init__ (function , receiver )
908948 self .block = block
909949 self .background = True
950+ self ._strict_permission = strict_permission
910951
911952 @abstractmethod
912953 def _progress_str (self ):
@@ -938,6 +979,11 @@ async def _a_execute(self):
938979 return await function (self .get_parameter ())
939980
940981 def _handle_response (self , r ):
982+ if self ._strict_permission :
983+ msg = getattr (r , 'msg' , None )
984+ error_type = getattr (r , 'error_type' , None )
985+ if _is_permission_denied_message (str (msg )) or _is_permission_denied_message (str (error_type )):
986+ raise exceptions .io .core .PrivilegeError ('' )
941987 if not self .block :
942988 return r
943989
@@ -958,8 +1004,8 @@ def _task_error(self, task): # pylint: disable=no-self-use
9581004
9591005class MultiResourceCommand (TaskCommand ):
9601006
961- def __init__ (self , function , receiver , block , * paths ):
962- super ().__init__ (function , receiver , block )
1007+ def __init__ (self , function , receiver , block , * paths , strict_permission = False ):
1008+ super ().__init__ (function , receiver , block , strict_permission = strict_permission )
9631009 self .paths = list (automatic_resolution (paths , receiver .context ))
9641010
9651011 def get_parameter (self ):
@@ -998,8 +1044,9 @@ def _task_error(self, task):
9981044
9991045class ResolverCommand (TaskCommand ):
10001046
1001- def __init__ (self , function , receiver , block , * paths , destination = None , resolver = None , cursor = None ):
1002- super ().__init__ (function , receiver , block )
1047+ def __init__ (self , function , receiver , block , * paths , destination = None , resolver = None , cursor = None ,
1048+ strict_permission = False ):
1049+ super ().__init__ (function , receiver , block , strict_permission = strict_permission )
10031050 self .paths = list (automatic_resolution (paths , receiver .context ))
10041051 self .destination = automatic_resolution (destination , receiver .context )
10051052 self .resolver = resolver
@@ -1088,11 +1135,13 @@ def _progress_str(self):
10881135
10891136 def _try_with_resolver (self , cursor ):
10901137 return Copy (self ._function , self ._receiver , self .block , * self .paths ,
1091- destination = self .destination , resolver = self .resolver , cursor = cursor ).execute ()
1138+ destination = self .destination , resolver = self .resolver , cursor = cursor ,
1139+ strict_permission = self ._strict_permission ).execute ()
10921140
10931141 async def _a_try_with_resolver (self , cursor ):
10941142 return await Copy (self ._function , self ._receiver , self .block , * self .paths ,
1095- destination = self .destination , resolver = self .resolver , cursor = cursor ).a_execute ()
1143+ destination = self .destination , resolver = self .resolver , cursor = cursor ,
1144+ strict_permission = self ._strict_permission ).a_execute ()
10961145
10971146 @property
10981147 def _error_object (self ):
@@ -1106,11 +1155,13 @@ def _progress_str(self):
11061155
11071156 def _try_with_resolver (self , cursor ):
11081157 return Move (self ._function , self ._receiver , self .block , * self .paths ,
1109- destination = self .destination , resolver = self .resolver , cursor = cursor ).execute ()
1158+ destination = self .destination , resolver = self .resolver , cursor = cursor ,
1159+ strict_permission = self ._strict_permission ).execute ()
11101160
11111161 async def _a_try_with_resolver (self , cursor ):
11121162 return await Move (self ._function , self ._receiver , self .block , * self .paths ,
1113- destination = self .destination , resolver = self .resolver , cursor = cursor ).a_execute ()
1163+ destination = self .destination , resolver = self .resolver , cursor = cursor ,
1164+ strict_permission = self ._strict_permission ).a_execute ()
11141165
11151166 @property
11161167 def _error_object (self ):
@@ -1122,21 +1173,26 @@ class Rename(Move):
11221173 def _progress_str (self ):
11231174 return 'Renaming'
11241175
1125- def __init__ (self , function , receiver , block , path , new_name , resolver , cursor = None ):
1126- super ().__init__ (function , receiver , block ,
1127- * [(path , automatic_resolution (path , receiver .context ).parent .join (new_name ))],
1128- resolver = resolver , cursor = cursor
1129- )
1176+ def __init__ (self , function , receiver , block , path , new_name , resolver , cursor = None , strict_permission = False ):
1177+ super ().__init__ (
1178+ function ,
1179+ receiver ,
1180+ block ,
1181+ * [(path , automatic_resolution (path , receiver .context ).parent .join (new_name ))],
1182+ resolver = resolver ,
1183+ cursor = cursor ,
1184+ strict_permission = strict_permission
1185+ )
11301186
11311187 def _try_with_resolver (self , cursor ):
11321188 source , destination = self .paths [0 ]
11331189 return Rename (self ._function , self ._receiver , self .block , source , destination .name ,
1134- resolver = self .resolver , cursor = cursor ).execute ()
1190+ resolver = self .resolver , cursor = cursor , strict_permission = self . _strict_permission ).execute ()
11351191
11361192 async def _a_try_with_resolver (self , cursor ):
11371193 source , destination = self .paths [0 ]
11381194 return await Rename (self ._function , self ._receiver , self .block , source , destination .name ,
1139- resolver = self .resolver , cursor = cursor ).a_execute ()
1195+ resolver = self .resolver , cursor = cursor , strict_permission = self . _strict_permission ).a_execute ()
11401196
11411197 @property
11421198 def _error_object (self ):
0 commit comments