aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-go/directory.go
blob: d27e62079a1843a7d833fbddc35b448b6082cc32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package storage

import (
    "encoding/xml"
    "net/http"
    "net/url"
)

// Directory represents a directory on a share.
type Directory struct {
    fsc        *FileServiceClient
    Metadata   map[string]string
    Name       string `xml:"Name"`
    parent     *Directory
    Properties DirectoryProperties
    share      *Share
}

// DirectoryProperties contains various properties of a directory.
type DirectoryProperties struct {
    LastModified string `xml:"Last-Modified"`
    Etag         string `xml:"Etag"`
}

// ListDirsAndFilesParameters defines the set of customizable parameters to
// make a List Files and Directories call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166980.aspx
type ListDirsAndFilesParameters struct {
    Marker     string
    MaxResults uint
    Timeout    uint
}

// DirsAndFilesListResponse contains the response fields from
// a List Files and Directories call.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166980.aspx
type DirsAndFilesListResponse struct {
    XMLName     xml.Name    `xml:"EnumerationResults"`
    Xmlns       string      `xml:"xmlns,attr"`
    Marker      string      `xml:"Marker"`
    MaxResults  int64       `xml:"MaxResults"`
    Directories []Directory `xml:"Entries>Directory"`
    Files       []File      `xml:"Entries>File"`
    NextMarker  string      `xml:"NextMarker"`
}

// builds the complete directory path for this directory object.
func (d *Directory) buildPath() string {
    path := ""
    current := d
    for current.Name != "" {
        path = "/" + current.Name + path
        current = current.parent
    }
    return d.share.buildPath() + path
}

// Create this directory in the associated share.
// If a directory with the same name already exists, the operation fails.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166993.aspx
func (d *Directory) Create() error {
    // if this is the root directory exit early
    if d.parent == nil {
        return nil
    }

    headers, err := d.fsc.createResource(d.buildPath(), resourceDirectory, nil, mergeMDIntoExtraHeaders(d.Metadata, nil), []int{http.StatusCreated})
    if err != nil {
        return err
    }

    d.updateEtagAndLastModified(headers)
    return nil
}

// CreateIfNotExists creates this directory under the associated share if the
// directory does not exists. Returns true if the directory is newly created or
// false if the directory already exists.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166993.aspx
func (d *Directory) CreateIfNotExists() (bool, error) {
    // if this is the root directory exit early
    if d.parent == nil {
        return false, nil
    }

    resp, err := d.fsc.createResourceNoClose(d.buildPath(), resourceDirectory, nil, nil)
    if resp != nil {
        defer readAndCloseBody(resp.body)
        if resp.statusCode == http.StatusCreated || resp.statusCode == http.StatusConflict {
            if resp.statusCode == http.StatusCreated {
                d.updateEtagAndLastModified(resp.headers)
                return true, nil
            }

            return false, d.FetchAttributes()
        }
    }

    return false, err
}

// Delete removes this directory.  It must be empty in order to be deleted.
// If the directory does not exist the operation fails.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166969.aspx
func (d *Directory) Delete() error {
    return d.fsc.deleteResource(d.buildPath(), resourceDirectory)
}

// DeleteIfExists removes this directory if it exists.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166969.aspx
func (d *Directory) DeleteIfExists() (bool, error) {
    resp, err := d.fsc.deleteResourceNoClose(d.buildPath(), resourceDirectory)
    if resp != nil {
        defer readAndCloseBody(resp.body)
        if resp.statusCode == http.StatusAccepted || resp.statusCode == http.StatusNotFound {
            return resp.statusCode == http.StatusAccepted, nil
        }
    }
    return false, err
}

// Exists returns true if this directory exists.
func (d *Directory) Exists() (bool, error) {
    exists, headers, err := d.fsc.resourceExists(d.buildPath(), resourceDirectory)
    if exists {
        d.updateEtagAndLastModified(headers)
    }
    return exists, err
}

// FetchAttributes retrieves metadata for this directory.
func (d *Directory) FetchAttributes() error {
    headers, err := d.fsc.getResourceHeaders(d.buildPath(), compNone, resourceDirectory, http.MethodHead)
    if err != nil {
        return err
    }

    d.updateEtagAndLastModified(headers)
    d.Metadata = getMetadataFromHeaders(headers)

    return nil
}

// GetDirectoryReference returns a child Directory object for this directory.
func (d *Directory) GetDirectoryReference(name string) *Directory {
    return &Directory{
        fsc:    d.fsc,
        Name:   name,
        parent: d,
        share:  d.share,
    }
}

// GetFileReference returns a child File object for this directory.
func (d *Directory) GetFileReference(name string) *File {
    return &File{
        fsc:    d.fsc,
        Name:   name,
        parent: d,
        share:  d.share,
    }
}

// ListDirsAndFiles returns a list of files and directories under this directory.
// It also contains a pagination token and other response details.
//
// See https://msdn.microsoft.com/en-us/library/azure/dn166980.aspx
func (d *Directory) ListDirsAndFiles(params ListDirsAndFilesParameters) (*DirsAndFilesListResponse, error) {
    q := mergeParams(params.getParameters(), getURLInitValues(compList, resourceDirectory))

    resp, err := d.fsc.listContent(d.buildPath(), q, nil)
    if err != nil {
        return nil, err
    }

    defer resp.body.Close()
    var out DirsAndFilesListResponse
    err = xmlUnmarshal(resp.body, &out)
    return &out, err
}

// SetMetadata replaces the metadata for this directory.
//
// Some keys may be converted to Camel-Case before sending. All keys
// are returned in lower case by GetDirectoryMetadata. HTTP header names
// are case-insensitive so case munging should not matter to other
// applications either.
//
// See https://msdn.microsoft.com/en-us/library/azure/mt427370.aspx
func (d *Directory) SetMetadata() error {
    headers, err := d.fsc.setResourceHeaders(d.buildPath(), compMetadata, resourceDirectory, mergeMDIntoExtraHeaders(d.Metadata, nil))
    if err != nil {
        return err
    }

    d.updateEtagAndLastModified(headers)
    return nil
}

// updates Etag and last modified date
func (d *Directory) updateEtagAndLastModified(headers http.Header) {
    d.Properties.Etag = headers.Get("Etag")
    d.Properties.LastModified = headers.Get("Last-Modified")
}

// URL gets the canonical URL to this directory.
// This method does not create a publicly accessible URL if the directory
// is private and this method does not check if the directory exists.
func (d *Directory) URL() string {
    return d.fsc.client.getEndpoint(fileServiceName, d.buildPath(), url.Values{})
}