git diff of just one file - c

The only way I found to get a diff from a single file using libgit2 is through git_diff_foreach and checking the filename at the diff_file_cb callback.
It's not the way I wanted to do this, I was looking for something easier.
Is there another way to do this?

Just to clarify, git_diff_index_to_workdir (or git_diff_tree_to_tree or another such function) finds the list of changed files and then git_diff_foreach walks through the found files and the text of the diffs. You can pass a "pathspec" in the options structure for git_diff_index_to_workdir that will limit the files being checked. You would do that as mentioned in the prior answer.
As a slightly broader example, if you wanted to diff a more complex set of files, you could write something like:
git_diff *diff;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
char *files[3];
files[0] = "myfile.txt";
files[1] = "yourfile.txt";
files[2] = "some/directory/*.h"
opts.pathspec.count = 3;
opts.pathspec.strings = files;
if (git_diff_index_to_workdir(&diff, repo, NULL, &opts) < 0) {
printf("Failed to diff\n");
exit(1);
}
git_diff_foreach(diff, file_cb, NULL, NULL, NULL);
git_diff_free(diff);
You can pass as many file names or file patterns as you like. If you want to disable the pattern matching behavior (i.e. expanding * and such), you can write opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH and only exact file name matches will be used.

Much easier using the pathspec option and git_diff_index_to_workdit instead of git_diff_foreach
char *pathspec = "foo.bar";
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
git_diff_index_to_workdir(&diff, repo, NULL, &opts);

Related

Remove file from directory - ext-like file system implementation

