View Full Version : The predefine inode & Binary file to #define
dum_dum
09-13-2004, 05:05 PM
Hi all,
I'm back with new doubts... if someone can help me. ;)
1 - I know that when i create a file with open() for example it locate a free inode/block and alloc it to me. But i want to make it by myself. How to can i define the inode my file will use ? For example, i want to create a file but it must be always at inode 15029. How to do it ? :)
2 - I want to copy a binary to a #define, and i want to use this #define to "recopy" it to a file. I know that for example to copy a file to file i can open source and destination and use feof() and fgets() for example to read the source and write to destination. But how to read the entire binary source and allocate at a #define ? If i read it with fgets with %d it doesn't work when i recopy it to file, and when i read with %s sometimes doesn't compile the code since some characteres are converted to " that affect the #define tag. How to do it ?
ps.: Code is appreciated. ;)
Thks and Regards.
1) you just don't. It's no coincidence that there is no method to do such a
thing. The only way would be to hack the filesystem driver on kernel level.
2) Nobody does such a thing. There are some ways to achieve that
indirectly by basically linking the file in question to your program, but
usually you allocate memory at runtime and then read it in once at
startup.
To work with binary file data you use read() and write(), other commands
like fscanf(), fprintf() and what not are for text operations only.
dum_dum
09-13-2004, 11:12 PM
1 - Tha's bad. If someone have some idea of how to make a workarround for that...
2 - Can be strange, but i will need store it at my source code (at a define, array, etc). The idea of use read e write appear be good, but how to do it without a feof ? If i use feof i get segvfault
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int in, out;
char buff[512]="\0";
char file1[]="/bin/ls";
char file2[]="test";
in = open(file1, O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
out = open(file2, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_
IROTH);
while( !feof(in) ){
read(in, buff, sizeof(buff));
write(out, buff, strlen(buff));
}
close(in);
close(out);
return(0);
}
What can i use instead feof ? If it work, i will redirect it to my source in a array or define and test write to other binary in execution time to see if work..
Cheers
i3839
09-13-2004, 11:22 PM
The inode number is used by the filesystem, it's not something for you to touch. Not all filesystems have an inode number either. Seems like you try to solve a certain problem the wrong way.
You can make such define, or better, a static const char array. Just make a parser that parses a binary file and gives escaped output which you can use.
Try this one.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int in, out, number;
char buff[512];
char file1[]="/bin/ls";
char file2[]="test";
in = open(file1, O_RDONLY);
out = open(file2, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
/* you should check if the opens really worked... in!=0, out !=0 ? */
while( !feof(in) ) /* actually while(true) would be enough */
{
number=read(in, buff, sizeof(buff));
if(number<=0) break; /* ==0 eof / <0 error */
if(write(out, buff, number) != number ) break;
}
close(in);
close(out);
return(0);
}
You have to open the source for reading what stands to reason I think.
read() returns the number of bytes written and that is the number you
also have to write. I guess you used 512Bytes because that's your block
size, that's not nescessary, you are too far "away" from a level were this
can bother you, you will only get the exact amount of bytes that are there,
not the blocks they are stored in. If number==0 you have eof, if number
<0 you have an error. If your write doesn't do the right number,
something is wrong either.
You don't understand that right I think. An application does not deal with
low level implementation problems. That's what the OS and the drivers are
there for. As long as you don't go that deep into the system the details of
the hardware and the lower abstraction levels don't concern you.
RobSeace
09-14-2004, 01:26 PM
Yeah, as far as choosing your own inode: just give that one up...
Unless you want to hack your kernel, it's just not going to happen...
And, really, there's absolutely NO sane reason that I can think of
why you should need or want to do such a bizarre thing, anyway...
And, feof() only works on stdio FILE*'s, and NOT on int descriptors
as returned by open()! The correct way to detect EOF on FDs is
by checking the return value of read(): it'll return 0 on EOF... So,
just remove that !feof() test from Nope's code (and, replace it with
a simple "1", to create an infinite loop), and it'll give you what you
want, I think... (Oh, and in checking the return value from open(),
you want to check for NEGATIVE values, by the way... Zero is
perfectly valid for an FD, if your stdin happens to be closed...)
dum_dum
09-14-2004, 03:03 PM
Hi
1 - I was searching about it and appear that isn't possible from userland. :( In a article that i found on internet (Linux Internal) the responsable for make this allocation is sys_pipe that call other functions to make the job. :(
2 - Thks it really worked well now. But as expected if i put the output in a array (cont char as suggested) i got errors like "unterminated character constant". So was gaved the idea of parse. What kind of parse are you thinking (please not very hard to make please) ?
I thinked in use uuencode, but it's linux binarys. Someone know a linux functions that encode and decode like uuencode/uudecode ?
Cheers,
RobSeace
09-14-2004, 08:15 PM
"unterminated character constant"? Huh?? I'm not sure what
you're doing that causes that... Sounds like code with a misplaced
single-quote, or something odd... I don't think it has anything to do
with reading/writing files, though...
I'm just not sure what you're trying to do exactly... Do you just
want to read in the entire contents of a file, and keep it in memory
somewhere for some later use? If so, your best bet would either
be to mmap() the file, which will transparently turn it into a memory
pointer for you, OR simply allocate a buffer large enough to hold
it all (you'll need to check the size of the file before starting to read
it) and then simply read() the contents of the file into that buffer...
I don't see the problem...
I also have no idea what you need uuencode for... But, if you do,
I think you'll just have to either code it up yourself (it's not a hard
algorithm), or steal it from one of many other apps out there that
do it...
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.