8000 Avoid creating targeted directory in the tar ? · Issue #1 · rolinh/targo · GitHub
[go: up one dir, main page]

Skip to content

Avoid creating targeted directory in the tar ? #1

Closed

Description

@AlbinOS

I just found your package and it is neat and simple like I love.

I have one issue while creating a tar for the directory 'somedir', I'm trying to create an archive of the form of :

somedir.tar/
..file1
..file2

But I get :

somedir.tar/
..somedir/
....file1
....file2

I trying to find a way to avoid this reading the code but I must say that its hard for me as I'm new to GO. Do you have any quick tips for doing so ?

Thanks a lot,
Albin.

Activity

  1. rolinh commented on Feb 18, 2016

    @rolinh
    Owner

    Hi,

    targo only provides methods to create and extract tar archives from directories. If I understand your use-case correctly, what you would like to do is flatten the content of a directory at the root of the archive. Is that correct? Or would you simply have the possibility to create a tar archive out of a number of files used as tar function input?

  2. AlbinOS commented on Feb 18, 2016

    @AlbinOS
    ContributorAuthor

    Hi Rolinh and thanks for you reply.

    What I would like is the possibility to flatten the content of the target directory at the root of the archive yes.

    I made some tests with your code and I'm not able to achieve this. However, I more or less rewrite the targo.Create function more naively and in a simpler way (without symlink treatment) and I have what I want. But I don't really understand why in targo function case a directory is created inside the tar and in my rewrite, the content is flatten. I'll investigate a little bit more but it's very strange to me.

    Do you think this use-case could enter in the scope of targo ? If yes, I'll be happy to submit a pull request once I find how to achieve it.

  3. rolinh commented on Feb 18, 2016

    @rolinh
    Owner

    What about a variadic create function which would directly take the path to files as argument? Would that fit your use case? Because I am not fond of the idea of flattening the content of a directory at the root of the tar archive. Modifying the original structure of the document is not a good behavior IMHO.

    If you do something like what I propose, considering you handle all the cases including symlink, I'll be happy to merge your pull request. Also, make sure to provide comprehensive tests so we know that the function behaves as expected.

  4. AlbinOS commented on Feb 18, 2016

    @AlbinOS
    ContributorAuthor

    Sorry for the misunderstanding but what I meant is not to flatten all content in the tar. What I would like is just to avoid the first directory given in argument in the tar but to keep the rest of the structure.

    Something like :

    somedir/
    ..file1
    ..somesubdir/
    ....file2
    

    becomes :

    sometar/
    ..file1
    ..somesubdir/
    ....file2
    

    The equivalent in linux command line would be tar -cf somedir.tar path/to/somedir/* instead of tar -cf somedir.tar path/to/somedir.

    But like I said earlier, I have trouble finding what in the code of targo.Create is making the root directory to be present in the tar or what I'm doing that removing it.

  5. rolinh commented on Feb 18, 2016

    @rolinh
    Owner

    I'm confused now. When you do tar -cf somedir.tar path/to/somedir/* what actually happens is that the wildcard argument is expanded to the files and directories contained in somedir, right? You are in fact, in this case, providing multiple arguments to the tar utility. Hence, my proposition of adding a variadic create function still holds, right?
    FYI, targo is not "explicitly adding a root directory". You give it a directory as argument, hence you will find that same directory, including its content, in the resulting tar archive.

  6. AlbinOS commented on Feb 18, 2016

    @AlbinOS
    ContributorAuthor

    Yeah your proposition is perfect. The call that I would like to make is something along the lines of targo.CreateFromFileList('path/to/somedir'; 'path/to/sometar').

    I understand the functionality of targo and I'm totally ok with it. I'm just saying that I have trouble to find in the code of targo.Create what causes this directory to be added in the tar and therefore to write a code that doesn't add this directory.

  7. rolinh commented on Feb 18, 2016

    @rolinh
    Owner

    Yeah, that would do it then :)

    As I already tried to explain, targo simply "walks" through the directory given as argument and recreates this exact structure in the resulting tar archive. The root directory is not treated differently, as explained in the documentation of the filepath.Walk function.

  8. AlbinOS commented on Feb 18, 2016

    @AlbinOS
    ContributorAuthor

    Excuse me if I wasn't entirely clear about my answers. Basically, I had two calls of apparently the same code doing different results, and I was trying to find out why.

    I finally have found what causes the difference between my hacked code and yours. The dirpath that I used calling your function was targo.Create("/tmp/sometar.tar", "/tmp/somedir/") and calling mine was targo.Create("/tmp/sometar.tar", "/tmp/somedir"). Note the trailing slash difference.

    The function causing the behavior of these two call is ´filepath.Dir(dirpath) :

    • filepath.Dir("/tmp/somedir/") => /tmp/somedir
    • filepath.Dir("/tmp/somedir") => /tmp

    I guess its because filepath.Dir consider the last directory of the first call to be '.' and the second call to be somedir.

    So, the conclusion is that I can use targo.Create for my use case without modifying anything to targo but I don't know if it was you intention to have such behavior. Maybe it's worth to document this behavior ?

  9. rolinh commented on Feb 18, 2016

    @rolinh
    Owner

    I can use targo.Create for my use case without modifying anything to targo

    Great.

    Maybe it's worth to document this behavior ?

    It could be useful, yes. I'll happily merge a pull request if you do so 😉

  10. rolinh commented on Feb 20, 2016

    @rolinh
    Owner

    Resolved by #2. Thanks @AlbinOS !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0