Other cleanups and documentation.

Do the checks, then build the filenames. Do not mix both.
This commit is contained in:
nekral-guest 2007-12-28 00:23:33 +00:00
parent 523392dc0b
commit 9a9a9c0414

View File

@ -109,7 +109,7 @@ static int selinux_file_context (const char *dst_name)
#endif #endif
/* /*
* remove_link - delete a link from the link list * remove_link - delete a link from the linked list
*/ */
static void remove_link (struct link_name *ln) static void remove_link (struct link_name *ln)
{ {
@ -218,39 +218,34 @@ int copy_tree (const char *src_root, const char *dst_root, uid_t uid, gid_t gid)
set_orig++; set_orig++;
} }
while ((0 == err) && (ent = readdir (dir)) != NULL) { while ((0 == err) && (ent = readdir (dir)) != NULL) {
/* /*
* Skip the "." and ".." entries * Skip the "." and ".." entries
*/ */
if ((strcmp (ent->d_name, ".") != 0) &&
if (strcmp (ent->d_name, ".") == 0 || (strcmp (ent->d_name, "..") != 0)) {
strcmp (ent->d_name, "..") == 0) {
continue;
}
/* /*
* Make the filename for both the source and the * Make sure the resulting source and destination
* destination files. * filenames will fit in their buffers.
*/ */
if ( (strlen (src_root) + strlen (ent->d_name) + 2 >
if (strlen (src_root) + strlen (ent->d_name) + 2 > sizeof src_name)
sizeof src_name) { || (strlen (dst_root) + strlen (ent->d_name) + 2 >
sizeof dst_name)) {
err = -1; err = -1;
break; } else {
} /*
snprintf (src_name, sizeof src_name, "%s/%s", src_root, * Build the filename for both the source and
ent->d_name); * the destination files.
*/
if (strlen (dst_root) + strlen (ent->d_name) + 2 > snprintf (src_name, sizeof src_name, "%s/%s",
sizeof dst_name) { src_root, ent->d_name);
err = -1; snprintf (dst_name, sizeof dst_name, "%s/%s",
break; dst_root, ent->d_name);
}
snprintf (dst_name, sizeof dst_name, "%s/%s", dst_root,
ent->d_name);
err = copy_entry (src_name, dst_name, uid, gid); err = copy_entry (src_name, dst_name, uid, gid);
} }
}
}
closedir (dir); closedir (dir);
if (set_orig) { if (set_orig) {
@ -389,8 +384,8 @@ static int copy_symlink (const char *src, const char *dst,
#endif #endif
if ( (symlink (oldlink, dst) != 0) if ( (symlink (oldlink, dst) != 0)
|| (lchown (dst, || (lchown (dst,
uid == (uid_t) - 1 ? statp->st_uid : uid, (uid == (uid_t) - 1) ? statp->st_uid : uid,
gid == (gid_t) - 1 ? statp->st_gid : gid) != 0)) { (gid == (gid_t) - 1) ? statp->st_gid : gid) != 0)) {
return -1; return -1;
} }
@ -417,6 +412,8 @@ static int copy_hardlink (const char *src, const char *dst,
return -1; return -1;
} }
/* If the file could be unlinked, decrement the links counter,
* and delete the file if it was the last reference */
lp->ln_count--; lp->ln_count--;
if (lp->ln_count <= 0) { if (lp->ln_count <= 0) {
remove_link (lp); remove_link (lp);