freemyipod r472 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r471‎ | r472 | r473 >
Date:02:06, 22 January 2011
Author:farthen
Status:new
Tags:
Comment:
emcore tools: adapt file I/O to use malloc() by default
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -877,7 +877,7 @@
878878 self.logger.info("done\n")
879879
880880 @command
881 - def readrawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
 881+ def readrawstoragefile(self, volume, sector, count, file, buffsize = 100000, buffer = None):
882882 """
883883 Reads <count> sectors starting at <sector> from storage <volume> to file <file>,
884884 buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
@@ -885,26 +885,34 @@
886886 volume = self._hexint(volume)
887887 sector = self._hexint(sector)
888888 count = self._hexint(count)
889 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
890 - else: buffer = self._hexint(buffer)
891889 buffsize = self._hexint(buffsize)
 890+ if buffer is None:
 891+ buffer = self.emcore.malloc(buffsize)
 892+ malloc = True
 893+ else:
 894+ buffer = self._hexint(buffer)
 895+ malloc = False
892896 try:
893 - f = open(file, 'wb')
894 - except IOError:
895 - raise ArgumentError("Could not open local file for writing.")
896 - self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
897 - storageinfo = self.emcore.storage_get_info(volume)
898 - while count > 0:
899 - sectors = min(count, int(buffsize / storageinfo.sectorsize))
900 - self.emcore.storage_read_sectors_md(volume, sector, sectors, buffer)
901 - f.write(self.emcore.read(buffer, storageinfo.sectorsize * sectors))
902 - sector = sector + sectors
903 - count = count - sectors
904 - f.close()
 897+ try:
 898+ f = open(file, 'wb')
 899+ except IOError:
 900+ raise ArgumentError("Could not open local file for writing.")
 901+ self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
 902+ storageinfo = self.emcore.storage_get_info(volume)
 903+ while count > 0:
 904+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 905+ self.emcore.storage_read_sectors_md(volume, sector, sectors, buffsize, buffer)
 906+ f.write(self.emcore.read(buffer, storageinfo.sectorsize * sectors))
 907+ sector = sector + sectors
 908+ count = count - sectors
 909+ f.close()
 910+ finally:
 911+ if malloc == True:
 912+ self.emcore.free(buffer)
905913 self.logger.info("done\n")
906914
907915 @command
908 - def writerawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
 916+ def writerawstoragefile(self, volume, sector, count, file, buffsize = 100000, buffer = None):
909917 """
910918 Writes contents of <file> to <count> sectors starting at <sector> on storage <volume>,
911919 buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
@@ -912,26 +920,34 @@
913921 volume = self._hexint(volume)
914922 sector = self._hexint(sector)
915923 count = self._hexint(count)
916 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
917 - else: buffer = self._hexint(buffer)
918924 buffsize = self._hexint(buffsize)
 925+ if buffer is None:
 926+ buffer = self.emcore.malloc(buffsize)
 927+ malloc = True
 928+ else:
 929+ buffer = self._hexint(buffer)
 930+ malloc = False
919931 try:
920 - f = open(file, 'rb')
921 - except IOError:
922 - raise ArgumentError("Could not open local file for reading.")
923 - self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
924 - storageinfo = self.emcore.storage_get_info(volume)
925 - while count > 0:
926 - sectors = min(count, int(buffsize / storageinfo.sectorsize))
927 - bytes = storageinfo.sectorsize * sectors
928 - data = f.read(bytes)
929 - if len(data) == 0: break
930 - while len(data) < bytes: data = data + f.read(bytes - len(data))
931 - self.emcore.write(buffer, data)
932 - self.emcore.storage_write_sectors_md(volume, sector, sectors, buffer)
933 - sector = sector + sectors
934 - count = count - sectors
935 - f.close()
 932+ try:
 933+ f = open(file, 'rb')
 934+ except IOError:
 935+ raise ArgumentError("Could not open local file for reading.")
 936+ self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
 937+ storageinfo = self.emcore.storage_get_info(volume)
 938+ while count > 0:
 939+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 940+ bytes = storageinfo.sectorsize * sectors
 941+ data = f.read(bytes)
 942+ if len(data) == 0: break
 943+ while len(data) < bytes: data = data + f.read(bytes - len(data))
 944+ self.emcore.write(buffer, data)
 945+ self.emcore.storage_write_sectors_md(volume, sector, sectors, buffsize, buffer)
 946+ sector = sector + sectors
 947+ count = count - sectors
 948+ f.close()
 949+ finally:
 950+ if malloc == True:
 951+ self.emcore.free(buffer)
