mirror of
https://github.com/ipmitool/ipmitool.git
synced 2025-05-11 19:17:22 +00:00
use portabale file io
This commit is contained in:
parent
c1fd61b419
commit
eab79363a2
@ -155,52 +155,54 @@ unsigned char ipmi_csum(unsigned char * d, int s)
|
|||||||
* file: filename
|
* file: filename
|
||||||
* rw: read-write flag, 1=write
|
* rw: read-write flag, 1=write
|
||||||
*/
|
*/
|
||||||
int ipmi_open_file(const char * file, int rw)
|
FILE * ipmi_open_file(const char * file, int rw)
|
||||||
{
|
{
|
||||||
struct stat st1, st2;
|
struct stat st1, st2;
|
||||||
int fp;
|
FILE * fp;
|
||||||
|
|
||||||
/* verify existance */
|
/* verify existance */
|
||||||
if (lstat(file, &st1) < 0) {
|
if (lstat(file, &st1) < 0) {
|
||||||
if (rw) {
|
if (rw) {
|
||||||
/* does not exist, ok to create */
|
/* does not exist, ok to create */
|
||||||
fp = open(file, O_WRONLY|O_TRUNC|O_EXCL|O_CREAT, 0600);
|
fp = fopen(file, "w");
|
||||||
if (fp < 0) {
|
if (!fp) {
|
||||||
printf("ERROR: Unable to open file %s for write: %s\n",
|
printf("ERROR: Unable to open file %s for write: %s\n",
|
||||||
file, strerror(errno));
|
file, strerror(errno));
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
return fp;
|
return fp;
|
||||||
} else {
|
} else {
|
||||||
printf("ERROR: File %s does not exist\n", file);
|
printf("ERROR: File %s does not exist\n", file);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* it exists - only regular files, not links */
|
/* it exists - only regular files, not links */
|
||||||
if (!S_ISREG(st1.st_mode)) {
|
if (!S_ISREG(st1.st_mode)) {
|
||||||
printf("ERROR: File %s has invalid mode: %d\n", file, st1.st_mode);
|
printf("ERROR: File %s has invalid mode: %d\n", file, st1.st_mode);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* allow only files with 1 link (itself) */
|
/* allow only files with 1 link (itself) */
|
||||||
if (st1.st_nlink != 1) {
|
if (st1.st_nlink != 1) {
|
||||||
printf("ERROR: File %s has invalid link count: %d != 1\n",
|
printf("ERROR: File %s has invalid link count: %d != 1\n",
|
||||||
file, (int)st1.st_nlink);
|
file, (int)st1.st_nlink);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = open(file, rw ? (O_WRONLY|O_TRUNC) : O_RDONLY, 0600);
|
fp = fopen(file, rw ? "w+" : "r");
|
||||||
if (fp < 0) {
|
if (!fp) {
|
||||||
printf("ERROR: Unable to open file %s: %s\n",
|
printf("ERROR: Unable to open file %s: %s\n",
|
||||||
file, strerror(errno));
|
file, strerror(errno));
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* stat again */
|
/* stat again */
|
||||||
if (fstat(fp, &st2) < 0) {
|
if (fstat(fileno(fp), &st2) < 0) {
|
||||||
close(fp);
|
printf("ERROR: Unable to stat file %s: %s\n",
|
||||||
return -1;
|
file, strerror(errno));
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* verify inode, owner, link count */
|
/* verify inode, owner, link count */
|
||||||
@ -208,8 +210,8 @@ int ipmi_open_file(const char * file, int rw)
|
|||||||
st2.st_uid != st1.st_uid ||
|
st2.st_uid != st1.st_uid ||
|
||||||
st2.st_nlink != 1) {
|
st2.st_nlink != 1) {
|
||||||
printf("ERROR: Unable to verify file %s\n", file);
|
printf("ERROR: Unable to verify file %s\n", file);
|
||||||
close(fp);
|
fclose(fp);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fp;
|
return fp;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user