I have an issue. I'm currently trying to implement an ext-ish file system. I've done the inode operations such as read and write. I've created a structure that represents both a regular file and a directory. I have a problem when trying to remove a certain file from the directory.
char
dirremove(struct dirent *dir, struct dirent *file)
{
dirent_t n = {.mode = NODDIR, .inumber = remdirnod,
.r = 0, .w = 0};
strcpy(n.nm, dir->nm);
dirent_t t;
dir->r = 0;
char r = 1;
while (!dirread(dir, &t))
{
int tt = dir->r;
dir->r = 0;
dirent_t ff[3];
filread(ff, dir, 3 * entrysiz);
dir->r = tt;
if (!strcmp(t.nm, ""))
return 1;
if (!(!strcmp(t.nm, file->nm) && !(r = 0)))
assert(!dirappend(&n, &t));
}
assert(n.w == dir->w - entrysiz);
dir->w = n.w;
dir->r = n.r;
copyinode(dir->inumber, remdirnod);
return r;
}
This is the function called from the rm command. It takes the directory object (where the file is stored) and the file object to be deleted. I know this solution is not the best in terms of speed and memory usage but I'm still a beginner in this area, so don't hate me a lot, please :).
The function is designed to do the following. It has to read all files and check if the current is the one to be deleted. If not, the file is added to a new directory (empty in the beginning) which will replace the old one at the end of the function. The "new" directory is an entry saved entirely for this purpose, so there isn't a chance that all inodes are already used.
The test that I've done is to create a file (works fine), then remove it, then create it again and remove it. Everything works perfectly except for the second execution of the dirremove function. The directory has its dot and dot-dot directories by default so it goes through them first. The result is that the first deletion is successful. Everything works perfectly. But the second time things go wrong somewhere.
int tt = dir->r;
dir->r = 0;
dirent_t ff[3];
filread(ff, dir, 3 * entrysize;
dir->r = tt;
I added the ff array that should read the whole content of the directory and this would help me figure out if the correct files are there. On the first and second iteration, all files (".", ".." and "some-other-file") are there but at the iteration which should hold the object of the file that's to be removed the third file suddenly goes all zeroes.
I've debugged this for several hours but it continues to fail the same way.
Probably I didn't explain the failure the best way, but there are a lot of things that I forgot to say, so if I missed something please don't ignore the question and just ask about it.

Unique String generator

I want to make a program (network server-client).
One of the specification for this program is next:
The server will receive the sent packages and save it into a file, with a unique name (generated by the server at the moment the transfer starts.
Ex __tf_"unique_random_string".txt
I made a function that returns a pointer to a "unique" string created.
The problem is: If i stop the server and then start it again it will generate the same names.
Ex:this file names were generated and then i stopped the server.
__ft_apqfwk.txt
__ft_arzowk.txt
__ft_cdyggx.txt
I start it again and i try to generate 3 file names. Them will be the same.
Sorry for my english. I'm still learning it.
My function to generate this "unique string" is:
char *create_random_name(void)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
char *file_name;
int i=0;
int key;
if((file_name = malloc(16 * sizeof ( char )) ) == NULL)
{
printf("Failed to alloc memory space\n");
return NULL;
}
strcpy(file_name,"__ft_");
for(i=5 ; i<11 ; i++)
{
key = rand() % (int)(sizeof(charset)-1);
file_name[i]=charset[key];
}
strcat(file_name,".txt");
file_name[15] = '\0';
return file_name;
}
One option is saving to a file the names that have been used, and using them as a checklist. You also want to seed rand with something like srand(time(NULL)).
another is ignoring the randomisation, and just going in order, e.g. aaa, aab aac...aba ,abb etc. Again, save where your cycle is up to on a file.
Your question seems a little bit unclear but if you want to generate a unique string there are a couple of things you can consider:
Get System timestamp ( yyyy-MM-dd-HH-mm-ss-fff-tt)
Use Random function to generate a random number
Combine this with your function and I am sure you will get a unique string.
Hope it helps !
If it's available, you could avoid manually generating random names that might collide and let the system do it for you (and handle collision resolution by creating a new name) by using mkstemps. This is also safer because it opens the file for you, removing the risk of a random name being generated, verified to be unique, then trying to open it and discovering another thread/process raced in and created it.
char name[] = "/path/to/put/files/in/__ft_XXXXXX.txt";
int fd = mkstemps(name, strlen(".txt"));
if (fd == -1) { ... handle error ... }
After mkstemps succeeds, name will hold the path to the file (it's mutated in place, replacing the XXXXXX string), and fd will be an open file descriptor to that newly created file; if you need a FILE*, use fdopen to convert to a stdio type.
Before calling rand(),--- once and only once---, call srand(time()) to initialize the random number generator.
Before settling on any specific file name, call stat() to assure that file name does not already exist.

How to get values from a config file

I have the following Config.cfg
[DD]
user=**
password=***
database=***
IPServidor=****
port=***
[Controller]
Control1=8
Temp=5
Hum=7
Link=8
Volt=9
[Controller]
Control2=10
Temp=5
Hum=7
Link=8
Volt=9
I would like to read the values of the controllers only and print them to the screen like
Controller_8: 5,7,8,9
I do not want to use libconfig or glib because I have problem with undefined functions. I did the installation, I have the headers but I do not know why it does not work. So I want another solution. My first thought is with the usage of strchr to find the lines which I want (to ignore [DD] table in my case) and with the usage of strtok to get only the values of temp,hum,link,volt
char buffer1[100];
FILE *f = fopen("/home/pi/Desktop/Config.cfg","r");
while(fgets(buffer1, sizeof(buffer1), f))
{
printf("%s",buffer1);
char *pos1 = strchr(buffer1,'Controller');
if (pos1)
{
item = strtok (buffer1,"Control");
printf("Results: %s\n", buffer1);
}
}
The above code is not correct. Is just a thought. Is there any better way?
Don't try parsing ini files, use some existing library.
Ini file parsing is included in a number of "frameworks", for instance in Gtk+ or on Windows. If you can't access those, you can still use some standalone library, for instance: http://ndevilla.free.fr/iniparser/

How do I use Minizip (on Zlib)?

I'm trying to archive files for a cross-platform application, and it looks like Minizip (built on zlib) is about as portable as archivers come.
When I try to run the following dummy code, however, I get a system error [my executable] has stopped working. Windows can check online for a solution to the problem.
Can anyone help me see how to use this library? — (there's no doc or tutorial anywhere that I can find)
zip_fileinfo zfi;
int main()
{
zipFile zf = zipOpen("myarch.zip",APPEND_STATUS_ADDINZIP);
int ret = zipOpenNewFileInZip(zf,
"myfile.txt",
&zfi,
NULL, 0,
NULL, 0,
"my comment for this interior file",
Z_DEFLATED,
Z_NO_COMPRESSION
);
zipCloseFileInZip(zf);
zipClose(zf, "my comment for exterior file");
return 0;
}
Specs: Msys + MinGW, Windows 7, using zlibwapi.dll from zlib125dll.zip/dll32
Since I found this question via Google and it didn't contain any complete, working code, I am providing some here for future visitors.
int CreateZipFile (std::vector<wstring> paths)
{
zipFile zf = zipOpen(std::string(destinationPath.begin(), destinationPath.end()).c_str(), APPEND_STATUS_CREATE);
if (zf == NULL)
return 1;
bool _return = true;
for (size_t i = 0; i < paths.size(); i++)
{
std::fstream file(paths[i].c_str(), std::ios::binary | std::ios::in);
if (file.is_open())
{
file.seekg(0, std::ios::end);
long size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (size == 0 || file.read(&buffer[0], size))
{
zip_fileinfo zfi = { 0 };
std::wstring fileName = paths[i].substr(paths[i].rfind('\\')+1);
if (S_OK == zipOpenNewFileInZip(zf, std::string(fileName.begin(), fileName.end()).c_str(), &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION))
{
if (zipWriteInFileInZip(zf, size == 0 ? "" : &buffer[0], size))
_return = false;
if (zipCloseFileInZip(zf))
_return = false;
file.close();
continue;
}
}
file.close();
}
_return = false;
}
if (zipClose(zf, NULL))
return 3;
if (!_return)
return 4;
return S_OK;
}
The minizip library does come with examples; minizip.c for zipping and miniunz.c for unzipping. Both are command line utilities that show how to use the library. They are a mess though.
You also need to fill the zfi zip_fileinfo. At the very least you should initialize the structure to zero. zfi contains information about the file you want to store using zipOpenNewFileInZip. The structure should contain the date and attributes of "myfile.txt".
I recommend using PKWARE Desktop to diagnosis zip issues. It shows the structure/properties of the files in the ZIP and the ZIP file itself. When I opened the myarch.zip it told me there were errors. I drilled down into the file properties and found that the attributes were off.
The minizip lib is well documented. Just open the zip.h for details.
I can tell you here, you may have passed a wrong parameter for zipOpen. (APPEND_STATUS_ADDINZIP requires an existing zip file!)
Also, please check whether zipOpen returns a valid zipFile handle.

Combining reading and writing of XML configuration functions.

I’m using libxml2 to create and read XML files in c that contain configuration information for the program I’m writing. The program makes its own configuration files (or another program sends it a configuration file and asks the program to run based off the config file), so the XML config files don’t need to be really easy for a human to read.
These configuration files contain lots of values and are really long. So right now I have a function that makes the XML files and another that reads the XML files. However any-time I change the write XML function than I need to also change the read xml function. So there isn’t actual code duplication, but something really close (ie. BAD) and because the configuration files are so long it is rather tedious to try to make sure everything is reading and writing the same thing.
This is the current set up.
struct config_data
{
// category one
int X
int Y
// category two
int Z
int A
}
int makeXMLsheet(char* fileout)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr root_node = NULL; /* node pointers */
LIBXML_TEST_VERSION;
doc = xmlNewDoc((xmlChar*) "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "configuration_file");
xmlDocSetRootElement(doc, root_node);
// catogory one
xmlNodePtr category_one = xmlNewChild(root_node, NULL, BAD_CAST "category_one", NULL);
xmlNewChild(category_one, NULL, BAD_CAST "x", BAD_CAST "12345");
xmlNewChild(category_one, NULL, BAD_CAST "y", BAD_CAST "1");
// catogory two
xmlNodePtr category_two = xmlNewChild(root_node, NULL, BAD_CAST "category_two", NULL);
xmlNewChild(category_two, NULL, BAD_CAST "Z", BAD_CAST "12345");
xmlNewChild(category_two, NULL, BAD_CAST "A", BAD_CAST "1");
xmlSaveFormatFileEnc(fileout, doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
return 0;
}
int readXMLsheet(char* filename,struct *config_data)
{
xmlDocPtr doc = getdoc(filename);
config_data->X = getIntegerFromXML(0,doc,(xmlChar*)"//configuration_file/category_one/X");
config_data->Y = getIntegerFromXML(0,doc,(xmlChar*)"//configuration_file/category_one/Y");
config_data->Z = getIntegerFromXML(0,doc,(xmlChar*)"//configuration_file/category_two/Z");
config_data->A = getIntegerFromXML(0,doc,(xmlChar*)"//configuration_file/category_two/a");
xmlFreeDoc(doc);
return 0;
}
Where
int getIntegerFromXML(int defaultValue, xmlDocPtr doc, xmlChar *xpath)
Does as its name says and gets a integer from the opened XML document that has the xpath location, and if it fails then it fills it with the default value so that the program doesn't crash and burn.
So I want to try to some how combine the read and write functions into one. My sample struct config-data is tiny compared to the number of values I actually have in my configuration struct, so combining them would make keeping track of everything much easier.
So I was thinking something like this.
int openXMLvalue(X, Y, Z, readOrWrite, defaultValue, value);
where X, Y, Z are the parent nodes, but there might be more or less than 3.
Any ideas on how to do this? Maybe make some type of array?
I would make generic read and write functions that populate (or serialize) a generic configuration structure.
A simplified case would be to create an key/value structure in memory with get/set methods. The generic writeToXml function would simply create elements with key names containing the values.
If warranted, a hierarchical tree structure could be used instead, and perhaps add a few validation rules when reading a configuration file (a simple one would be to use an XML Schema for validation) to verify that required configuration values exist and are valid.
To add, change or remove configuration values would then only require the following steps (note that neither read or write functions require update):
Decide the new format of the configuration file
Update existing configuration files
Update any places in the application using the configuration values
Optionally update validation rules
Because of the large configuration file size, we switched to using sqlite. Then we made a function that would read a database and make an xml sheet, we made a function that would read an xml sheet and populate the database, and working on functions to print the database to stout and fill the C struct. Think this is going to make life much easier.

Resources