936952 self.logger.info("done\n")
937953
938954 @command
@@ -989,111 +1005,143 @@
9901006 self.logger.info(" done\n")
9911007
9921008 @command
993 - def get(self, remotename, localname, buffer = False, buffsize = "10000"):
 1009+ def get(self, remotename, localname, buffsize = 10000, buffer = None):
9941010 """
9951011 Downloads a file
9961012 <remotename>: filename on the device
9971013 <localname>: filename on the computer
 1014+ [buffsize]: buffer size (optional)
9981015 [buffer]: buffer address (optional)
999 - [buffsize]: buffer size (optional)
10001016 """
1001 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
1002 - else: buffer = self._hexint(buffer)
10031017 buffsize = self._hexint(buffsize)
 1018+ if buffer is None:
 1019+ buffer = self.emcore.malloc(buffsize)
 1020+ malloc = True
 1021+ else:
 1022+ buffer = self._hexint(buffer)
 1023+ malloc = False
10041024 try:
1005 - f = open(localname, 'wb')
1006 - except IOError:
1007 - raise ArgumentError("Could not open local file for writing.")
1008 - self.logger.info("Downloading file " + remotename + " to " + localname + "...")
1009 - fd = self.emcore.file_open(remotename, 0)
1010 - size = self.emcore.file_size(fd)
1011 - while size > 0:
1012 - bytes = self.emcore.file_read(fd, buffer, buffsize)
1013 - f.write(self.emcore.read(buffer, bytes))
1014 - size = size - bytes
1015 - self.emcore.file_close(fd)
1016 - f.close()
 1025+ try:
 1026+ f = open(localname, 'wb')
 1027+ except IOError:
 1028+ raise ArgumentError("Could not open local file for writing.")
 1029+ self.logger.info("Downloading file " + remotename + " to " + localname + "...")
 1030+ fd = self.emcore.file_open(remotename, 0)
 1031+ size = self.emcore.file_size(fd)
 1032+ while size > 0:
 1033+ bytes = self.emcore.file_read(fd, buffer, buffsize)
 1034+ f.write(self.emcore.read(buffer, bytes))
 1035+ size = size - bytes
 1036+ self.emcore.file_close(fd)
 1037+ f.close()
 1038+ finally:
 1039+ if malloc == True:
 1040+ self.emcore.free(buffer)
10171041 self.logger.info(" done\n")
10181042
10191043 @command
1020 - def gettree(self, remotepath, localpath, buffer = False, buffsize = "10000"):
 1044+ def gettree(self, remotepath, localpath, buffsize = 10000, buffer = None):
10211045 """
10221046 Downloads a directory tree
10231047 <remotepath>: path on the device
10241048 <localpath>: path on the computer
 1049+ [buffsize]: buffer size (optional)
10251050 [buffer]: buffer address (optional)
1026 - [buffsize]: buffer size (optional)
10271051 """
1028 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
1029 - else: buffer = self._hexint(buffer)
10301052 buffsize = self._hexint(buffsize)
1031 - try: os.mkdir(localpath)
1032 - except: pass
 1053+ if buffer is None:
 1054+ buffer = self.emcore.malloc(buffsize)
 1055+ malloc = True
 1056+ else:
 1057+ buffer = self._hexint(buffer)
 1058+ malloc = False
 1059+ try:
 1060+ try: os.mkdir(localpath)
 1061+ except: pass
