I have a memory address of the start of a number of structs, but need to eliminate some from my output. I think the memory space would look something like
+------------------+------------------+------------------+
| struct | struct | struct |
| linux_dirent64{ | linux_dirent64{ | linux_dirent64{ |
| first} | second} | third} |
+------------------+------------------+------------------+
|*p|-------------->| |
As I'm iterating through each struct and returning some data in it, there may be one I need to skip. Would overwriting the first part of the struct with a pointer to the next give me the result I'm after? If so, how do I do that? And if not, how should I approach this?
I know there would be an easier solution if I modified the struct to include what I needed, but unfortunately it's not an option as it's part of the Linux Kernel.
Thanks for any replies.
EDIT: As everyone is interested, I'm filtering the structs based on filename. At the moment, I'm literally overwriting the d_name part with O's so it isn't returned by a system call. However this causes different problems depending on which application issues the system call so I need a more thorough way of getting rid of an instance of the struct.
I've since come up with the following, however it gets killed by the kernel if the d_name test returns true.
while(pos < length){
printk("d_name = %s\t| pos = %i\t| d_reclen = %i\t| st = %i|\n", dirent->d_name, pos, dirent->d_reclen, st);
if((st = strcmp(dirent->d_name, "testFile")) == 0){
printk("Out of context file %s\n", dirent->d_name);
posOverwrite = pos;
size = dirent->d_reclen;
while(posOverwrite < length){
struct linux_dirent64 *next = (struct linux_dirent64 *)(pos + dirent->d_reclen);
memcpy(dirent, next, sizeof(next));
dirent = next;
next = next + next->d_reclen;
}
dirent = (struct linux_dirent64 *) pos;
continue;
}
pos = pos + dirent->d_reclen; //Push our position along according to the size of the dirent
dirent = (struct linux_dirent64 *) (p+pos); //point our dirent to the next calculated system dirent
}
In the end I solved this by creating the new memory buffer, and copying all the structs I needed to the new buffer, leaving out any I didn't based on the d_name test. After I'd gone through the whole list, I then memcpy over my entire new list. Thanks for all the help guys :)