1 /******************************************************************************
5 / Description: Utility functions for copying file data and attributes.
7 / Copyright 1998-1999, Be Incorporated, All Rights Reserved
9 ******************************************************************************/
10 #include "FileUtils.h"
19 #include "AutoDeleter.h"
26 CopyFileData(BFile
& dst
, BFile
& src
)
29 status_t err
= src
.GetStat(&src_stat
);
31 printf("couldn't get stat: %#010" B_PRIx32
"\n", err
);
35 size_t bufSize
= src_stat
.st_blksize
;
39 char* buf
= new (nothrow
) char[bufSize
];
42 ArrayDeleter
<char> _(buf
);
44 printf("copy data, bufSize = %ld\n", bufSize
);
47 ssize_t bytes
= src
.Read(buf
, bufSize
);
49 ssize_t result
= dst
.Write(buf
, bytes
);
50 if (result
!= bytes
) {
51 fprintf(stderr
, "Failed to write %ld bytes: %s\n", bytes
,
52 strerror((status_t
)result
));
54 return (status_t
)result
;
60 fprintf(stderr
, "Failed to read file: %s\n", strerror(
62 return (status_t
)bytes
;
70 // finish up miscellaneous stat stuff
71 dst
.SetPermissions(src_stat
.st_mode
);
72 dst
.SetOwner(src_stat
.st_uid
);
73 dst
.SetGroup(src_stat
.st_gid
);
74 dst
.SetModificationTime(src_stat
.st_mtime
);
75 dst
.SetCreationTime(src_stat
.st_crtime
);
82 CopyAttributes(BNode
& dst
, BNode
& src
)
86 char attrName
[B_ATTR_NAME_LENGTH
];
87 while (src
.GetNextAttrName(attrName
) == B_OK
) {
89 if (src
.GetAttrInfo(attrName
, &info
) != B_OK
) {
90 fprintf(stderr
, "Failed to read info for attribute '%s'\n",
94 // copy one attribute in chunks of 4096 bytes
98 ssize_t read
= src
.ReadAttr(attrName
, info
.type
, offset
, buffer
,
99 std::min((off_t
)size
, info
.size
));
101 fprintf(stderr
, "Error reading attribute '%s'\n", attrName
);
102 return (status_t
)read
;
104 // NOTE: Attributes of size 0 are perfectly valid!
106 ssize_t written
= dst
.WriteAttr(attrName
, info
.type
, offset
,
108 if (written
!= read
) {
109 fprintf(stderr
, "Error writing attribute '%s'\n", attrName
);
111 return (status_t
)written
;
116 read
= src
.ReadAttr(attrName
, info
.type
, offset
, buffer
,
117 std::min((off_t
)size
, info
.size
- offset
));
119 fprintf(stderr
, "Error reading attribute '%s'\n", attrName
);
120 return (status_t
)read
;
132 CopyFile(BFile
& dst
, BFile
& src
)
134 status_t err
= CopyFileData(dst
, src
);
138 return CopyAttributes(dst
, src
);