10331062
1034 - handle = self.emcore.dir_open(remotepath)
1035 - while True:
1036 - try:
1037 - entry = self.emcore.dir_read(handle)
1038 - if entry.name == "." or entry.name == "..": continue
1039 - elif entry.attributes & 0x10:
1040 - self.gettree(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
1041 - else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
1042 - except: break
1043 - self.emcore.dir_close(handle)
 1063+ handle = self.emcore.dir_open(remotepath)
 1064+ while True:
 1065+ try:
 1066+ entry = self.emcore.dir_read(handle)
 1067+ if entry.name == "." or entry.name == "..": continue
 1068+ elif entry.attributes & 0x10:
 1069+ self.gettree(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
 1070+ else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
 1071+ except: break
 1072+ self.emcore.dir_close(handle)
 1073+ finally:
 1074+ if malloc == True:
 1075+ self.emcore.free(buffer)
10441076
10451077 @command
1046 - def put(self, localname, remotename, buffer = False, buffsize = "10000"):
 1078+ def put(self, localname, remotename, buffsize = 10000, buffer = None):
10471079 """
10481080 Uploads a file
10491081 <localname>: filename on the computer
10501082 <remotename>: filename on the device
 1083+ [buffsize]: buffer size (optional)
10511084 [buffer]: buffer address (optional)
1052 - [buffsize]: buffer size (optional)
10531085 """
1054 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
1055 - else: buffer = self._hexint(buffer)
10561086 buffsize = self._hexint(buffsize)
 1087+ if buffer is None:
 1088+ buffer = self.emcore.malloc(buffsize)
 1089+ malloc = True
 1090+ else:
 1091+ buffer = self._hexint(buffer)
 1092+ malloc = False
10571093 try:
1058 - f = open(localname, 'rb')
1059 - except IOError:
1060 - raise ArgumentError("Could not open local file for reading.")
1061 - self.logger.info("Uploading file " + localname + " to " + remotename + "...")
1062 - fd = self.emcore.file_open(remotename, 0x15)
1063 - while True:
1064 - data = f.read(buffsize)
1065 - if len(data) == 0: break
1066 - self.emcore.write(buffer, data)
1067 - bytes = 0
1068 - while bytes < len(data):
1069 - bytes = bytes + self.emcore.file_write(fd, buffer + bytes, len(data) - bytes)
1070 - self.emcore.file_close(fd)
1071 - f.close()
 1094+ try:
 1095+ f = open(localname, 'rb')
 1096+ except IOError:
 1097+ raise ArgumentError("Could not open local file for reading.")
 1098+ self.logger.info("Uploading file " + localname + " to " + remotename + "...")
 1099+ fd = self.emcore.file_open(remotename, 0x15)
 1100+ while True:
 1101+ data = f.read(buffsize)
 1102+ if len(data) == 0: break
 1103+ self.emcore.write(buffer, data)
 1104+ bytes = 0
 1105+ while bytes < len(data):
 1106+ bytes = bytes + self.emcore.file_write(fd, buffer + bytes, len(data) - bytes)
 1107+ self.emcore.file_close(fd)
 1108+ f.close()
 1109+ finally:
 1110+ if malloc == True:
 1111+ self.emcore.free(buffer)
10721112 self.logger.info(" done\n")
10731113
10741114 @command
1075 - def puttree(self, localpath, remotepath, buffer = False, buffsize = "10000"):
 1115+ def puttree(self, localpath, remotepath, buffsize = 10000, buffer = None):
10761116 """
10771117 Uploads a directory tree
10781118 <localpath>: path on the computer
10791119 <remotepath>: path on the device
 1120+ [buffsize]: buffer size (optional)
10801121 [buffer]: buffer address (optional)
1081 - [buffsize]: buffer size (optional)
10821122 """
1083 - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
1084 - else: buffer = self._hexint(buffer)
10851123 buffsize = self._hexint(buffsize)
1086 - try: self.mkdir(remotepath)
1087 - except: self.logger.info(" failed\n")
1088 - pathlen = len(localpath)
1089 - for d in os.walk(localpath):
1090 - prefix = remotepath + "/" + d[0].replace("\\", "/")[pathlen:] + "/"
1091 - for dir in d[1]:
1092 - if dir != ".svn":
1093 - try: self.mkdir(prefix + dir)
1094 - except: self.logger.info(" failed\n")
1095 - for f in d[2]:
1096 - if not prefix.find("/.svn/") > -1:
1097 - self.put(d[0] + "/" + f, prefix + f, buffer, buffsize)
 1124+ if buffer is None:
 1125+ buffer = self.emcore.malloc(buffsize)
 1126+ malloc = True
 1127+ else:
 1128+ buffer = self._hexint(buffer)
 1129+ malloc = False
 1130+ try:
 1131+ try: self.mkdir(remotepath)
 1132+ except: self.logger.info(" failed\n")
 1133+ pathlen = len(localpath)
 1134+ for d in os.walk(localpath):
 1135+ prefix = remotepath + "/" + d[0].replace("\\", "/")[pathlen:] + "/"
 1136+ for dir in d[1]:
 1137+ if dir != ".svn":
 1138+ try: self.mkdir(prefix + dir)
 1139+ except: self.logger.info(" failed\n")
 1140+ for f in d[2]:
 1141+ if not prefix.find("/.svn/") > -1:
 1142+ self.put(d[0] + "/" + f, prefix + f, buffer, buffsize)
 1143+ finally:
 1144+ if malloc == True:
 1145+ self.emcore.free(buffer)
10981146
10991147 @command
11001148 def ls(self, path = "/"):
Index: emcore/trunk/tools/libemcore.py
@@ -618,16 +618,33 @@
619619 return result
620620
621621 @command(timeout = 50000)
622 - def storage_read_sectors_md(self, volume, sector, count, addr):
623 - """ Read sectors from as storage device """
624 - result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 622+ def storage_read_sectors_md(self, volume, sector, count, size = 100000, addr = None):
 623+ """ Read sectors from as storage device. If addr is not given it allocates a buffer itself. """
 624+ if addr is None:
 625+ addr = self.malloc(size)
 626+ malloc = True
 627+ else:
 628+ malloc = False
 629+ try:
 630+ result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 631+ finally:
 632+ if malloc == True:
 633+ self.free(addr)
625634 if result.rc > 0x80000000:
626635 raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
627636
628 - @command(timeout = 50000)
629 - def storage_write_sectors_md(self, volume, sector, count, addr):
630 - """ Read sectors from as storage device """
631 - result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 637+ def storage_write_sectors_md(self, volume, sector, count, size = 100000, addr = None):
 638+ """ Read sectors from as storage device. If addr is not given it allocates a buffer itself. """
 639+ if addr is None:
 640+ addr = self.malloc(size)
 641+ malloc = True
 642+ else:
 643+ malloc = False
 644+ try:
 645+ result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 646+ finally:
 647+ if malloc == True:
 648+ self.free(addr)
632649 if result.rc > 0x80000000:
633650 raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
634651
@@ -648,17 +665,35 @@
649666 return result.size
650667
651668 @command(timeout = 30000)
652 - def file_read(self, fd, addr, size):
653 - """ Reads data from a file referenced by a handle """
654 - result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
 669+ def file_read(self, fd, size = 100000, addr = None):
 670+ """ Reads data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
 671+ if addr is None:
 672+ addr = self.malloc(size)
 673+ malloc = True
 674+ else:
 675+ malloc = False
 676+ try:
 677+ result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
 678+ finally:
 679+ if malloc == True:
 680+ self.free(addr)
655681 if result.rc > 0x80000000:
656682 raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
657683 return result.rc
658684
659685 @command(timeout = 30000)
660 - def file_write(self, fd, addr, size):
661 - """ Writes data from a file referenced by a handle """
662 - result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
 686+ def file_write(self, fd, size = 100000, addr = None):
 687+ """ Writes data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
 688+ if addr is None:
 689+ addr = self.malloc(size)
 690+ malloc = True
 691+ else:
 692+ malloc = False
 693+ try:
 694+ result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
 695+ finally:
 696+ if malloc == True:
 697+ self.free(addr)
663698 if result.rc > 0x80000000:
664699 raise DeviceError("file_write(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
665700 return result.rc