def readspeed(path, buffersize=None): """ Measures the amount of bytes transferred and the time elapsed reading the content of the file/device """ cmd = [ constants.EXT_DD, "if=%s" % path, "iflag=%s" % DIRECTFLAG, "of=/dev/null" ] if buffersize: cmd.extend(["bs=%d" % buffersize, "count=1"]) rc, _, err = execCmd(cmd) if rc != 0: log.error("Unable to read file '%s'", path) raise se.MiscFileReadException(path) try: # The statistics are located in the last line: m = _readspeed_regex.match(err[-1]) except IndexError: log.error("Unable to find dd statistics to parse") raise se.MiscFileReadException(path) if m is None: log.error("Unable to parse dd output: '%s'", err[-1]) raise se.MiscFileReadException(path) return { 'bytes': int(m.group('bytes')), 'seconds': float(m.group('seconds')), }
def readspeed(path, buffersize=None): """ Measures the amount of bytes transferred and the time elapsed reading the content of the file/device """ rc, _, err = _readfile(path, buffersize) if rc != 0: log.error("Unable to read file '%s'", path) raise se.MiscFileReadException(path) try: # The statistics are located in the last line: m = _readspeed_regex.match(err[-1]) except IndexError: log.error("Unable to find dd statistics to parse") raise se.MiscFileReadException(path) if m is None: log.error("Unable to parse dd output: '%s'", err[-1]) raise se.MiscFileReadException(path) return { 'bytes': int(m.group('bytes')), 'seconds': float(m.group('seconds')), }
def _copyData(inFile, outFile, totalSize): bytesToRead = totalSize while totalSize > 0: toRead = min(BUFFER_SIZE, totalSize) try: data = inFile.read(toRead) except IOError as e: error = "error reading file: %s" % e log.error(error) raise se.MiscFileReadException(error) if not data: error = "partial data %s from %s" % \ (bytesToRead - totalSize, bytesToRead) log.error(error) raise se.MiscFileReadException(error) outFile.write(data) # outFile may not be a real file object but a wrapper. # To ensure that we don't use more memory as the input buffer size # we flush on every write. outFile.flush() totalSize = totalSize - len(data)
def readfileSUDO(name): """ Read the content of the file using 'cat' command via sudo. """ cmd = [constants.EXT_CAT, name] (rc, out, err) = execCmd(cmd, sudo=True) if rc: raise se.MiscFileReadException(name) return out
def readfile(name): """ Read the content of the file using /bin/dd command """ cmd = [constants.EXT_DD, "iflag=%s" % DIRECTFLAG, "if=%s" % name] (rc, out, err) = execCmd(cmd, sudo=False) if rc: raise se.MiscFileReadException(name) return out
def _readfile(name, buffersize=None): cmd = [constants.EXT_DD] if fileUtils.pathRequiresFlagForDirectIO(name): cmd.append("iflag=%s" % DIRECTFLAG) cmd.append("if=%s" % name) if buffersize: cmd.extend(["bs=%d" % buffersize, "count=1"]) (rc, out, err) = execCmd(cmd, sudo=False) if rc: raise se.MiscFileReadException(name) return rc, out, err