1 /******************************************************************************* 2 / 3 / File: FileUtils.cpp 4 / 5 / Description: Utility functions for copying file data and attributes. 6 / 7 / Copyright 1998-1999, Be Incorporated, All Rights Reserved 8 / 9 *******************************************************************************/ 10 11 12 #include <stdio.h> 13 #include <fs_attr.h> 14 #include "array_delete.h" 15 #include "FileUtils.h" 16 17 status_t CopyFileData(BFile& dst, BFile& src) 18 { 19 struct stat src_stat; 20 status_t err = src.GetStat(&src_stat); 21 if (err != B_OK) { 22 printf("couldn't get stat: %#010lx\n", err); 23 return err; 24 } 25 26 size_t bufSize = src_stat.st_blksize; 27 if (! bufSize) { 28 bufSize = 32768; 29 } 30 31 char* buf = new char[bufSize]; 32 array_delete<char> bufDelete(buf); 33 34 printf("copy data, bufSize = %ld\n", bufSize); 35 // copy data 36 while (true) { 37 ssize_t bytes = src.Read(buf, bufSize); 38 if (bytes > 0) { 39 ssize_t result = dst.Write(buf, bytes); 40 if (result != bytes) { 41 printf("result = %#010lx, bytes = %#010lx\n", (uint32) result, 42 (uint32) bytes); 43 return B_ERROR; 44 } 45 } else { 46 if (bytes < 0) { 47 printf(" bytes = %#010lx\n", (uint32) bytes); 48 return bytes; 49 } else { 50 // EOF 51 break; 52 } 53 } 54 } 55 56 // finish up miscellaneous stat stuff 57 dst.SetPermissions(src_stat.st_mode); 58 dst.SetOwner(src_stat.st_uid); 59 dst.SetGroup(src_stat.st_gid); 60 dst.SetModificationTime(src_stat.st_mtime); 61 dst.SetCreationTime(src_stat.st_crtime); 62 63 return B_OK; 64 } 65 66 67 status_t CopyAttributes(BNode& dst, BNode& src) 68 { 69 // copy attributes 70 src.RewindAttrs(); 71 char name[B_ATTR_NAME_LENGTH]; 72 while (src.GetNextAttrName(name) == B_OK) { 73 attr_info info; 74 if (src.GetAttrInfo(name, &info) == B_OK) { 75 size_t bufSize = info.size; 76 char* buf = new char[bufSize]; 77 array_delete<char> bufDelete = buf; 78 79 // copy one attribute 80 ssize_t bytes = src.ReadAttr(name, info.type, 0, buf, bufSize); 81 if (bytes > 0) { 82 dst.WriteAttr(name, info.type, 0, buf, bufSize); 83 } else { 84 return bytes; 85 } 86 } 87 } 88 89 return B_OK; 90 } 91 92 93 status_t CopyFile(BFile& dst, BFile& src) 94 { 95 status_t err = CopyFileData(dst, src); 96 if (err != B_OK) 97 return err; 98 99 return CopyAttributes(dst, src); 100 } 101