Why can’t I get ENODATA from Setxattr? - c

The following is always meeting the ENOENT condition at the end. I would like to print ENODATA. What do I need to change in setxattr?
#include <sys/xattr.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
int ret;
const char * file = "non_existent_file.txt";
const char * name = "user.test";
const char * value = "value";
ssize_t size = strlen(value);
printf("Setting Xattr\n");
ret = setxattr(file, name, value, size, XATTR_REPLACE);
if (ret == -1) {
printf("Xattr set failed: %s\n", strerror(errno));
perror("");
}
if (errno == ENODATA) {
printf("ENODATA\n");
}
if (errno == ENOENT) {
printf("ENOENT\n");
}
return 0;
}
I get “ENOENT” printed from last condition end statements
I tried adding the XATTR_REPLACE flag.
I’ve removed valid paths for the file path
Edit:
Target OS: Ubuntu/ Linux

Related

Create file recursive in C Linux

I want to create file test in this path /tmp/a1/a2/a3/a4/test
But all the directories (a1..a4) doesn't exist, How can I create this file in C at Linux OS?
You can use the mkdir function from sys/stat.h to create the directories as you need them. E.g.,
mkdir("/tmp/a1",0766);
However, you should check, via stat, whether or not the directories exist already.
Once you've created the directory structure, your file can be created with
open("/tmp/a1/a2/a3/a4/test",O_WRONLY|O_CREAT);
Obviously, you need to check the return values of all of these function calls.
Below is a complete function in C that achieves what you want.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
int create_file_with_parent_dirs (char pathname[])
{
for (char *p = pathname; (p = strchr(p ,'/')) != NULL; ++p) {
char c = p[1];
p[1] = '\0';
errno = 0;
if (mkdir(pathname, 0700) != 0 && errno != EEXIST) {
perror("mkdir");
return -1;
}
p[1] = c;
}
int fd = creat(pathname, 0600);
if (fd < 0)
perror("creat");
return fd;
}
int main (void)
{
char pathname[] = "/tmp/a1/a2/a3/a4/test";
create_file_with_parent_dirs(pathname);
}
Note that the array pointed to by pathname must be modifiable. Do not call the function with a string literal. Also beware that the file will be truncated to zero length if it already exists.
You can use this code. This program split the path and check whether the path exist or not if not create the path and create the final path as file.
#include <dirent.h>
#include <errno.h>
#include <bits/stdc++.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
#include <fstream>
using namespace std;
int checkDir(char * path)
{
DIR* dir = opendir(path);
if (dir) {
/* Directory exists. */
closedir(dir);
return 0;
} else if (ENOENT == errno) {
/* Directory does not exist. */
return -1;
} else {
/* opendir() failed for some other reason. */
return -2;
}
}
int make_dir(char *path)
{
int ret = checkDir(path);
if( ret == -1)
{
if (mkdir(path, 0777) == -1)
{
cerr << "Error : " << strerror(errno) << endl;
return -1;
}
else
{
cout << "Directory created";
return 0;
}
}
return ret;
}
int main(int args, char **argv)
{
std::string strpath(argv[1]);
std::string delimeter = "/";
std::string substr1 = "";
int cnt = 0;
std::vector<string> strPaths;
std::string strbck = strpath;
for( int i = strpath.find(delimeter); i != std::string::npos; i = strpath.find(delimeter))
{
if(cnt > 0)
{
substr1 = strbck.substr(0, substr1.length() + i + 1);
strPaths.push_back(substr1);
}
strpath = strpath.substr(i +1,strpath.length());
cnt++;
}
strPaths.push_back(strbck);
std::string str;
for_each( strPaths.begin() ,strPaths.end() -1 , [](std::string str) {make_dir((char*)str.c_str());});
ofstream outfile;
std::ofstream file {strPaths[strPaths.size() -1].c_str() };
file << "hello"<< std::endl;
file.close();
return 0;
}

Example of using sysctl() call in C on Linux

