goduf.go
changeset 19 3389a17fc0d2
parent 16 cc0ee80cf216
child 20 f7ce9d750e83
equal deleted inserted replaced
18:5219596f5c71 19:3389a17fc0d2
   163 	if err != nil {
   163 	if err != nil {
   164 		return err
   164 		return err
   165 	}
   165 	}
   166 	defer file.Close()
   166 	defer file.Close()
   167 	hash := sha1.New()
   167 	hash := sha1.New()
   168 	// XXX: duplicated code!
   168 
   169 	// BOF
   169 	// Read first bytes and last bytes from file
   170 	if _, err := io.CopyN(hash, file, medsumBytes); err != nil {
   170 	for i := 0; i < 2; i++ {
   171 		if err == nil {
       
   172 			return errors.New("failed to read bytes from file:" +
       
   173 				fo.FilePath)
       
   174 		}
       
   175 		return err
       
   176 	}
       
   177 	/*
       
   178 		// MOF
       
   179 		file.Seek((fo.Size()-medsumBytes)/2, 0)
       
   180 		if _, err := io.CopyN(hash, file, medsumBytes); err != nil {
   171 		if _, err := io.CopyN(hash, file, medsumBytes); err != nil {
   181 			if err == nil {
   172 			if err == nil {
   182 				return errors.New("failed to read bytes from file:" +
   173 				const errmsg = "failed to read bytes from file: "
   183 					fo.FilePath)
   174 				return errors.New(errmsg + fo.FilePath)
   184 			}
   175 			}
   185 			return err
   176 			return err
   186 		}
   177 		}
   187 	*/
   178 		if i == 0 { // Seek to end of file
   188 	// EOF
   179 			file.Seek(0-medsumBytes, 2)
   189 	file.Seek(0-medsumBytes, 2)
   180 		}
   190 	if _, err := io.CopyN(hash, file, medsumBytes); err != nil {
       
   191 		if err == nil {
       
   192 			return errors.New("failed to read bytes from file:" +
       
   193 				fo.FilePath)
       
   194 		}
       
   195 		return err
       
   196 	}
   181 	}
   197 
   182 
   198 	fo.PartialHash = hash.Sum(nil)
   183 	fo.PartialHash = hash.Sum(nil)
   199 
   184 
   200 	return nil
   185 	return nil