def Run(self, args): """Delete all the GRR temp files in path. If path is a directory, look in the top level for filenames beginning with Client.tempfile_prefix, and delete them. If path is a regular file and starts with Client.tempfile_prefix delete it. Args: args: pathspec pointing to directory containing temp files to be deleted, or a single file to be deleted. Returns: deleted: array of filename strings that were deleted Raises: ErrorBadPath: if path doesn't exist or is not a regular file or directory """ # Normalize the path, so DeleteGRRTempFile can correctly check if # it is within Client.tempdir. if args.path: path = client_utils.CanonicalPathToLocalPath( utils.NormalizePath(args.path)) else: path = config_lib.CONFIG["Client.tempdir"] deleted = [] errors = [] if os.path.isdir(path): for filename in os.listdir(path): abs_filename = os.path.join(path, filename) try: DeleteGRRTempFile(abs_filename) deleted.append(abs_filename) except Exception as e: # pylint: disable=broad-except # The error we are most likely to get is ErrorNotTempFile but # especially on Windows there might be locking issues that raise # various WindowsErrors so we just catch them all and continue # deleting all other temp files in this directory. errors.append(e) elif os.path.isfile(path): DeleteGRRTempFile(path) deleted = [path] elif not os.path.exists(path): raise ErrorBadPath("File %s does not exist" % path) else: raise ErrorBadPath("Not a regular file or directory: %s" % path) reply = "" if deleted: reply = "Deleted: %s." % deleted else: reply = "Nothing deleted." if errors: reply += "\n%s" % errors self.SendReply(rdf_client.LogMessage(data=reply))
def Run(self, args): """Delete all the GRR temp files in path. If path is a directory, look in the top level for filenames beginning with Client.tempfile_prefix, and delete them. If path is a regular file and starts with Client.tempfile_prefix delete it. Args: args: pathspec pointing to directory containing temp files to be deleted, or a single file to be deleted. Returns: deleted: array of filename strings that were deleted Raises: ErrorBadPath: if path doesn't exist or is not a regular file or directory """ # Normalize the path, so DeleteGRRTempFile can correctly check if # it is within Client.tempdir. if args.path: path = client_utils.CanonicalPathToLocalPath( utils.NormalizePath(args.path)) else: path = config_lib.CONFIG["Client.tempdir"] deleted = [] if os.path.isdir(path): for filename in os.listdir(path): abs_filename = os.path.join(path, filename) try: DeleteGRRTempFile(abs_filename) deleted.append(abs_filename) except ErrorNotTempFile: pass elif os.path.isfile(path): DeleteGRRTempFile(path) deleted = [path] elif not os.path.exists(path): raise ErrorBadPath("File %s does not exist" % path) else: raise ErrorBadPath("Not a regular file or directory: %s" % path) out_rdfvalue = rdf_client.LogMessage(data="Deleted: %s" % deleted) self.SendReply(out_rdfvalue)
def testHandleMessage(self): """Test handling of a normal request with a response.""" args = rdf_client.LogMessage(data="hello") # Push a request on it message = rdf_flows.GrrMessage( name="MockAction", session_id=self.session_id, auth_state=rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED, payload=args, request_id=1) self.context.HandleMessage(message) # Check the response - one data and one status message_list = self.context.Drain().job self.assertEqual(message_list[0].session_id, self.session_id) self.assertEqual(message_list[0].response_id, 1) self.assertIn("hello", message_list[0].payload.data) self.assertEqual(message_list[1].response_id, 2) self.assertEqual(message_list[1].type, rdf_flows.GrrMessage.Type.STATUS)
def MockSendReply(unused_self, reply=None, **kwargs): results.append(reply or rdf_client.LogMessage(**kwargs))