I've read some of the warnings against using the sysctl() call in C, and it seems if I cannot use sysctl() safely, the only other way I can find to make the needed change would be to use soemething like:
system("echo fs.inotify.max_user_watches=NEW_MAX_DIRECTORIES >> /etc/sysctl.conf");
system("sysctl -p");
(of course, this assumes ensuring the binary is running as root. However, I would rather NOT have to shell out using system calls.
Can someone point me in the correct and safe of using sysctl()?
here is a snippet of the code I am using.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
int main ()
{
int ret;
const char *LOGNAME="iNotifyMonitor";
logger(INFO, "================================================");
ret = startDaemon();
daemonRunning = ret;
if (ret == 0)
{
daemonRunning = 1;
FIRST_RUN = 0;
}
if(ret)
{
syslog(LOG_USER | LOG_ERR, "Error starting iNotifyMonitor");
logger(ERR, "Unable to start iNotifyMonitor");
closelog();
return EXIT_FAILURE;
}
signal(SIGINT, signalHandler);
signal(SIGHUP, signalHandler);
char *log_file_name = malloc(sizeof(char *) * sizeof(char *));
sprintf(log_file_name, "%s%s", INM_LOG_DIR, INM_LOG_FILE);
/* Try to open log file to this daemon */
if (INM_OPEN_LOG && INM_LOG_FILE)
{
log_stream = fopen(concatString(INM_LOG_DIR, INM_LOG_FILE), "a+");
if (log_stream == NULL)
{
char *errMsg;
sprintf(errMsg, "Cannot open log file %s, error: %s", concatString(INM_LOG_DIR, INM_LOG_FILE), strerror(errno));
log_stream = stdout;
}
}
else
{
log_stream = stdout;
}
while (daemonRunning == 1)
{
if (ret < 0)
{
logger(LOG_ERR, "Can not write to log stream: %s, error: %s", (log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
ret = fflush(log_stream);
if (ret != 0)
{
logger(LOG_ERR, "Can not fflush() log stream: %s, error: %s",
(log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
int curcount =countDirectory("/home/darrinw/Development/CrossRoads/");
directoryCount = curcount;
if(directoryCounrt > INM_MAX_DIRECTORIES)
{
int newVal = roundUp(directoryCount, 32768);
// call to sysctl() to modify fs.inotify.max_users_watches=newVal
}
sleep(INM_SCAN_INTERVAL);
}
My understanding is that the modern recommended approach to access sysctl variables is via the pseudo-files in /proc/sys. So just open /proc/sys/fs/inotify/max_user_watches and write there.
int fd = open("/proc/sys/fs/inotify/max_user_watches", O_WRONLY);
dprintf(fd, "%d", NEW_MAX_DIRECTORIES);
close(fd);
Error checking left as an exercise.
Modifying /etc/sysctl.conf would make the setting persist across reboots (assuming your distribution uses the file this way, I am not sure if all of them do). That's kind of rude to do automatically; probably better to use the documentation to advise the system administrator to do it themselves if it's needed.

Getting Thread EXC_BAD_ACCESS on BerkeleyDB sample code

I'm writing my very first C program and I was really doing well. The application talks to RESTful server. All was good until I decided to use an embedded database(libdb) for storage. I got this code below that was part of my entire program. My problem is it keeps on crashing on this line:
my_archive->db_home_dir = DEFAULT_HOMEDIR;
I thought I was running out of stack so I malloc'd all my lengthy variables but the problem was still occuring so I decided to separate this libdb part into a new code, but the problem still remains.
Any idea what has gone wrong here?
P.S. I'm doing all the coding in Xcode and stepping through each line after debug breakpoint right after main() doesn't help me a bit. Always ends up on the same error line. Or perhaps I just don't know what I'm doing.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include "db.h"
#define DEFAULT_HOMEDIR "/Users/mark/Documents/bdb/"
#define URLSDB "urls"
typedef struct archive_dbs {
DB *URLS_dbp;
char *db_home_dir;
char *URLS_db_name;
} ARCHIVE_DBS;
void initialize_archivedbs(ARCHIVE_DBS *my_archive)
{
my_archive->db_home_dir = DEFAULT_HOMEDIR; //CRASHES HERE: Thread 1: EXC_BAD_ACCESS (code=2, address=0x1000061da)
my_archive->URLS_dbp = NULL;
my_archive->URLS_db_name = NULL;
}
void set_db_filenames(ARCHIVE_DBS *my_archive)
{
size_t size;
size = strlen(my_archive->db_home_dir) + strlen(URLSDB) + 1;
my_archive->URLS_db_name = malloc(size);
snprintf(my_archive->URLS_db_name, size, "%s%s", my_archive->db_home_dir, URLSDB);
}
int open_database(DB **dbpp, const char *file_name, const char *program_name, FILE *error_file_pointer)
{
DB *dbp;
u_int32_t open_flags;
int ret;
ret = db_create(&dbp, NULL, 0);
if (ret != 0) {
fprintf(error_file_pointer, "%s: %s\n", program_name,
db_strerror(ret));
return(ret);
}
*dbpp = dbp;
dbp->set_errfile(dbp, error_file_pointer);
dbp->set_errpfx(dbp, program_name);
open_flags = DB_CREATE;
ret = dbp->open(dbp,
NULL,
file_name,
NULL,
DB_BTREE,
open_flags,
0);
if (ret != 0) {
dbp->err(dbp, ret, "Database '%s' open failed.", file_name);
return(ret);
}
return (0);
}
int databases_setup(ARCHIVE_DBS *my_archive, const char *program_name, FILE *error_file_pointer)
{
int ret;
ret = open_database(&(my_archive->URLS_dbp), my_archive->URLS_db_name, program_name, error_file_pointer);
if (ret != 0)
return (ret);
printf("databases opened successfully\n");
return (0);
}
int databases_close(ARCHIVE_DBS *my_archive)
{
int ret;
if (my_archive->URLS_dbp != NULL) {
ret = my_archive->URLS_dbp->close(my_archive->URLS_dbp, 0);
if (ret != 0)
fprintf(stderr, "URLS database close failed: %s\n",
db_strerror(ret));
}
printf("databases closed.\n");
return (0);
}
int main(void){
ARCHIVE_DBS *archivedbs;
initialize_archivedbs(archivedbs);
set_db_filenames(archivedbs);
databases_setup(archivedbs, "urlfetcher", NULL);
open_database(&archivedbs->URLS_dbp, "URLS.db", "urlfetcher",
NULL);
databases_close(archivedbs);
}

Why do I get the following linkage error "undefined reference to `__printf__'"

I am using JaetBrains' Clion with MinGW 3.2.1 on windows. and I'm trying to build a project in c.
I keep getting the following linkage error:
undefined reference to `printf'
any Idea How to solve it?
this is my code:
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h> // for time measurement
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <libintl.h>
#define BUFFERSIZE 1
int main(int argc, char** argv) {
assert(argc == 3);
char* inputDirPath = argv[0];
char* keyFilePath = argv[1];
char* outputDirPath = argv[2];
// open key file
int key_fd = open(keyFilePath, O_RDONLY);
if (key_fd < 0) {
printf("Failed opening Key file %s. Error: %s\n", keyFilePath, strerror(errno));
return errno;
}
// making sure the file is not empty
char keyFirstChar;
if (read(key_fd, (void*)keyFirstChar, 1) == 0)
{
printf("Error. Key file is empty %s.", keyFilePath);
return errno;
}
else {
// go back to the begining of the file.
assert(!close(key_fd));
key_fd = open(keyFilePath, O_RDONLY);
if (key_fd < 0) {
printf("Failed opening Key file %s. Error: %s\n", keyFilePath,
strerror(errno)
);
return errno;
}
}
// Temp file name
char inputFilepath[200] ;
struct dirent *dirEntity;
DIR *inputDir_dfd;
// open directory stream
assert((inputDir_dfd = opendir(inputDirPath)) != NULL);
while ((dirEntity = readdir(inputDir_dfd)) != NULL)
{
// full path to input file
sprintf(inputFilepath, "%s/%s",inputDirPath, dirEntity->d_name) ;
// call stat to get file metadata
struct stat statbuf ;
assert( stat(inputFilepath,&statbuf ) != -1 );
// skip directories
if ( ( statbuf.st_mode & S_IFMT ) == S_IFDIR )
{
continue;
}
// open input file
int inputFile_fd = open(inputFilepath, O_RDONLY);
if (inputFile_fd < 0) {
printf("Failed opening file in input directory, %s. Error: %s\n", inputFilepath, strerror(errno));
return errno;
}
// Temp file name
char outputFilePath[200] ;
// full path to file
sprintf(outputFilePath, "%s/%s",outputDirPath, dirEntity->d_name) ;
// open input file
int outputFile_fd = open(outputFilePath, O_WRONLY | O_CREAT | O_TRUNC);
if (outputFile_fd < 0) {
printf("Failed opening file in output directory, %s. Error: %s\n", outputFilePath, strerror(errno));
return errno;
}
char inputFileBuf[BUFFERSIZE];
while (read(inputFile_fd, inputFileBuf, BUFFERSIZE) == BUFFERSIZE){
char keyFileBuf[BUFFERSIZE];
if (read(key_fd, keyFileBuf, BUFFERSIZE) == 0) {
assert(!close(key_fd));
key_fd = open(keyFilePath, O_RDONLY);
if (key_fd < 0) {
printf("Failed opening Key file %s. Error: %s\n", keyFilePath, strerror(errno));
return errno;
}
read(key_fd,keyFileBuf, BUFFERSIZE);
}
char outputToWrite[BUFFERSIZE];
int i;
for(i = 0; i < BUFFERSIZE; i++){
outputToWrite[i] = keyFileBuf[i] ^ inputFileBuf[1];
}
if( write(outputFile_fd, outputToWrite, BUFFERSIZE) == -1){
printf("Failed writing to output file, %s. Error: %s\n", outputFilePath, strerror(errno));
return errno;
};
}
if(close(inputFile_fd) ); // close key file
}
closedir(inputDir_dfd); // close Dir
assert(!close(key_fd)); // close key file
}
thanks.

Change inode ctime in Linux

utimensat() updates ctime when restoring atime and/or mtime, but I need to restore all 3 timestamps. I solved this in a tricky way, by saving the current clock, moving system clock to the needed ctime, calling utimensat(), restoring current clock.
It does the job, but I understand this is a bad solution, so I'm looking for a better one, all in (root) userspace without kernel mods.
Here is the code I wrote to verify utimensat() behaviour and test the solution.
/* Test utimensat() behaviour, restore also Change timestamp
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MYFILE "/home/user1/Work/myfile.txt"
static int get_timestamps(struct stat *sb, const char *descr) {
sleep(1);
if (stat(MYFILE, sb) == -1) {
perror("stat(" MYFILE ") ERROR");
return(errno);
}
char *a = ctime(&sb->st_atime);
char *m = ctime(&sb->st_mtime);
char *c = ctime(&sb->st_ctime);
printf("%s\n", descr);
printf("Last file access: %.*s.%lu\n", (int)strlen(a)-1, a, sb->st_atim.tv_nsec);
printf("Last file modification: %.*s.%lu\n", (int)strlen(m)-1, m, sb->st_mtim.tv_nsec);
printf("Last status change: %.*s.%lu\n\n", (int)strlen(c)-1, c, sb->st_ctim.tv_nsec);
return(0);
}
int main(int argc, char **argv) {
int rc = 0;
struct stat sb_original;
struct stat sb;
// Get file stats
rc = get_timestamps(&sb_original, "Initial");
if (rc) return(rc);
// Read file
FILE *myfile = fopen(MYFILE, "r");
if (myfile == NULL) {
perror("fopen(" MYFILE ") ERROR");
return(errno);
}
char buffer[10];
(void)fread(buffer, 1, 1, myfile);
(void)fclose(myfile);
rc = get_timestamps(&sb, "After fopen(); fread(); fclose()");
if (rc) return(rc);
system("touch " MYFILE);
rc = get_timestamps(&sb, "After touch");
if (rc) return(rc);
// Restore original Last Access timestamp updated after fopen(); fread(); fclose()
if (sb.st_ino) {
int have_sudo = 0;
struct timespec ts[2], tsctime, tsnow;
ts[0].tv_sec = sb_original.st_atim.tv_sec;
ts[0].tv_nsec = sb_original.st_atim.tv_nsec;
ts[1].tv_sec = sb_original.st_mtim.tv_sec;
ts[1].tv_nsec = sb_original.st_mtim.tv_nsec;
tsctime.tv_sec = sb_original.st_mtim.tv_sec;
tsctime.tv_nsec = sb_original.st_mtim.tv_nsec;
if (clock_gettime(CLOCK_REALTIME, &tsnow) < 0) {
perror("clock_gettime(CLOCK_REALTIME) ERROR");
return(errno);
}
/* ##### Fixme: is there a better way to restore all 3 timestamps? */
// Bad solution but does the job ;)
if (clock_settime(CLOCK_REALTIME, &tsctime) < 0) {
perror("clock_settime(CLOCK_REALTIME,ctime) ERROR");
if (errno != EPERM)
return(errno);
have_sudo = 1;
}
if (utimensat(0, MYFILE, ts, 0) < 0) { // This updates Change timestamp!
perror("utimensat(" MYFILE ") ERROR");
return(errno);
}
if (have_sudo && clock_settime(CLOCK_REALTIME, &tsnow) < 0) {
perror("clock_settime(CLOCK_REALTIME,now) ERROR");
return(errno);
}
rc = get_timestamps(&sb, "After utimensat()");
if (rc) return(rc);
}
return(0);
}

Resources